Skip to content

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

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

MatthewMckee4
Copy link

Resolves #14036

This comment has been minimized.

@srittau
Copy link
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
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.

@@ -105,7 +105,7 @@ class Element(Generic[_Tag]):
def get(self, key: str, default: None = None) -> str | None: ...
@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
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__

Copy link
Contributor

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

archinstall (https://github.com/archlinux/archinstall)
- ./archinstall/tui/curses_menu.py:710: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
- https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
- Please report a bug at https://github.com/python/mypy/issues
- version: 1.15.0
- ./archinstall/tui/curses_menu.py:710: : note: use --pdb to drop into pdb
- Traceback (most recent call last):
-   File "mypy/checkexpr.py", line 5903, in accept
-   File "mypy/nodes.py", line 1889, in accept
-   File "mypy/checkexpr.py", line 3284, in visit_member_expr
-   File "mypy/checkexpr.py", line 3309, in analyze_ordinary_member_access
-   File "mypy/checkmember.py", line 207, in analyze_member_access
-   File "mypy/checkmember.py", line 226, in _analyze_member_access
-   File "mypy/checkmember.py", line 344, in analyze_instance_member_access
-   File "mypy/meet.py", line 96, in meet_types
-   File "mypy/subtypes.py", line 223, in is_proper_subtype
-   File "mypy/subtypes.py", line 351, in _is_subtype
-   File "mypy/types.py", line 1469, in accept
-   File "mypy/subtypes.py", line 581, in visit_instance
-   File "mypy/subtypes.py", line 2095, in infer_class_variances
-   File "mypy/subtypes.py", line 2057, in infer_variance
-   File "mypy/subtypes.py", line 186, in is_subtype
-   File "mypy/subtypes.py", line 351, in _is_subtype
-   File "mypy/types.py", line 3033, in accept
-   File "mypy/subtypes.py", line 1079, in visit_partial_type
- RuntimeError: Partial type "<partial list[?]>" cannot be checked with "issubtype()"

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]
3 participants