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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions stdlib/xml/etree/ElementTree.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class Element(Generic[_Tag]):
def __init__(self, tag: _Tag, attrib: dict[str, str] = {}, **extra: str) -> None: ...
def append(self, subelement: Element[Any], /) -> None: ...
def clear(self) -> None: ...
def extend(self, elements: Iterable[Element], /) -> None: ...
def extend(self, elements: Iterable[Element[Any]], /) -> None: ...
def find(self, path: str, namespaces: dict[str, str] | None = None) -> Element | None: ...
def findall(self, path: str, namespaces: dict[str, str] | None = None) -> list[Element]: ...
@overload
Expand All @@ -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__

def items(self) -> ItemsView[str, str]: ...
def iter(self, tag: str | None = None) -> Generator[Element, None, None]: ...
@overload
Expand All @@ -116,7 +116,7 @@ class Element(Generic[_Tag]):
def keys(self) -> dict_keys[str, str]: ...
# makeelement returns the type of self in Python impl, but not in C impl
def makeelement(self, tag: _OtherTag, attrib: dict[str, str], /) -> Element[_OtherTag]: ...
def remove(self, subelement: Element, /) -> None: ...
def remove(self, subelement: Element[Any], /) -> None: ...
def set(self, key: str, value: str, /) -> None: ...
def __copy__(self) -> Element[_Tag]: ... # returns the type of self in Python impl, but not in C impl
def __deepcopy__(self, memo: Any, /) -> Element: ... # Only exists in C impl
Expand All @@ -129,15 +129,15 @@ class Element(Generic[_Tag]):
# Doesn't actually exist at runtime, but instance of the class are indeed iterable due to __getitem__.
def __iter__(self) -> Iterator[Element]: ...
@overload
def __setitem__(self, key: SupportsIndex, value: Element, /) -> None: ...
def __setitem__(self, key: SupportsIndex, value: Element[Any], /) -> None: ...
@overload
def __setitem__(self, key: slice, value: Iterable[Element], /) -> None: ...
def __setitem__(self, key: slice, value: Iterable[Element[Any]], /) -> None: ...

# Doesn't really exist in earlier versions, where __len__ is called implicitly instead
@deprecated("Testing an element's truth value is deprecated.")
def __bool__(self) -> bool: ...

def SubElement(parent: Element, tag: str, attrib: dict[str, str] = ..., **extra: str) -> Element: ...
def SubElement(parent: Element[Any], tag: str, attrib: dict[str, str] = ..., **extra: str) -> Element: ...
def Comment(text: str | None = None) -> _CallableElement: ...
def ProcessingInstruction(target: str, text: str | None = None) -> _CallableElement: ...

Expand All @@ -156,7 +156,7 @@ class QName:
_Root = TypeVar("_Root", Element, Element | None, default=Element | None)

class ElementTree(Generic[_Root]):
def __init__(self, element: Element | None = None, file: _FileRead | None = None) -> None: ...
def __init__(self, element: Element[Any] | None = None, file: _FileRead | None = None) -> None: ...
def getroot(self) -> _Root: ...
def parse(self, source: _FileRead, parser: XMLParser | None = None) -> Element: ...
def iter(self, tag: str | None = None) -> Generator[Element, None, None]: ...
Expand Down Expand Up @@ -187,7 +187,7 @@ HTML_EMPTY: set[str]
def register_namespace(prefix: str, uri: str) -> None: ...
@overload
def tostring(
element: Element,
element: Element[Any],
encoding: None = None,
method: Literal["xml", "html", "text", "c14n"] | None = None,
*,
Expand All @@ -197,7 +197,7 @@ def tostring(
) -> bytes: ...
@overload
def tostring(
element: Element,
element: Element[Any],
encoding: Literal["unicode"],
method: Literal["xml", "html", "text", "c14n"] | None = None,
*,
Expand All @@ -207,7 +207,7 @@ def tostring(
) -> str: ...
@overload
def tostring(
element: Element,
element: Element[Any],
encoding: str,
method: Literal["xml", "html", "text", "c14n"] | None = None,
*,
Expand All @@ -217,7 +217,7 @@ def tostring(
) -> Any: ...
@overload
def tostringlist(
element: Element,
element: Element[Any],
encoding: None = None,
method: Literal["xml", "html", "text", "c14n"] | None = None,
*,
Expand All @@ -227,7 +227,7 @@ def tostringlist(
) -> list[bytes]: ...
@overload
def tostringlist(
element: Element,
element: Element[Any],
encoding: Literal["unicode"],
method: Literal["xml", "html", "text", "c14n"] | None = None,
*,
Expand All @@ -237,16 +237,16 @@ def tostringlist(
) -> list[str]: ...
@overload
def tostringlist(
element: Element,
element: Element[Any],
encoding: str,
method: Literal["xml", "html", "text", "c14n"] | None = None,
*,
xml_declaration: bool | None = None,
default_namespace: str | None = None,
short_empty_elements: bool = True,
) -> list[Any]: ...
def dump(elem: Element | ElementTree[Any]) -> None: ...
def indent(tree: Element | ElementTree[Any], space: str = " ", level: int = 0) -> None: ...
def dump(elem: Element[Any] | ElementTree[Any]) -> None: ...
def indent(tree: Element[Any] | ElementTree[Any], space: str = " ", level: int = 0) -> None: ...
def parse(source: _FileRead, parser: XMLParser[Any] | None = None) -> ElementTree[Element]: ...

# This class is defined inside the body of iterparse
Expand Down