From 2f8196d82f21b14a3cf89dfcffac39741ca783aa Mon Sep 17 00:00:00 2001 From: Sumit Date: Thu, 12 Nov 2026 11:31:16 +0530 Subject: [PATCH] [defusedxml] Complete annotations in defusedxml.lxml --- pyrightconfig.stricter.json | 1 - stubs/defusedxml/defusedxml/lxml.pyi | 102 ++++++++++++++++++++------- 2 files changed, 76 insertions(+), 27 deletions(-) diff --git a/pyrightconfig.stricter.json b/pyrightconfig.stricter.json index 2b724712154b..f7a13ca52e86 100644 --- a/pyrightconfig.stricter.json +++ b/pyrightconfig.stricter.json @@ -34,7 +34,6 @@ "stubs/cffi", "stubs/colorful", "stubs/dateparser", - "stubs/defusedxml", "stubs/docker", "stubs/docutils", "stubs/Flask-SocketIO", diff --git a/stubs/defusedxml/defusedxml/lxml.pyi b/stubs/defusedxml/defusedxml/lxml.pyi index 0ff53d95ca3b..a4e0ef0b7f57 100644 --- a/stubs/defusedxml/defusedxml/lxml.pyi +++ b/stubs/defusedxml/defusedxml/lxml.pyi @@ -1,27 +1,65 @@ import threading -from _typeshed import Incomplete -from typing import Final, Literal, type_check_only - -# Not bothering with types here as lxml support is supposed to be dropped in a future version of defusedxml +from _typeshed import Incomplete, ReadableBuffer, StrOrBytesPath, SupportsRead, Unused +from collections.abc import Iterable, Iterator +from typing import Any, Final, Literal, NoReturn, TypeAlias, overload, type_check_only LXML3: bool __origin__: Final = "lxml.etree" +# lxml lacks typeshed stubs +_LxmlElement: TypeAlias = Any +_LxmlElementTree: TypeAlias = Any +_LxmlXMLParser: TypeAlias = Any +_LxmlTag: TypeAlias = Any + +@overload +def tostring( + element_or_tree: _LxmlElement | _LxmlElementTree, + *, + encoding: None = None, + method: Literal["xml", "html", "text", "c14n", "c14n2"] = "xml", + xml_declaration: bool | None = None, + pretty_print: bool = False, + with_tail: bool = True, + standalone: bool | None = None, + doctype: str | bytes | None = None, + exclusive: bool = False, + inclusive_ns_prefixes: Iterable[str | bytes] | None = None, + with_comments: bool = True, + strip_text: bool = False, +) -> bytes: ... +@overload +def tostring( + element_or_tree: _LxmlElement | _LxmlElementTree, + *, + encoding: Literal["unicode"] | type[str], + method: Literal["xml", "html", "text", "c14n", "c14n2"] = "xml", + xml_declaration: bool | None = None, + pretty_print: bool = False, + with_tail: bool = True, + standalone: bool | None = None, + doctype: str | bytes | None = None, + exclusive: bool = False, + inclusive_ns_prefixes: Iterable[str | bytes] | None = None, + with_comments: bool = True, + strip_text: bool = False, +) -> str: ... +@overload def tostring( - element_or_tree, + element_or_tree: _LxmlElement | _LxmlElementTree, *, - encoding: str | None = None, + encoding: str | bytes, method: Literal["xml", "html", "text", "c14n", "c14n2"] = "xml", xml_declaration: bool | None = None, pretty_print: bool = False, with_tail: bool = True, standalone: bool | None = None, - doctype=None, + doctype: str | bytes | None = None, exclusive: bool = False, - inclusive_ns_prefixes=None, + inclusive_ns_prefixes: Iterable[str | bytes] | None = None, with_comments: bool = True, strip_text: bool = False, -): ... +) -> Any: ... # Should be imported from lxml.etree.ElementBase, but lxml lacks types @type_check_only @@ -30,26 +68,38 @@ class _ElementBase: ... class RestrictedElement(_ElementBase): __slots__ = () blacklist: Incomplete - def __iter__(self): ... - def iterchildren(self, tag=None, reversed: bool = False): ... - def iter(self, tag=None, *tags): ... - def iterdescendants(self, tag=None, *tags): ... - def itersiblings(self, tag=None, preceding: bool = False): ... - def getchildren(self): ... - def getiterator(self, tag=None): ... + def __iter__(self) -> Iterator[_LxmlElement]: ... + def iterchildren(self, tag: _LxmlTag | None = None, reversed: bool = False) -> Iterator[_LxmlElement]: ... + def iter(self, tag: _LxmlTag | None = None, *tags: _LxmlTag) -> Iterator[_LxmlElement]: ... + def iterdescendants(self, tag: _LxmlTag | None = None, *tags: _LxmlTag) -> Iterator[_LxmlElement]: ... + def itersiblings(self, tag: _LxmlTag | None = None, preceding: bool = False) -> Iterator[_LxmlElement]: ... + def getchildren(self) -> list[_LxmlElement]: ... + def getiterator(self, tag: _LxmlTag | None = None) -> Iterator[_LxmlElement]: ... class GlobalParserTLS(threading.local): - parser_config: Incomplete - element_class: Incomplete - def createDefaultParser(self): ... - def setDefaultParser(self, parser) -> None: ... - def getDefaultParser(self): ... + parser_config: dict[str, Any] + element_class: type[_LxmlElement] | None + def createDefaultParser(self) -> _LxmlXMLParser: ... + def setDefaultParser(self, parser: _LxmlXMLParser) -> None: ... + def getDefaultParser(self) -> _LxmlXMLParser: ... -def getDefaultParser(): ... -def check_docinfo(elementtree, forbid_dtd: bool = False, forbid_entities: bool = True) -> None: ... -def parse(source, parser=None, base_url=None, forbid_dtd: bool = False, forbid_entities: bool = True): ... -def fromstring(text, parser=None, base_url=None, forbid_dtd: bool = False, forbid_entities: bool = True): ... +def getDefaultParser() -> _LxmlXMLParser: ... +def check_docinfo(elementtree: _LxmlElementTree, forbid_dtd: bool = False, forbid_entities: bool = True) -> None: ... +def parse( + source: StrOrBytesPath | SupportsRead[bytes | str], + parser: _LxmlXMLParser | None = None, + base_url: str | bytes | None = None, + forbid_dtd: bool = False, + forbid_entities: bool = True, +) -> _LxmlElementTree: ... +def fromstring( + text: str | ReadableBuffer, + parser: _LxmlXMLParser | None = None, + base_url: str | bytes | None = None, + forbid_dtd: bool = False, + forbid_entities: bool = True, +) -> RestrictedElement | _LxmlElement: ... XML = fromstring -def iterparse(*args, **kwargs) -> None: ... +def iterparse(*args: Unused, **kwargs: Unused) -> NoReturn: ...