From 2c63108897e33af120c4e93d2fa85bc627c25da0 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 27 Jul 2025 13:46:59 +0200 Subject: [PATCH 1/2] Clean up callable element types in ElementTree Remove _CallableElement and make _ElementCallable recursive --- stdlib/xml/etree/ElementTree.pyi | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/stdlib/xml/etree/ElementTree.pyi b/stdlib/xml/etree/ElementTree.pyi index 4c55a1a7452e..d58749e3b564 100644 --- a/stdlib/xml/etree/ElementTree.pyi +++ b/stdlib/xml/etree/ElementTree.pyi @@ -78,10 +78,8 @@ def canonicalize( ) -> None: ... # The tag for Element can be set to the Comment or ProcessingInstruction -# functions defined in this module. _ElementCallable could be a recursive -# type, but defining it that way uncovered a bug in pytype. -_ElementCallable: TypeAlias = Callable[..., Element[Any]] -_CallableElement: TypeAlias = Element[_ElementCallable] +# functions defined in this module. +_ElementCallable: TypeAlias = Callable[..., Element[_ElementCallable]] _Tag = TypeVar("_Tag", default=str, bound=str | _ElementCallable) _OtherTag = TypeVar("_OtherTag", default=str, bound=str | _ElementCallable) @@ -138,8 +136,8 @@ class Element(Generic[_Tag]): def __bool__(self) -> bool: ... def SubElement(parent: Element, 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: ... +def Comment(text: str | None = None) -> Element[Comment]: ... +def ProcessingInstruction(target: str, text: str | None = None) -> Element[ProcessingInstruction]: ... PI = ProcessingInstruction From 4da1592bff8fb2c09dae8d04b620a09608c49ace Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 27 Jul 2025 13:53:46 +0200 Subject: [PATCH 2/2] Fix --- stdlib/xml/etree/ElementTree.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/xml/etree/ElementTree.pyi b/stdlib/xml/etree/ElementTree.pyi index d58749e3b564..67cbf5b2e86a 100644 --- a/stdlib/xml/etree/ElementTree.pyi +++ b/stdlib/xml/etree/ElementTree.pyi @@ -136,8 +136,8 @@ class Element(Generic[_Tag]): def __bool__(self) -> bool: ... def SubElement(parent: Element, tag: str, attrib: dict[str, str] = ..., **extra: str) -> Element: ... -def Comment(text: str | None = None) -> Element[Comment]: ... -def ProcessingInstruction(target: str, text: str | None = None) -> Element[ProcessingInstruction]: ... +def Comment(text: str | None = None) -> Element[_ElementCallable]: ... +def ProcessingInstruction(target: str, text: str | None = None) -> Element[_ElementCallable]: ... PI = ProcessingInstruction