Skip to content

stdlib xml.etree: tags, attributes and attribute values can be QName instances - #11841

Open
pdewacht wants to merge 1 commit into
python:mainfrom
pdewacht:qname
Open

pdewacht wants to merge 1 commit into
python:mainfrom
pdewacht:qname

Conversation

@pdewacht

Copy link
Copy Markdown

See e.g. the ElementTree unit tests:
https://github.com/python/cpython/blob/3.12/Lib/test/test_xml_etree.py#L1195

I guess this is fairly obscure functionality. The documentation does describe the QName class, but it doesn't say much more than the bare minimum.

…instances

See e.g. the ElementTree unit tests:
https://github.com/python/cpython/blob/3.12/Lib/test/test_xml_etree.py#L1195

I guess this is fairly obscure functionality. The documentation does
describe the QName class, but it doesn't say much more than the bare
minimum.
@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

vision (https://github.com/pytorch/vision)
+ torchvision/datasets/voc.py:219: error: Dict entry 0 has incompatible type "str | QName": "dict[Any, Any]"; expected "str": "Any"  [dict-item]
+ torchvision/datasets/voc.py:223: error: Invalid index type "str | QName" for "dict[str, Any]"; expected type "str"  [index]

mkdocs (https://github.com/mkdocs/mkdocs)
+ mkdocs/utils/rendering.py:72: error: Item "QName" of "str | QName" has no attribute "startswith"  [union-attr]
+ mkdocs/utils/rendering.py:85: error: Incompatible return value type (got "str | QName", expected "str | None")  [return-value]
+ mkdocs/structure/pages.py:337: error: Argument 1 has incompatible type "str | QName"; expected "str"  [arg-type]
+ mkdocs/structure/pages.py:340: error: Argument 1 has incompatible type "str | QName"; expected "str"  [arg-type]
+ mkdocs/structure/pages.py:370: error: Argument 1 to "path_to_url" of "_RelativePathTreeprocessor" has incompatible type "str | QName"; expected "str"  [arg-type]

pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/junitxml.py:103: error: Argument 1 to "add_stats" of "LogXML" has incompatible type "str | QName"; expected "str"  [arg-type]

boostedblob (https://github.com/hauntsaninja/boostedblob)
+ boostedblob/listing.py:410: error: Argument 1 to "AzureStat" has incompatible type "Dict[Union[str, QName, Any], Union[str, None, Any]]"; expected "Mapping[str, Any]"  [arg-type]

@pdewacht

Copy link
Copy Markdown
Author

This apparently badly breaks the openpyxl typings. I'm afraid I'm not familiar with that library and I don't understand what openpyxl/xml/_functions_overloads.pyi is doing...

@srittau

srittau commented Apr 29, 2024

Copy link
Copy Markdown
Collaborator

The main problems seems to be the Element.tag field. I assume that most code just consumed elements created when reading an XML file, where tag will be a str. This code would break. One approach to try is making Element generic over the tag type, using a default of str.

venthur added a commit to venthur/python-debianbts that referenced this pull request Jun 11, 2024
can be undone once python/typeshed#11841 is merged
@Avasam

Avasam commented Jun 1, 2025

Copy link
Copy Markdown
Collaborator

Going back deep into openpyxl implementation. The tag can actually be a str OR a callable.
The only place the tag attribute isn't isn't for a comparison (equal, contains, itemgetter, ...) is: https://foss.heptapod.net/openpyxl/openpyxl/-/blob/700cafe4928c9c5e7ffc998f5d911e475ac8c37f/openpyxl/descriptors/serialisable.py#L76
And that's after being passed through the localname function: https://foss.heptapod.net/openpyxl/openpyxl/-/blob/700cafe4928c9c5e7ffc998f5d911e475ac8c37f/openpyxl/xml/functions.py#L77-81

Not that it really helps you here since QName isn't callable.

Looking at the primer results, as @srittau said, there's a lot of code out there that seems to assume some of the attributes you change can only be str.

@JelleZijlstra

Copy link
Copy Markdown
Member

This is very out of date; we've since made Element generic in another PR.

Avasam added a commit that referenced this pull request Jun 1, 2025

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants