types-lxml
types-lxml copied to clipboard
Complete lxml external type annotation
Important note
types-lxml 2024.03.27release requires cssselect package to work, sincelxml.cssselectsubmodule utilises inline annotation fromcssselect 1.2.0.- Next release (
2024.04.14) requiresmypy 1.9;2024.03.27is the last release supportingmypy 1.5.
Introduction
This repository contains external type annotations for lxml. It can be used by type-checking tools (currently supporting mypy and pyright) to check code that uses lxml, or used within IDEs like VSCode or PyCharm to facilitate development.
Goal ① : Completion
Now the coverage of lxml submodules is complete (unless intentionally rejected, see further below), thus no more considered as partial:
- [x]
lxml.etree - [x]
lxml.html- [x]
lxml.html.builder - [x]
lxml.html.clean(already removed in lxml 5.2.0, this project will follow suite in future) - [x]
lxml.html.diff - [x]
lxml.html.html5parser - [x]
lxml.html.soupparser
- [x]
- [x]
lxml.isoschematron - [x]
lxml.objectify - [x]
lxml.builder - [x]
lxml.cssselect - [x]
lxml.sax - [x]
lxml.ElementInclude
Following submodules will not be implemented due to irrelevance to type checking or other reasons:
lxml.etree.Schematron(obsolete and superseded bylxml.isoschematron)lxml.usedoctestlxml.html.usedoctestlxml.html.formfill(shouldn't have existed, this would belong to HTTP libraries likerequestsorhttpx)
Check out project page for future plans and progress.
Goal ② : Support multiple type checkers
Currently the annotations are validated for both mypy and pyright.
In the future, there is plan to bring even more type checker support.
Goal ③: Review and test suite
- [x] All prior
lxml-stubscontributions are reviewed thoroughly, bringing coherency of annotation across the whole package - [x] Much more extensive test cases
- [x] Mypy test suite already vastly expanded
- [x] Perform runtime check, and compare against static type checker result; this guarantees annotations are indeed working in real code, not just in some cooked up test suite
- [x] Proof of concept for incorporating
pyrightresult under progress, currently just comparingreveal_type()results - [ ] Migrate static
mypytests to runtimepyrighttests in future (under progress)
- [x] Proof of concept for incorporating
- [x] Modernize package building infrastructure
Goal ④ : Support for IDEs
Despite having no official PEP, some IDEs support showing docstring from external annotations. This package tries to bring type annotation specific docstrings for some lxml classes and functions, explaining how they can be used. Following screenshots show what would look like in Visual Studio Code, behaving as if docstrings come from real python code:

Besides docstring, current annotations are geared towards convenience for code writers instead of absolute logical 'correctness'. The deviation of class inheritance for HtmlComment and friends is one prominent example.
Installation
The normal choice for most people is to fetch package from PyPI via pip:
pip install -U types-lxml
There are a few other alternatives though.
From downloaded wheel file
Head over to latest release in GitHub and download wheel file (with extension .whl), which can be installed in the same way as PyPI package:
pip install -U types-lxml*.whl
Bleeding edge from GitHub
pip install -U git+https://github.com/abelcheung/types-lxml.git
History
Type annotations for lxml were initially included in typeshed, but as it was still incomplete at that time, the stubs are ripped out as a separate project. The code was since then under governance of lxml, until 2022 when this fork intended to revamp lxml-stubs completely and emerge into separate project.
types-lxml is a fork of lxml-stubs that strives for the goals described above, so that most people would find it more useful.