Add types for defusedxml (#11179)

This commit is contained in:
solkaz
2024-01-14 14:08:59 +01:00
committed by GitHub
parent 673c3c37fe
commit 0b43c1d95b
13 changed files with 287 additions and 1 deletions

View File

@@ -34,6 +34,7 @@
"stubs/click-default-group",
"stubs/commonmark",
"stubs/dateparser",
"stubs/defusedxml",
"stubs/docutils",
"stubs/Flask-SocketIO",
"stubs/fpdf2",
@@ -47,9 +48,9 @@
"stubs/ldap3",
"stubs/Markdown",
"stubs/mysqlclient",
"stubs/networkx",
"stubs/oauthlib",
"stubs/openpyxl",
"stubs/networkx",
"stubs/passlib",
"stubs/peewee",
"stubs/pexpect",

View File

@@ -0,0 +1,7 @@
version = "0.7.*"
upstream_repository = "https://github.com/tiran/defusedxml"
partial_stub = true
[tool.stubtest]
ignore_missing_stub = true
stubtest_requirements = ["lxml"]

View File

@@ -0,0 +1,47 @@
from _typeshed import Incomplete
from collections.abc import Iterator, Sequence
from typing import Any
from xml.dom.minidom import Element
from xml.etree.ElementTree import ElementTree, ParseError as ParseError, XMLParser as _XMLParser, tostring as tostring
class DefusedXMLParser(_XMLParser):
forbid_dtd: bool
forbid_entities: bool
forbid_external: bool
def __init__(
self,
html=...,
target: Incomplete | None = None,
encoding: str | None = None,
forbid_dtd: bool = False,
forbid_entities: bool = True,
forbid_external: bool = True,
) -> None: ...
def defused_start_doctype_decl(self, name, sysid, pubid, has_internal_subset) -> None: ...
def defused_entity_decl(self, name, is_parameter_entity, value, base, sysid, pubid, notation_name) -> None: ...
def defused_unparsed_entity_decl(self, name, base, sysid, pubid, notation_name) -> None: ...
def defused_external_entity_ref_handler(self, context, base, sysid, pubid) -> None: ...
XMLTreeBuilder = DefusedXMLParser
XMLParse = DefusedXMLParser
XMLParser = DefusedXMLParser
# wrapper to xml.etree.ElementTree.parse
def parse(
source, parser: XMLParser | None = None, forbid_dtd: bool = False, forbid_entities: bool = True, forbid_external: bool = True
) -> ElementTree: ...
# wrapper to xml.etree.ElementTree.iterparse
def iterparse(
source,
events: Sequence[str] | None = None,
parser: XMLParser | None = None,
forbid_dtd: bool = False,
forbid_entities: bool = True,
forbid_external: bool = True,
) -> Iterator[tuple[str, Any]]: ...
def fromstring(text, forbid_dtd: bool = False, forbid_entities: bool = True, forbid_external: bool = True) -> Element: ...
XML = fromstring
__all__ = ["ParseError", "XML", "XMLParse", "XMLParser", "XMLTreeBuilder", "fromstring", "iterparse", "parse", "tostring"]

View File

@@ -0,0 +1,9 @@
from .common import (
DefusedXmlException as DefusedXmlException,
DTDForbidden as DTDForbidden,
EntitiesForbidden as EntitiesForbidden,
ExternalReferenceForbidden as ExternalReferenceForbidden,
NotSupportedError as NotSupportedError,
)
__all__ = ["DefusedXmlException", "DTDForbidden", "EntitiesForbidden", "ExternalReferenceForbidden", "NotSupportedError"]

View File

@@ -0,0 +1,13 @@
from .ElementTree import (
XML as XML,
ParseError as ParseError,
XMLParse as XMLParse,
XMLParser as XMLParser,
XMLTreeBuilder as XMLTreeBuilder,
fromstring as fromstring,
iterparse as iterparse,
parse as parse,
tostring as tostring,
)
__all__ = ["ParseError", "XML", "XMLParse", "XMLParser", "XMLTreeBuilder", "fromstring", "iterparse", "parse", "tostring"]

View File

@@ -0,0 +1,29 @@
from _typeshed import Incomplete
PY3: bool
class DefusedXmlException(ValueError): ...
class DTDForbidden(DefusedXmlException):
name: Incomplete
sysid: Incomplete
pubid: Incomplete
def __init__(self, name, sysid, pubid) -> None: ...
class EntitiesForbidden(DefusedXmlException):
name: Incomplete
value: Incomplete
base: Incomplete
sysid: Incomplete
pubid: Incomplete
notation_name: Incomplete
def __init__(self, name, value, base, sysid, pubid, notation_name) -> None: ...
class ExternalReferenceForbidden(DefusedXmlException):
context: Incomplete
base: Incomplete
sysid: Incomplete
pubid: Incomplete
def __init__(self, context, base, sysid, pubid) -> None: ...
class NotSupportedError(DefusedXmlException): ...

View File

@@ -0,0 +1,34 @@
from _typeshed import SupportsRead
from xml.dom.expatbuilder import ExpatBuilder as _ExpatBuilder, Namespaces as _Namespaces
from xml.dom.minidom import Document
from xml.dom.xmlbuilder import Options
__origin__: str
class DefusedExpatBuilder(_ExpatBuilder):
forbid_dtd: bool
forbid_entities: bool
forbid_external: bool
def __init__(
self, options: Options | None = None, forbid_dtd: bool = False, forbid_entities: bool = True, forbid_external: bool = True
) -> None: ...
def defused_start_doctype_decl(self, name, sysid, pubid, has_internal_subset) -> None: ...
def defused_entity_decl(self, name, is_parameter_entity, value, base, sysid, pubid, notation_name) -> None: ...
def defused_unparsed_entity_decl(self, name, base, sysid, pubid, notation_name) -> None: ...
def defused_external_entity_ref_handler(self, context, base, sysid, pubid) -> None: ...
def install(self, parser) -> None: ...
class DefusedExpatBuilderNS(_Namespaces, DefusedExpatBuilder):
def install(self, parser) -> None: ...
def reset(self) -> None: ...
def parse(
file: str | SupportsRead[bytes | str],
namespaces: bool = True,
forbid_dtd: bool = False,
forbid_entities: bool = True,
forbid_external: bool = True,
) -> Document: ...
def parseString(
string: str, namespaces: bool = True, forbid_dtd: bool = False, forbid_entities: bool = True, forbid_external: bool = True
) -> Document: ...

View File

@@ -0,0 +1,10 @@
from _typeshed import Incomplete
# Cannot type most things here as DefusedExpatParser is based off of
# xml.sax.expatreader, which is an undocumented module and lacks types at the moment.
__origin__: str
DefusedExpatParser = Incomplete
def create_parser(*args, **kwargs): ...

View File

@@ -0,0 +1,47 @@
import threading
from _typeshed import Incomplete
# Not bothering with types here as lxml support is supposed to be dropped in a future version
# of defusedxml
LXML3: Incomplete
__origin__: str
tostring: Incomplete
# Should be imported from lxml.etree.ElementBase, but lxml lacks types
class _ElementBase: ...
class RestrictedElement(_ElementBase):
blacklist: Incomplete
def __iter__(self): ...
def iterchildren(self, tag: Incomplete | None = ..., reversed: bool = ...): ...
def iter(self, tag: Incomplete | None = ..., *tags): ...
def iterdescendants(self, tag: Incomplete | None = ..., *tags): ...
def itersiblings(self, tag: Incomplete | None = ..., preceding: bool = ...): ...
def getchildren(self): ...
def getiterator(self, tag: Incomplete | None = ...): ...
class GlobalParserTLS(threading.local):
parser_config: Incomplete
element_class: Incomplete
def createDefaultParser(self): ...
def setDefaultParser(self, parser) -> None: ...
def getDefaultParser(self): ...
getDefaultParser: Incomplete
def check_docinfo(elementtree, forbid_dtd: bool = ..., forbid_entities: bool = ...) -> None: ...
def parse(
source,
parser: Incomplete | None = ...,
base_url: Incomplete | None = ...,
forbid_dtd: bool = ...,
forbid_entities: bool = ...,
): ...
def fromstring(
text, parser: Incomplete | None = ..., base_url: Incomplete | None = ..., forbid_dtd: bool = ..., forbid_entities: bool = ...
): ...
XML = fromstring
def iterparse(*args, **kwargs) -> None: ...

View File

@@ -0,0 +1,19 @@
from _typeshed import Incomplete
__origin__: str
def parse(
file,
parser: Incomplete | None = None,
bufsize: int | None = None,
forbid_dtd: bool = False,
forbid_entities: bool = True,
forbid_external: bool = True,
): ...
def parseString(
string: str,
parser: Incomplete | None = None,
forbid_dtd: bool = False,
forbid_entities: bool = True,
forbid_external: bool = True,
): ...

View File

@@ -0,0 +1,21 @@
from xml.dom.pulldom import DOMEventStream
from .expatreader import DefusedExpatParser
__origin__: str
def parse(
stream_or_string,
parser: DefusedExpatParser | None = None,
bufsize: int | None = None,
forbid_dtd: bool = False,
forbid_entities: bool = True,
forbid_external: bool = True,
) -> DOMEventStream: ...
def parseString(
string: str,
parser: DefusedExpatParser | None = None,
forbid_dtd: bool = False,
forbid_entities: bool = True,
forbid_external: bool = True,
) -> DOMEventStream: ...

View File

@@ -0,0 +1,24 @@
from _typeshed import Incomplete
from xml.sax import ErrorHandler as _ErrorHandler
from .expatreader import DefusedExpatParser
__origin__: str
def parse(
source,
handler,
errorHandler: _ErrorHandler = ...,
forbid_dtd: bool = False,
forbid_entities: bool = True,
forbid_external: bool = True,
) -> None: ...
def parseString(
string,
handler,
errorHandler: _ErrorHandler = ...,
forbid_dtd: bool = False,
forbid_entities: bool = True,
forbid_external: bool = True,
) -> None: ...
def make_parser(parser_list: list[Incomplete] = []) -> DefusedExpatParser: ...

View File

@@ -0,0 +1,25 @@
from _typeshed import Incomplete
from xmlrpc.client import ExpatParser
__origin__: str
MAX_DATA: int = 31457280
def defused_gzip_decode(data, limit: int | None = None): ...
# Couldn't type this as a class deriving from gzip.GzipFile
# since overwriting `read` method does not define an optional argument
# for size when the underlying class does.
DefusedGzipDecodedResponse = Incomplete
class DefusedExpatParser(ExpatParser):
forbid_dtd: bool
forbid_entities: bool
forbid_external: bool
def __init__(self, target, forbid_dtd: bool = False, forbid_entities: bool = True, forbid_external: bool = True) -> None: ...
def defused_start_doctype_decl(self, name, sysid, pubid, has_internal_subset) -> None: ...
def defused_entity_decl(self, name, is_parameter_entity, value, base, sysid, pubid, notation_name) -> None: ...
def defused_unparsed_entity_decl(self, name, base, sysid, pubid, notation_name) -> None: ...
def defused_external_entity_ref_handler(self, context, base, sysid, pubid) -> None: ...
def monkey_patch() -> None: ...
def unmonkey_patch() -> None: ...