Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pdewacht
Copy link

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.
Copy link
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
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
Copy link
Collaborator

srittau commented Apr 29, 2024

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.

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.

None yet

2 participants