typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

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

Open pdewacht opened this issue 1 year ago • 5 comments

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.

pdewacht avatar Apr 28 '24 20:04 pdewacht

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]

github-actions[bot] avatar Apr 28 '24 21:04 github-actions[bot]

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...

pdewacht avatar Apr 28 '24 21:04 pdewacht

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.

srittau avatar Apr 29 '24 12:04 srittau