14

New to Python and come from a statically typed language background. I want type hints for https://lxml.de just for ease of development (mypy flagging issues and suggesting methods would be nice!)

To my knowledge, this is a python 2.0 module and doesn’t have types. Currently I’ve used https://mypy.readthedocs.io/en/stable/stubgen.html to create stub type definitions and filling in “any”-types I’m using with more information, but it’s really hacky. Are there any safer ways to get type hints?

hoefling
  • 59,418
  • 12
  • 147
  • 194
Ian
  • 301
  • 3
  • 6

3 Answers3

14

There is an official stubs package for lxml now called lxml-stubs:

$ pip install lxml-stubs

Note, however, that the stubs are still in development and are not 100% complete yet (although very much usable from my experience). These stubs were once part of typeshed, then curated by Jelle Zijlstra after removal and now are developed as part of the lxml project.

If you want the development version of the stubs, install via

$ pip install git+https://github.com/lxml/lxml-stubs.git

(the project's readme installation command is missing the git+ prefix in URL's scheme and won't work).

hoefling
  • 59,418
  • 12
  • 147
  • 194
  • Seems to be buggy :( ``` error: Unexpected keyword argument "namespaces" for "find" of "_Element"; did you mean "namespace"? error: "_Element" has no attribute "replace" ``` – pihentagy Jan 12 '21 at 18:18
2

Welcome to check out types-lxml if any late comer are still interested. I have done quite some gap filling over the years, so now it can be considered completed for all major submodules: lxml.etree, lxml.html and lxml.objectify. The next release after March 2023 should cover everything except lxml.isoschematron, which does very powerful schema validation, but few people would use it.

I’ve used stubgen to create stub type definitions and filling in “any”-types

This is actually the correct approach if it's not lxml; creating template from mypy stubgen is the starting point for many stub files. But lxml is mostly written in Cython, for which stubgen do not have perfect support yet. Besides, as OP noted, this is a python 2.0 era module, and author uses function arguments in a quite polymorphous way. There are lots of unique challenges annotating lxml, as lxml is essentially a python interface for libxml and libxslt in its core.

As an example, the support of both unicode and bytes input complicates matter too; this is the same difficulty found when annotating xml.etree bundled with python, but in a much greater magnitude.

[Disclaimer: I'm maintainer of types-lxml package mentioned above]

Abel Cheung
  • 417
  • 5
  • 12
1

I would not call this "hacky", rather it is gradual typing.

You can take a closer look at lxml-stubs repository. From about:

This repository contains external type annotations (see PEP 484) for the lxml package. Such type annotations are normally included in typeshed, but lxml's annotations were frequently problematic and have therefore been deleted from typeshed. In particular, the stubs are incomplete and it has been difficult to provide complete stubs.

Perhaps it will be useful to you

alex_noname
  • 26,459
  • 5
  • 69
  • 86