-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
base: main
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
I haven't looked too deeply into this, but I don't think changing the from xml.etree import ElementTree
tree = ElementTree.parse("foo.xml")
el = tree.find(".//*")
assert el is not None
print(type(el.tag))
This needs a more nuanced approach. I'm not sure what problems @JelleZijlstra alluded to in #14036, though. |
The problem is primarily in argument types, not return types. For example, the For return types, returning 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: ... |
There was a problem hiding this comment.
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__
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()"
|
Resolves #14036