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.
There are lots of enhancements on top of lxml-stubs:
- Main goal ① Completes annotation for at least 90% of publicly used
lxmlAPI. Besides various completed classes and methods, here are currently implemented extra submodules:-
lxml.builder -
lxml.html.builder -
lxml.html.clean -
lxml.html.html5parser -
lxml.html.soupparser -
lxml.sax - Check out project page for future plans and progress
-
- Main goal ② All existing contributions reviewed thoroughly, bringing coherency of annotation across the whole package
- Guarantees error free for
pyrightbasic checking mode as well - Much more extensive test cases
- Guarantees error free for
- Modernize package building infrastructure
This is the normal choice for most people:
pip install -U types-lxml If there is plan to use html submodule for external libraries (mainly lxml.html.html5parser and lxml.html.soupparser), please install extra dependencies instead:
pip install -U types-lxml[extra] 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 pip install -U git+https://github.com/abelcheung/types-lxml.git There are two special stub-only classes that do not exist as concrete class in lxml:
lxml.etree.ParserTargetlxml.etree.SmartStr
They are intended as helpers when writing code, wrapped under if TYPE_CHECKING: .... Please consult their docstring in stub files for detail, or if you are using IDEs, the docstring might have been formatted nicely for reference.
Type annotations for lxml were initially included in typeshed, but as it was still incomplete, it was decided to be ripped out as a separate project. The code was extracted by Jelle Zijlstra and moved to lxml-stubs repository using git filter-branch.
types-lxml is a fork of lxml-stubs that strives for completeness, so that most people would at least find it usable; while the the original lxml-stubs aims to be stable.

