Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type hints for lxml?

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?

like image 694
Ian Avatar asked Aug 05 '20 06:08

Ian


People also ask

What is lxml?

lxml is a Python library which allows for easy handling of XML and HTML files, and can also be used for web scraping. There are a lot of off-the-shelf XML parsers out there, but for better results, developers sometimes prefer to write their own XML and HTML parsers. This is when the lxml library comes to play.

What is Etree in lxml?

lxml. etree supports parsing XML in a number of ways and from all important sources, namely strings, files, URLs (http/ftp) and file-like objects. The main parse functions are fromstring() and parse(), both called with the source as first argument.

Is lxml included in Python?

lxml has been downloaded from the Python Package Index millions of times and is also available directly in many package distributions, e.g. for Linux or macOS.


1 Answers

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).

like image 168
hoefling Avatar answered Jan 02 '23 07:01

hoefling