Skip to content

Commit 2f8196d

Browse files
[defusedxml] Complete annotations in defusedxml.lxml
1 parent 98ab147 commit 2f8196d

2 files changed

Lines changed: 76 additions & 27 deletions

File tree

‎pyrightconfig.stricter.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
"stubs/cffi",
3535
"stubs/colorful",
3636
"stubs/dateparser",
37-
"stubs/defusedxml",
3837
"stubs/docker",
3938
"stubs/docutils",
4039
"stubs/Flask-SocketIO",
Lines changed: 76 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,65 @@
11
import threading
2-
from _typeshed import Incomplete
3-
from typing import Final, Literal, type_check_only
4-
5-
# Not bothering with types here as lxml support is supposed to be dropped in a future version of defusedxml
2+
from _typeshed import Incomplete, ReadableBuffer, StrOrBytesPath, SupportsRead, Unused
3+
from collections.abc import Iterable, Iterator
4+
from typing import Any, Final, Literal, NoReturn, TypeAlias, overload, type_check_only
65

76
LXML3: bool
87
__origin__: Final = "lxml.etree"
98

9+
# lxml lacks typeshed stubs
10+
_LxmlElement: TypeAlias = Any
11+
_LxmlElementTree: TypeAlias = Any
12+
_LxmlXMLParser: TypeAlias = Any
13+
_LxmlTag: TypeAlias = Any
14+
15+
@overload
16+
def tostring(
17+
element_or_tree: _LxmlElement | _LxmlElementTree,
18+
*,
19+
encoding: None = None,
20+
method: Literal["xml", "html", "text", "c14n", "c14n2"] = "xml",
21+
xml_declaration: bool | None = None,
22+
pretty_print: bool = False,
23+
with_tail: bool = True,
24+
standalone: bool | None = None,
25+
doctype: str | bytes | None = None,
26+
exclusive: bool = False,
27+
inclusive_ns_prefixes: Iterable[str | bytes] | None = None,
28+
with_comments: bool = True,
29+
strip_text: bool = False,
30+
) -> bytes: ...
31+
@overload
32+
def tostring(
33+
element_or_tree: _LxmlElement | _LxmlElementTree,
34+
*,
35+
encoding: Literal["unicode"] | type[str],
36+
method: Literal["xml", "html", "text", "c14n", "c14n2"] = "xml",
37+
xml_declaration: bool | None = None,
38+
pretty_print: bool = False,
39+
with_tail: bool = True,
40+
standalone: bool | None = None,
41+
doctype: str | bytes | None = None,
42+
exclusive: bool = False,
43+
inclusive_ns_prefixes: Iterable[str | bytes] | None = None,
44+
with_comments: bool = True,
45+
strip_text: bool = False,
46+
) -> str: ...
47+
@overload
1048
def tostring(
11-
element_or_tree,
49+
element_or_tree: _LxmlElement | _LxmlElementTree,
1250
*,
13-
encoding: str | None = None,
51+
encoding: str | bytes,
1452
method: Literal["xml", "html", "text", "c14n", "c14n2"] = "xml",
1553
xml_declaration: bool | None = None,
1654
pretty_print: bool = False,
1755
with_tail: bool = True,
1856
standalone: bool | None = None,
19-
doctype=None,
57+
doctype: str | bytes | None = None,
2058
exclusive: bool = False,
21-
inclusive_ns_prefixes=None,
59+
inclusive_ns_prefixes: Iterable[str | bytes] | None = None,
2260
with_comments: bool = True,
2361
strip_text: bool = False,
24-
): ...
62+
) -> Any: ...
2563

2664
# Should be imported from lxml.etree.ElementBase, but lxml lacks types
2765
@type_check_only
@@ -30,26 +68,38 @@ class _ElementBase: ...
3068
class RestrictedElement(_ElementBase):
3169
__slots__ = ()
3270
blacklist: Incomplete
33-
def __iter__(self): ...
34-
def iterchildren(self, tag=None, reversed: bool = False): ...
35-
def iter(self, tag=None, *tags): ...
36-
def iterdescendants(self, tag=None, *tags): ...
37-
def itersiblings(self, tag=None, preceding: bool = False): ...
38-
def getchildren(self): ...
39-
def getiterator(self, tag=None): ...
71+
def __iter__(self) -> Iterator[_LxmlElement]: ...
72+
def iterchildren(self, tag: _LxmlTag | None = None, reversed: bool = False) -> Iterator[_LxmlElement]: ...
73+
def iter(self, tag: _LxmlTag | None = None, *tags: _LxmlTag) -> Iterator[_LxmlElement]: ...
74+
def iterdescendants(self, tag: _LxmlTag | None = None, *tags: _LxmlTag) -> Iterator[_LxmlElement]: ...
75+
def itersiblings(self, tag: _LxmlTag | None = None, preceding: bool = False) -> Iterator[_LxmlElement]: ...
76+
def getchildren(self) -> list[_LxmlElement]: ...
77+
def getiterator(self, tag: _LxmlTag | None = None) -> Iterator[_LxmlElement]: ...
4078

4179
class GlobalParserTLS(threading.local):
42-
parser_config: Incomplete
43-
element_class: Incomplete
44-
def createDefaultParser(self): ...
45-
def setDefaultParser(self, parser) -> None: ...
46-
def getDefaultParser(self): ...
80+
parser_config: dict[str, Any]
81+
element_class: type[_LxmlElement] | None
82+
def createDefaultParser(self) -> _LxmlXMLParser: ...
83+
def setDefaultParser(self, parser: _LxmlXMLParser) -> None: ...
84+
def getDefaultParser(self) -> _LxmlXMLParser: ...
4785

48-
def getDefaultParser(): ...
49-
def check_docinfo(elementtree, forbid_dtd: bool = False, forbid_entities: bool = True) -> None: ...
50-
def parse(source, parser=None, base_url=None, forbid_dtd: bool = False, forbid_entities: bool = True): ...
51-
def fromstring(text, parser=None, base_url=None, forbid_dtd: bool = False, forbid_entities: bool = True): ...
86+
def getDefaultParser() -> _LxmlXMLParser: ...
87+
def check_docinfo(elementtree: _LxmlElementTree, forbid_dtd: bool = False, forbid_entities: bool = True) -> None: ...
88+
def parse(
89+
source: StrOrBytesPath | SupportsRead[bytes | str],
90+
parser: _LxmlXMLParser | None = None,
91+
base_url: str | bytes | None = None,
92+
forbid_dtd: bool = False,
93+
forbid_entities: bool = True,
94+
) -> _LxmlElementTree: ...
95+
def fromstring(
96+
text: str | ReadableBuffer,
97+
parser: _LxmlXMLParser | None = None,
98+
base_url: str | bytes | None = None,
99+
forbid_dtd: bool = False,
100+
forbid_entities: bool = True,
101+
) -> RestrictedElement | _LxmlElement: ...
52102

53103
XML = fromstring
54104

55-
def iterparse(*args, **kwargs) -> None: ...
105+
def iterparse(*args: Unused, **kwargs: Unused) -> NoReturn: ...

0 commit comments

Comments
 (0)