Skip to content

Use Element[Any] instead of Element in ElementTree#14198

Merged
srittau merged 7 commits intopython:mainfrom
MatthewMckee4:element-tree-element-any
Oct 8, 2025
Merged

Use Element[Any] instead of Element in ElementTree#14198
srittau merged 7 commits intopython:mainfrom
MatthewMckee4:element-tree-element-any

Conversation

@MatthewMckee4
Copy link
Copy Markdown
Contributor

Resolves #14036

@github-actions

This comment has been minimized.

@srittau
Copy link
Copy Markdown
Collaborator

srittau commented May 30, 2025

I haven't looked too deeply into this, but I don't think changing the Element instances from Element (which is Element[str]) to just Element[Any] is the right course of action. This will probably make previous type safe code unsafe. For example:

from xml.etree import ElementTree

tree = ElementTree.parse("foo.xml")
el = tree.find(".//*")
assert el is not None
print(type(el.tag))

el.tag is str in this case and it's typed as such. Now, it is Any.

This needs a more nuanced approach. I'm not sure what problems @JelleZijlstra alluded to in #14036, though.

@JelleZijlstra
Copy link
Copy Markdown
Member

The problem is primarily in argument types, not return types. For example, the tostring function is (still with this PR) marked as accepting only Element, which means only Element[str]. And because Element is invariant, that means any other Element is not allowed.

For return types, returning Element[str] might be fine though it should be verified that that's what these methods actually return.

I'm becoming skeptical that the way we're using TypeVar defaults has been a good idea; it can lead to usability issues quite easily.

@overload
def get(self, key: str, default: _T) -> str | _T: ...
def insert(self, index: int, subelement: Element, /) -> None: ...
def insert(self, index: int, subelement: Element[Any], /) -> None: ...
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because append accepts Element[Any] it seems appropriate to accept Element[Any] for insert, remove and __setitem__

@github-actions

This comment has been minimized.

@tungol
Copy link
Copy Markdown
Contributor

tungol commented Aug 20, 2025

I'm becoming skeptical that the way we're using TypeVar defaults has been a good idea; it can lead to usability issues quite easily.

As the person who added that part to the xml stubs, I believe my thought process was that tag is str in the normal use cases, so I didn't want downstream consumers to have to worry about it unless they were using one of the weirder use cases.

This particular issue came up because it seems like I didn't look at the uses of Element without parameters that existed in the stubs before my MR, which was definitely an oversight.

Copy link
Copy Markdown
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, and sorry for the delay.

@srittau
Copy link
Copy Markdown
Collaborator

srittau commented Oct 8, 2025

stubtest errors are unrelated.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Oct 8, 2025

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau merged commit 12e7d4e into python:main Oct 8, 2025
60 of 63 checks passed
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.

xml.etree.ElementTree stubs often use Element when they should use Element[Any]

4 participants