Skip to content

Add annotations to XML.py #5541

Closed
Closed

Description

@robinmackaij

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

  1. pekkaklarck commented on Nov 19, 2025

    @pekkaklarck
    Member

    I just reviewed the PR #5542.

    Here are answers to the design decisions in the description:

    1. I don't think we should use protocols or anything complicated like that. It's not our business to try to come up with a protocol that matches both the standard ElementTree and lxml.
    2. I don't know where you got the impression bytes works with attributes. You can use bytes when constructing elements e.g. with the XML method, but even then tag, attrib, etc. contain str, not bytes. See the PR review comments for examples.
    3. Trying to precisely type source is 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 type source. I strongly believe we should start with Any to get other typing in and only then look at alternatives.
  2. added 3 commits that reference this issue on Nov 27, 2025
    5474d2c
    af6666a
    c02d1c7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions