Skip to content

Commit 9e02477

Browse files
committed
Complete type stubs for xml.dom.minidom (python#6886)
This patch completes all the type stubs for xml.dom.minidom. I did not look at the rest of the xml.dom directory except as required to make the typing of minidom complete, so it probably doesn’t completely resolve python#6886. xml.dom.minidom is very poorly documented—its authors seem to have assumed that one could refer to the W3C DOM spec. But at the same time the implementation diverges significantly from the spec, so I do not have complete confidence in all the type annotations. Still, it should be an improvement on what’s there now. The following commands now pass with no errors when executed from the top of the typeshed tree: ```sh mypy --custom-typeshed-dir . --strict --warn-incomplete-stub \ -m xml.dom.minidom stubtest --custom-typeshed-dir . xml.dom.minidom ``` **However**, attempting to do the same for xml.dom.minicompat produces the following error: ```sh mypy --custom-typeshed-dir . --strict --warn-incomplete-stub \ -m xml.dom.minicompat stdlib/xml/dom/minicompat.pyi:11: error: All bases of a protocol must be protocols [misc] ``` I don’t know how to fix this, because in the implementation, EmptyNodeList is not a subclass of NodeList, therefore if I don’t make NodeList a protocol, then xml.dom.minidom.Childless fails stubtest. This patch probably should not land until this is resolved. Any advice you could give would be welcome.
1 parent 95cee31 commit 9e02477

File tree

3 files changed

+156
-162
lines changed

3 files changed

+156
-162
lines changed

stdlib/xml/dom/__init__.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any
1+
from typing import Any, Protocol
22

33
from .domreg import getDOMImplementation as getDOMImplementation, registerDOMImplementation as registerDOMImplementation
44

@@ -56,12 +56,14 @@ class NamespaceErr(DOMException): ...
5656
class InvalidAccessErr(DOMException): ...
5757
class ValidationErr(DOMException): ...
5858

59-
class UserDataHandler:
59+
class UserDataHandler(Protocol):
6060
NODE_CLONED: int
6161
NODE_IMPORTED: int
6262
NODE_DELETED: int
6363
NODE_RENAMED: int
6464

65+
def handle(self, operation: int, key: str, data: object, src: Node, dst: Node) -> None: ...
66+
6567
XML_NAMESPACE: str
6668
XMLNS_NAMESPACE: str
6769
XHTML_NAMESPACE: str

stdlib/xml/dom/minicompat.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
from collections.abc import Iterable
2-
from typing import Any, TypeVar
1+
from collections.abc import Iterable, Sequence
2+
from typing import Any, Protocol, TypeVar
33
from typing_extensions import Literal
44

55
__all__ = ["NodeList", "EmptyNodeList", "StringTypes", "defproperty"]
66

7-
_T = TypeVar("_T")
7+
_T = TypeVar("_T", covariant=True)
88

99
StringTypes: tuple[type[str]]
1010

11-
class NodeList(list[_T]):
11+
class NodeList(Sequence[_T], Protocol):
1212
@property
1313
def length(self) -> int: ...
1414
def item(self, index: int) -> _T | None: ...

0 commit comments

Comments
 (0)