Repository navigation
[xml.etree] Allow comparing plain Element.tag with ET.Comment/PI - #16735
Open
hrithiksaini99 wants to merge 2 commits into
Open
hrithiksaini99 wants to merge 2 commits into
hrithiksaini99 wants to merge 2 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
…findall/iter
mypy_primer showed widening the _Tag default to str | _ElementCallable
broke downstream (colour: tag is no longer str where str is expected).
Instead, traversal APIs return Element[Any], so el.tag is Any and
identity comparisons against ET.Comment/PI don't error, while
Element('x').tag stays str. Simpler and no downstream fallout.
hrithiksaini99
force-pushed
the
fix/14671-et-comment-tag
branch
from
November 7, 2026 08:55
3f6c37f to
7c6f19f
Compare
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: 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[Any] | None", variable has type "Element[str]") [assignment]
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ET.CommentandET.ProcessingInstructionare valid element tags at runtime (TreeBuilder(insert_comments=True)), but elements fromfindall('*')get tag typestr, soel.tag is ET.Commentraises comparison-overlap (since mypy 1.15 via #13349).Fix
Traversal APIs (
Element/ElementTree.find,findall,iter,iterfind,ElementTree.parse) now returnElement[Any], so.tagisAnyand identity comparisons against the comment/PI factories are allowed.Element.tagitself staysstrfor explicitly tagged elements (Element('x').tagstill infersstr) — no change to the generic default.Earlier iteration widened the TypeVar default to
str | _ElementCallable; mypy_primer showed that regressed downstream (colour expectedstr), so this narrower version keepstag: strintact.Verification
New regression identity checks in
stdlib/@tests/test_cases/check_xml.py.Fixes #14671.