Complete lxml external type annotation
Project description
Important note
-
Since
2025.03.04:BeautifulSoup4package is added as dependency to utilise its inline annotation, thus droppingtypes-beautifulsoup4dependency.- Fixes compatibility with older versions of type checkers, as well as
mypy1.14+.
-
Since
2025.02.24:- Incorporates
basedpyrighttype checker support (an enhanced fork ofpyright)
- Incorporates
-
Since
2024.11.08:pyrightusers will see warning if a single string is supplied where collection of string is expected (tuple,set,listetc). In terms of typing, a singlestritself is valid as aSequence, so type checkers normally would not raise alarm when usingstrin such function parameters, but can induce unexpected runtime behavior. See #64 for more info.mypydoes not support this feature (which (ab)uses@deprecatedwarning).- It is possible to verify release files indeed come from GitHub and not maliciously altered. See Release file attestation for detail.
-
Since
2024.08.07:- Each release will contain multiple builds; besides normal version, there is an alternative build suitable for multiple XML element subclass. Please check out relevant Installation section for detail (hint: many people don't need to bother with this). Also note that this release requires
mypy 1.11if one chooses to usemypyfor type checking.pyrightversion requirement (1.1.351) is not changed.
- Each release will contain multiple builds; besides normal version, there is an alternative build suitable for multiple XML element subclass. Please check out relevant Installation section for detail (hint: many people don't need to bother with this). Also note that this release requires
Introduction
This repository contains external type annotations for lxml. It can be used by type-checking tools to check code that uses lxml, or used within IDEs like VSCode 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:
-
lxml.etree -
lxml.html-
lxml.html.builder -
lxml.html.clean(already removed in lxml 5.2.0, this project will follow suite in future) -
lxml.html.diff -
lxml.html.html5parser -
lxml.html.soupparser
-
-
lxml.isoschematron -
lxml.objectify -
lxml.builder -
lxml.cssselect -
lxml.sax -
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 following type checkers:
basedpyright, version 1.4.0 or abovepyright, version 1.1.351 or abovemypy, version 1.10.0 or above
pyright and basedpyright are recommended for their greater flexibility and early adoption of newer type checking features. In the future, there is plan to bring even more type checker support.
Goal ③: Review and test suite
- All prior
lxml-stubscontributions are reviewed thoroughly, bringing coherency of annotation across the whole package - Perform runtime check, and compare against static type checker result; this guarantees annotations are indeed working in real world, not just within some cooked up test suite
- Existing static test suite already vastly expanded, and is under progress of migrating to runtime test
- 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, like:
uv pip install -U types-lxml # using uv
pip install -U types-lxml # using pip
In the unlikely case PyPI is down, one can directly download wheel from latest release in GitHub, and then perform installation as local file.
As convenience, it is possible to pull type checker directly with extras:
uv pip install -U types-lxml[pyright]
pip install -U types-lxml[mypy]
Choosing the build
Since 2024.08.07 release, there will be two versions of types-lxml. First one is the default one; if there's no problem using it, there's no need to switch.
The second version, types-lxml-multi-subclass, is intended for specific need, namely creation of multiple lxml element subclasses. For example:
graph TD;
etree.ElementBase-->MyBaseElement;
MyBaseElement-->MySubElement1;
MyBaseElement-->MySubElement2;
If a parsed or constructed element tree consists of single type of element nodes, it is safe to assume the children or parent of a node are of the same type too. But this assumption does not hold for multiple subclasses. Using diagram above as example, calling .iter() method from MyBaseElement node may produce element of any subelement or even MyBaseElement itself.
Therefore output type should be simply MyBaseElement only.
Such scenario is already in effect for lxml.html. <form> element (FormElement) is supposed to contain other form related tags like <input>, <select> etc. But we can't possibly pinpoint single subelement type, so <form> children can only possibly be of type HtmlElement. The multiple subelement scenario is already hardcoded for HtmlElement and ObjectifiedElement within this annotation package, but users may choose to have their own overridden element subclasses (inherit from ElementBase) too.
The 2 paradigms can't coexist within a single type annotation package. See bug #51 that illustrated why multiple build is necessary.
Remember that anybody can only choose one of the 2 builds. It is impossible to install both, as pip just arbitrarily overwrite conflicting files with one another. If in doubt, removing existing package first, then install the one you needed.
Release file attestation
Since 2024.11.08 users can download types-lxml release files and verify that they indeed do originate from GitHub. For those haven't heard of it, this is sort of like gnupg or minisign signatures, but with GitHub backed infrastructure.
After downloading release wheel file (say pip download types-lxml, or browser access to PyPI directly), one can use GitHub cli to verify it comes from this GitHub repository without being altered:
gh at verify types_lxml-2024.11.8-py3-none-any.whl --repo abelcheung/types-lxml
Should generate following result:
Loaded digest sha256:4b4fa7f9e2f1d5f58b98ac9852a75927e4e0f69363249f9cebc78db095c046e0 for file://types_lxml-2024.11.8-py3-none-any.whl
Loaded 1 attestation from GitHub API
✓ Verification succeeded!
sha256:4b4fa7f9e2f1d5f58b98ac9852a75927e4e0f69363249f9cebc78db095c046e0 was attested by:
REPO PREDICATE_TYPE WORKFLOW
abelcheung/types-lxml https://slsa.dev/provenance/v1 .github/workflows/release.yml@refs/tags/2024.11.08
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file types_lxml_multi_subclass-2025.3.4.tar.gz.
File metadata
- Download URL: types_lxml_multi_subclass-2025.3.4.tar.gz
- Upload date:
- Size: 141.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39f5865a43652183be94a37b0f0591fe890399daca44f6fcfc37d6dd8265ebf0
|
|
| MD5 |
9c98e4e21208579e55c780a61866355e
|
|
| BLAKE2b-256 |
60dda2c3097c0cd6def53ac301c8d625a63dfff1e1f28e3e40760a1d5df9242a
|
Provenance
The following attestation bundles were made for types_lxml_multi_subclass-2025.3.4.tar.gz:
Publisher:
release.yml on abelcheung/types-lxml
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
types_lxml_multi_subclass-2025.3.4.tar.gz -
Subject digest:
39f5865a43652183be94a37b0f0591fe890399daca44f6fcfc37d6dd8265ebf0 - Sigstore transparency entry: 176587430
- Sigstore integration time:
-
Permalink:
abelcheung/types-lxml@2c73e690a97cb16a78e3e10a1e5a4fd25725ecaa -
Branch / Tag:
refs/tags/2025.03.04 - Owner: https://github.com/abelcheung
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2c73e690a97cb16a78e3e10a1e5a4fd25725ecaa -
Trigger Event:
push
-
Statement type:
File details
Details for the file types_lxml_multi_subclass-2025.3.4-py3-none-any.whl.
File metadata
- Download URL: types_lxml_multi_subclass-2025.3.4-py3-none-any.whl
- Upload date:
- Size: 91.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5989dffe4181218e1ecbb02e529279ca537d224d9faf1427fea4b1f54f0feb9a
|
|
| MD5 |
5e518aaec2d3442b71d9cc58ea9f9292
|
|
| BLAKE2b-256 |
a5da7b40220783ba583a8081497213320fd31752d639ae0c4975191ffdd020d8
|
Provenance
The following attestation bundles were made for types_lxml_multi_subclass-2025.3.4-py3-none-any.whl:
Publisher:
release.yml on abelcheung/types-lxml
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
types_lxml_multi_subclass-2025.3.4-py3-none-any.whl -
Subject digest:
5989dffe4181218e1ecbb02e529279ca537d224d9faf1427fea4b1f54f0feb9a - Sigstore transparency entry: 176587436
- Sigstore integration time:
-
Permalink:
abelcheung/types-lxml@2c73e690a97cb16a78e3e10a1e5a4fd25725ecaa -
Branch / Tag:
refs/tags/2025.03.04 - Owner: https://github.com/abelcheung
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2c73e690a97cb16a78e3e10a1e5a4fd25725ecaa -
Trigger Event:
push
-
Statement type: