types-lxml, version 2026.2.16, provides comprehensive external type annotations (stubs) for the `lxml` library, enabling static type checking with tools like MyPy and Pyright, and improving IDE support. It is actively maintained with frequent updates, often releasing multiple versions per month, and is a more complete fork of the original `lxml-stubs` project. It aims to cover major lxml submodules like `lxml.etree`, `lxml.html`, and `lxml.objectify` for Python 3.9 and newer.
pip install types-lxmlVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic XML parsing, element finding, and modification using `lxml.etree`. `types-lxml` enhances this experience by providing accurate type hints for `lxml` objects like `_Element` and `_ElementTree`, which are consumed by static type checkers and IDEs.
Upgrade your Python environment to 3.9 or later.
Consider using Pyright for type checking projects that heavily rely on lxml. If using MyPy, be aware of potential strictness around lxml's dynamic behaviors.
Ensure your `lxml` dependency is updated to a supported version (e.g., 5.4 or 6.0+).
Example: `from __future__ import annotations` then `from lxml.etree import _Element` or `def my_func(element: "_Element"): ...`.
Most users should stick to the default `types-lxml`. Only install `types-lxml-multi-subclass` if you have specific advanced subclassing requirements for lxml elements.