Repository navigation
Description
This issue is part of the annotation effort on the standard libraries.
The linked PR highlights some points of attention that need a decision before finalizing the PR.
Use of Protocols
I've added Protocols to annotate the relevant etree classes / methods.
The reason for this is the nature of the xml.etree module and the support for lxml. While the xml.etree implementation is a reference interface that other tools should follow, no Protocols are available in the xml.etree module. While using the actual classes from the xml.etree module could be used for annotations, there is the risk that any type checks would fail at runtime if lxml is used.
In addition, both xml.etree and lxml are not particularly well annotated (if at all). The addition of Protocols with proper annotations should allow us control over any automatic argument conversion and / or documentation generation.
Whether or not to keep the Protocols (or parts and / or a number of them) and / or to move them to another module should be decided.
"bytes | str" usage
In many places, the "bytes | str" annotation is used and there's a number of unresolved pyright/mypy errors due to this. The core of this issue is that fact that most properties of an Element can be str or bytes (https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element):
The element name, attribute names, and attribute values can be either bytestrings or Unicode strings. tag is the element name. attrib is an optional dictionary, containing element attributes. extra contains additional attributes, given as keyword arguments
Now for a given keyword call / Element it is to be expected that either str or bytes are used, but there is no way to enforce this.
source: "Element | Unparsed" annotation
This is the most common annotation within this library and I'd like to centralize / alias it, but this does not seem possible without access to TypeAlias. It appears that a type alias using Union does not preserve / respect the order of the types and that causes problems with automatic argument conversion; if the str annotation (the last part of the Unparsed alias) is found before Element, the argument conversion will convert the Element to its string representation.
Activity
pekkaklarck commentedon Nov 19, 2025
I just reviewed the PR #5542.
Here are answers to the design decisions in the description:
bytesworks with attributes. You can usebyteswhen constructing elements e.g. with the XML method, but even thentag,attrib, etc. containstr, notbytes. See the PR review comments for examples.sourceis most likely impossible giving that we support both the standard ElementTree and lxml. Even if that would be possible, I'm worried the result would look really confusing to users. My earlier comment to the PR covers this in more detail and proposes how we could typesource. I strongly believe we should start withAnyto get other typing in and only then look at alternatives.Add annotations to XML (#5542)
Enhance XML typing.
Remove documentation about Boolean arguments