Registry / type-stubs / types-lxml

types-lxml

JSON →
library2026.2.16pypypi✓ verified 28d ago

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-lxml
INSTALL
IMPORT
SIG · TYPES-LXML
T
types-lxml
type-stubspythonv2026.2.16
Install
2.0s avg
Import
—
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v2026.2.16 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 20MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 2.0s · import 0.000s · 21MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

etree
✓ from lxml import etree
✗ from lxml-stubs import etree

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.

from lxml import etree # Example XML string xml_string: str = '<root><item id="1">Apple</item><item id="2">Banana</item></root>' # Parse the XML string root: etree._Element = etree.fromstring(xml_string) # Find an element by tag name item: etree._Element | None = root.find('item') if item is not None: print(f"First item: {item.text}") # Find all elements with a specific tag all_items: list[etree._Element] = root.findall('item') for i, element in enumerate(all_items): item_id: str | None = element.get('id') print(f"Item {i+1} (ID: {item_id}): {element.text}") # Create a new element and append it new_element: etree._Element = etree.SubElement(root, 'item', id='3') new_element.text = 'Cherry' # Print the modified XML print("\nModified XML:") print(etree.tostring(root, pretty_print=True).decode())
Debug
Known issues
breakingPython 3.8 support was dropped since the 2025.11.25 release. Ensure your project uses Python 3.9 or newer.
fix
Upgrade your Python environment to 3.9 or later.
affects: >=2025.11.25
gotchaPyright is generally the preferred type checker for lxml code over MyPy. lxml's attribute normalization can be too restrictive for MyPy, leading to potential false positives or difficulties.
fix
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.
affects: all
breakingSince the 2025.08.25 release, `types-lxml` officially supports `lxml` versions 6.0 and 5.4, and no longer tests against `lxml` 4.9.
fix
Ensure your `lxml` dependency is updated to a supported version (e.g., 5.4 or 6.0+).
affects: >=2025.08.25
gotchaWhen using specific lxml types, particularly internal ones like `_Element` or `_ElementTree` in type annotations in your own code, it's best practice to import them from `lxml` directly, not `types_lxml`. Also, use `from __future__ import annotations` or quote the type hints to avoid forward reference issues.
fix
Example: `from __future__ import annotations` then `from lxml.etree import _Element` or `def my_func(element: "_Element"): ...`.
affects: all
gotchaSince the 2024.08.07 release, there are two versions of `types-lxml`: the default and `types-lxml-multi-subclass`. The latter is intended for specific needs involving the creation of multiple lxml element subclasses.
fix
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.
affects: >=2024.08.07
Upgrade
Version history
2026.2.16latest on PyPI · released Feb 17, 2026
Audit
Dependencies
lxmlrequiredProvides type stubs for this library; necessary for runtime execution of code that types-lxml annotates.
BeautifulSoup4requiredAdded as a dependency to utilize its inline annotation within types-lxml itself.
Agent activity
41 hits · last 30 days
node
36
OpenAI (training)
1
Resources
types-lxml — pip install types-lxml · libregistry