Skip to content

Conversation

tungol
Copy link
Contributor

@tungol tungol commented Sep 5, 2025

This is an alternative to #14674

While investigating, I also noticed that QName classes can be the value of both Element.tag and Eelement.text, as well in the result of Element.items() (and thus as keys and values in Element.attrib). The test suite in cpython also includes explict tests for assigning None to Element.tag as a form of suppressing it, so I added that into the annotation as well.

With the broad matrix of possible types for Element.tag, Element.text, and Element.attrib, it does seem like typevars are probably useful? But on the other hand there's a cleanness to this approach, and there's tricky correlations between the three attributes which can't be expressed very well in the type system.

This is an alternative to python#14674

While investigating, I also noticed that QName classes can be the value
of both Element.tag and Eelement.text, as well in the result of
Element.items() (and thus as keys and values in Element.attrib).
The test suite in cpython also includes explict tests for assigning
None to Element.tag as a form of suppressing it, so I added that into
the annotation as well.
@tungol tungol marked this pull request as draft September 5, 2025 12:12
Copy link
Contributor

github-actions bot commented Sep 5, 2025

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

colour (https://github.com/colour-science/colour)
+ colour/io/tm2714.py:1693: error: Argument 2 to "match" has incompatible type "str | QName | Callable[..., Element] | None"; expected "str"  [arg-type]
- colour/io/tm2714.py:1743: error: Argument 1 to "as_float_array" has incompatible type "list[str | None]"; expected "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]"  [arg-type]
+ colour/io/tm2714.py:1742: error: Argument 1 to "as_float_array" has incompatible type "list[str | QName]"; expected "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]"  [arg-type]
+ colour/io/tm2714.py:1743: error: Argument 1 to "as_float_array" has incompatible type "list[str | QName | None]"; expected "Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]"  [arg-type]

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/utils/rendering.py:104: error: Unsupported left operand type for + ("QName")  [operator]
+ mkdocs/utils/rendering.py:104: note: Left operand is of type "str | QName"
+ 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]

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/ext/graphviz.py: note: In function "fix_svg_relative_paths":
+ sphinx/ext/graphviz.py:252:63: error: Argument 1 to "urlsplit" has incompatible type "str | QName"; expected "str"  [arg-type]

materialize (https://github.com/MaterializeInc/materialize)
- misc/python/materialize/cli/gen-chroma-syntax.py:56: error: Incompatible types in assignment (expression has type "Element[str] | None", variable has type "Element[str]")  [assignment]
+ misc/python/materialize/cli/gen-chroma-syntax.py:56: error: Incompatible types in assignment (expression has type "Element | None", variable has type "Element")  [assignment]
- misc/python/materialize/cli/gen-chroma-syntax.py:59: error: Incompatible types in assignment (expression has type "Sequence[str]", variable has type "str | None")  [assignment]
+ misc/python/materialize/cli/gen-chroma-syntax.py:59: error: Incompatible types in assignment (expression has type "Sequence[str]", variable has type "str | QName | None")  [assignment]

boostedblob (https://github.com/hauntsaninja/boostedblob)
+ boostedblob/azure_auth.py:287: error: Incompatible types in assignment (expression has type "Union[str, QName, None]", variable has type "Optional[str]")  [assignment]
+ boostedblob/request.py:293: error: Incompatible types in assignment (expression has type "Union[str, QName]", target has type "str")  [assignment]
+ boostedblob/listing.py:410: error: Argument 1 to "AzureStat" has incompatible type "dict[Union[str, QName, Callable[..., Element], Any, None], Union[str, QName, Any, None]]"; expected "Mapping[str, Any]"  [arg-type]

zulip (https://github.com/zulip/zulip)
+ zerver/lib/markdown/static.py:31: error: Item "QName" of "str | QName" has no attribute "startswith"  [union-attr]
+ zerver/lib/markdown/static.py:32: error: Item "QName" of "str | QName" has no attribute "removeprefix"  [union-attr]
+ zerver/lib/markdown/__init__.py:538: error: Argument 1 to "is_static_or_current_realm_url" has incompatible type "str | QName"; expected "str"  [arg-type]
+ zerver/lib/markdown/__init__.py:541: error: Argument 1 to "get_camo_url" has incompatible type "str | QName"; expected "str"  [arg-type]
+ zerver/lib/markdown/__init__.py:568: error: Argument 1 to "is_static_or_current_realm_url" has incompatible type "str | QName"; expected "str"  [arg-type]
+ zerver/lib/markdown/__init__.py:575: error: Argument 1 to "get_camo_url" has incompatible type "str | QName"; expected "str"  [arg-type]
+ zerver/lib/markdown/__init__.py:911: error: Incompatible return value type (got "tuple[str | QName, str | QName | None]", expected "tuple[str, str | None] | None")  [return-value]
+ zerver/lib/markdown/__init__.py:1022: error: Item "QName" of "str | QName" has no attribute "split"  [union-attr]
+ zerver/lib/markdown/__init__.py:1761: error: Unsupported left operand type for + ("QName")  [operator]
+ zerver/lib/markdown/__init__.py:1761: note: Left operand is of type "str | QName"
+ zerver/lib/markdown/__init__.py:2012: error: Item "QName" of "str | QName" has no attribute "strip"  [union-attr]
+ zerver/lib/markdown/__init__.py:2024: error: Item "QName" of "str | QName" has no attribute "strip"  [union-attr]
+ zerver/lib/markdown/__init__.py:2070: error: Item "QName" of "str | QName" has no attribute "strip"  [union-attr]
+ zerver/lib/markdown/nested_code_blocks.py:38: error: Incompatible return value type (got "tuple[str | QName | Callable[..., Element], str | QName | None]", expected "tuple[str, str | None] | None")  [return-value]
+ zerver/webhooks/pivotal/view.py:31: error: Incompatible return value type (got "str | QName", expected "str")  [return-value]
+ zerver/webhooks/pivotal/view.py:45: error: Argument 1 to "match" of "Pattern" has incompatible type "str | QName"; expected "str"  [arg-type]
+ zerver/webhooks/pivotal/view.py:54: error: Unsupported left operand type for + ("QName")  [operator]
+ zerver/webhooks/pivotal/view.py:54: note: Left operand is of type "str | QName"
+ zerver/webhooks/pivotal/view.py:57: error: Unsupported left operand type for + ("QName")  [operator]
+ zerver/webhooks/pivotal/view.py:57: note: Left operand is of type "str | QName"

mkosi (https://github.com/systemd/mkosi)
+ mkosi/distributions/opensuse.py:266:27: error: Item "QName" of "Union[str, QName]" has no attribute "startswith"  [union-attr]
+ mkosi/distributions/opensuse.py:267:22: error: Item "QName" of "Union[str, QName]" has no attribute "partition"  [union-attr]

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

hydpy (https://github.com/hydpy-dev/hydpy)
+ hydpy/exe/xmltools.py:195: error: Item "QName" of "str | QName | Callable[..., Element] | None" has no attribute "rsplit"  [union-attr]
+ hydpy/exe/xmltools.py:195: error: Item "function" of "str | QName | Callable[..., Element] | None" has no attribute "rsplit"  [union-attr]
+ hydpy/exe/xmltools.py:195: error: Item "None" of "str | QName | Callable[..., Element] | None" has no attribute "rsplit"  [union-attr]
+ hydpy/exe/xmltools.py:207: error: Item "QName" of "str | QName" has no attribute "split"  [union-attr]
+ hydpy/exe/xmltools.py:387: error: Argument 1 to "strip" has incompatible type "str | QName | Callable[..., Element] | None"; expected "str"  [arg-type]
+ hydpy/exe/xmltools.py:423: error: Argument 1 to "strip" has incompatible type "str | QName | Callable[..., Element] | None"; expected "str"  [arg-type]
+ hydpy/exe/xmltools.py:543: error: Unsupported right operand type for in ("str | QName | Callable[..., Element] | None")  [operator]
+ hydpy/exe/xmltools.py:623: error: Argument 1 to "strip" has incompatible type "str | QName | Callable[..., Element] | None"; expected "str"  [arg-type]
+ hydpy/exe/xmltools.py:625: error: Argument 1 to "strip" has incompatible type "str | QName | Callable[..., Element] | None"; expected "str"  [arg-type]
+ hydpy/exe/xmltools.py:651: error: Incompatible types in assignment (expression has type "tuple[str | QName, str | QName, str | QName]", variable has type "Timegrids | Timegrid | tuple[datetime | str | Date, datetime | str | Date, timedelta | str | Period]")  [assignment]
+ hydpy/exe/xmltools.py:730: error: Item "QName" of "str | QName" has no attribute "split"  [union-attr]
+ hydpy/exe/xmltools.py:739: error: Argument 1 to "Selection" has incompatible type "str | QName"; expected "str"  [arg-type]
+ hydpy/exe/xmltools.py:889: error: Argument "text" to "XMLNetworkUserDefined" has incompatible type "str | QName | None"; expected "str | None"  [arg-type]
+ hydpy/exe/xmltools.py:907: error: Argument "text" to "XMLControlUserDefined" has incompatible type "str | QName | None"; expected "str | None"  [arg-type]
+ hydpy/exe/xmltools.py:1383: error: Incompatible return value type (got "str | QName", expected "str")  [return-value]
+ hydpy/exe/xmltools.py:1465: error: Incompatible types in assignment (expression has type "str | QName", variable has type "str | None")  [assignment]
+ hydpy/exe/xmltools.py:1475: error: Argument 2 to "value2bool" has incompatible type "str | QName"; expected "str | int"  [arg-type]
+ hydpy/exe/xmltools.py:1515: error: Argument 1 to "strip" has incompatible type "str | QName | Callable[..., Element] | None"; expected "str"  [arg-type]
+ hydpy/exe/xmltools.py:1519: error: Argument 1 to "strip" has incompatible type "str | QName | Callable[..., Element] | None"; expected "str"  [arg-type]
+ hydpy/exe/xmltools.py:1521: error: Argument 1 to "strip" has incompatible type "str | QName | Callable[..., Element] | None"; expected "str"  [arg-type]
+ hydpy/exe/xmltools.py:1544: error: Argument 1 to "strip" has incompatible type "str | QName | Callable[..., Element] | None"; expected "str"  [arg-type]
+ hydpy/exe/xmltools.py:2052: error: Argument 1 to "strip" has incompatible type "str | QName | Callable[..., Element] | None"; expected "str"  [arg-type]
+ hydpy/exe/xmltools.py:2059: error: Argument 1 to "strip" has incompatible type "str | QName | Callable[..., Element] | None"; expected "str"  [arg-type]
+ hydpy/exe/xmltools.py:2366: error: Argument 1 to "strip" has incompatible type "str | QName | Callable[..., Element] | None"; expected "str"  [arg-type]
+ hydpy/exe/xmltools.py:2375: error: Argument "keyword" to "SetItem" has incompatible type "str | QName | None"; expected "str | None"  [arg-type]
+ hydpy/exe/xmltools.py:2383: error: Item "QName" of "str | QName" has no attribute "split"  [union-attr]

meson (https://github.com/mesonbuild/meson)
+ mesonbuild/modules/_qt.py:319:35: error: Argument 1 to "append" of "list" has incompatible type "str | QName"; expected "str"  [arg-type]
+ mesonbuild/mtest.py:903:49: error: Argument 1 to "int" has incompatible type "str | QName"; expected "str | Buffer | SupportsInt | SupportsIndex | SupportsTrunc"  [arg-type]
+ mesonbuild/mtest.py:909:51: error: Argument 1 to "int" has incompatible type "str | QName"; expected "str | Buffer | SupportsInt | SupportsIndex | SupportsTrunc"  [arg-type]
+ mesonbuild/mtest.py:913:51: error: Argument 1 to "int" has incompatible type "str | QName"; expected "str | Buffer | SupportsInt | SupportsIndex | SupportsTrunc"  [arg-type]
+ mesonbuild/mtest.py:916:50: error: Argument 1 to "int" has incompatible type "str | QName"; expected "str | Buffer | SupportsInt | SupportsIndex | SupportsTrunc"  [arg-type]
+ mesonbuild/mtest.py:919:52: error: Argument 1 to "int" has incompatible type "str | QName"; expected "str | Buffer | SupportsInt | SupportsIndex | SupportsTrunc"  [arg-type]
+ mesonbuild/mtest.py:923:52: error: Argument 1 to "int" has incompatible type "str | QName"; expected "str | Buffer | SupportsInt | SupportsIndex | SupportsTrunc"  [arg-type]
+ mesonbuild/mtest.py:927:50: error: Argument 1 to "int" has incompatible type "str | QName"; expected "str | Buffer | SupportsInt | SupportsIndex | SupportsTrunc"  [arg-type]
+ mesonbuild/mtest.py:931:50: error: Argument 1 to "int" has incompatible type "str | QName"; expected "str | Buffer | SupportsInt | SupportsIndex | SupportsTrunc"  [arg-type]
+ mesonbuild/mtest.py:945:50: error: Argument 1 to "int" has incompatible type "str | QName"; expected "str | Buffer | SupportsInt | SupportsIndex | SupportsTrunc"  [arg-type]

discord.py (https://github.com/Rapptz/discord.py)
- discord/ext/commands/hybrid.py:508: error: Overlap between argument names and ** TypedDict items: "description", "name"  [misc]
+ discord/ext/commands/hybrid.py:508: error: Overlap between argument names and ** TypedDict items: "name", "description"  [misc]
- discord/ext/commands/hybrid.py:629: error: Overlap between argument names and ** TypedDict items: "description", "name"  [misc]
+ discord/ext/commands/hybrid.py:629: error: Overlap between argument names and ** TypedDict items: "name", "description"  [misc]
- discord/ext/commands/hybrid.py:834: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
+ discord/ext/commands/hybrid.py:834: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
- discord/ext/commands/hybrid.py:858: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
+ discord/ext/commands/hybrid.py:858: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
- discord/ext/commands/hybrid.py:883: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
+ discord/ext/commands/hybrid.py:883: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
- discord/ext/commands/hybrid.py:935: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
+ discord/ext/commands/hybrid.py:935: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
- discord/ext/commands/bot.py:290: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
+ discord/ext/commands/bot.py:290: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
- discord/ext/commands/bot.py:314: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
+ discord/ext/commands/bot.py:314: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]

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.

1 participant