Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
MatthewMckee4 wants to merge6 commits intopython:main
base:main
Choose a base branch
Loading
fromMatthewMckee4:element-tree-element-any

Conversation

MatthewMckee4
Copy link

Resolves#14036

@github-actionsGitHub Actions

This comment has been minimized.

@srittau
Copy link
Collaborator

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

fromxml.etreeimportElementTreetree=ElementTree.parse("foo.xml")el=tree.find(".//*")assertelisnotNoneprint(type(el.tag))

el.tag isstr in this case and it's typed as such. Now, it isAny.

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, thetostring function is (still with this PR) marked as accepting onlyElement, which means onlyElement[str]. And because Element is invariant, that means any other Element is not allowed.

For return types, returningElement[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.

AlexWaygood reacted with thumbs up emoji

@@ -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: ...

Choose a reason for hiding this comment

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

becauseappend acceptsElement[Any] it seems appropriate to acceptElement[Any] forinsert,remove and__setitem__

@github-actionsGitHub Actions
Copy link
Contributor

Diff frommypy_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 freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

xml.etree.ElementTree stubs often use Element when they should useElement[Any]
3 participants
@MatthewMckee4@srittau@JelleZijlstra

[8]ページ先頭

©2009-2025 Movatter.jp