stdlib: add __slots__ (#14611)

This commit is contained in:
Jelle Zijlstra
2025-08-21 07:24:59 -07:00
committed by GitHub
parent 28abff1eb3
commit f32d9f08bd
38 changed files with 247 additions and 12 deletions
+1
View File
@@ -3,6 +3,7 @@ from typing import Any, Final, Literal
from .domreg import getDOMImplementation as getDOMImplementation, registerDOMImplementation as registerDOMImplementation
class Node:
__slots__ = ()
ELEMENT_NODE: Final = 1
ATTRIBUTE_NODE: Final = 2
TEXT_NODE: Final = 3
+5
View File
@@ -17,6 +17,7 @@ FILTER_INTERRUPT: Final = DOMBuilderFilter.FILTER_INTERRUPT
theDOMImplementation: DOMImplementation
class ElementInfo:
__slots__ = ("_attr_info", "_model", "tagName")
tagName: str
def __init__(self, tagName: str, model: _Model | None = None) -> None: ...
def getAttributeType(self, aname: str) -> TypeInfo: ...
@@ -66,19 +67,23 @@ class ExpatBuilder:
def xml_decl_handler(self, version: str, encoding: str | None, standalone: int) -> None: ...
class FilterVisibilityController:
__slots__ = ("filter",)
filter: DOMBuilderFilter
def __init__(self, filter: DOMBuilderFilter) -> None: ...
def startContainer(self, node: Node) -> int: ...
def acceptNode(self, node: Node) -> int: ...
class FilterCrutch:
__slots__ = ("_builder", "_level", "_old_start", "_old_end")
def __init__(self, builder: ExpatBuilder) -> None: ...
class Rejecter(FilterCrutch):
__slots__ = ()
def start_element_handler(self, *args: Any) -> None: ...
def end_element_handler(self, *args: Any) -> None: ...
class Skipper(FilterCrutch):
__slots__ = ()
def start_element_handler(self, *args: Any) -> None: ...
def end_element_handler(self, *args: Any) -> None: ...
+2
View File
@@ -8,11 +8,13 @@ _T = TypeVar("_T")
StringTypes: tuple[type[str]]
class NodeList(list[_T]):
__slots__ = ()
@property
def length(self) -> int: ...
def item(self, index: int) -> _T | None: ...
class EmptyNodeList(tuple[()]):
__slots__ = ()
@property
def length(self) -> Literal[0]: ...
def item(self, index: int) -> None: ...
+26
View File
@@ -188,6 +188,7 @@ _AttrChildrenVar = TypeVar("_AttrChildrenVar", bound=_AttrChildren)
_AttrChildrenPlusFragment = TypeVar("_AttrChildrenPlusFragment", bound=_AttrChildren | DocumentFragment)
class Attr(Node):
__slots__ = ("_name", "_value", "namespaceURI", "_prefix", "childNodes", "_localName", "ownerDocument", "ownerElement")
nodeType: ClassVar[Literal[2]]
nodeName: str # same as Attr.name
nodeValue: str # same as Attr.value
@@ -231,6 +232,7 @@ class Attr(Node):
# In the DOM, this interface isn't specific to Attr, but our implementation is
# because that's the only place we use it.
class NamedNodeMap:
__slots__ = ("_attrs", "_attrsNS", "_ownerElement")
def __init__(self, attrs: dict[str, Attr], attrsNS: dict[_NSName, Attr], ownerElement: Element) -> None: ...
@property
def length(self) -> int: ...
@@ -262,6 +264,7 @@ class NamedNodeMap:
AttributeList = NamedNodeMap
class TypeInfo:
__slots__ = ("namespace", "name")
namespace: str | None
name: str | None
def __init__(self, namespace: Incomplete | None, name: str | None) -> None: ...
@@ -270,6 +273,20 @@ _ElementChildrenVar = TypeVar("_ElementChildrenVar", bound=_ElementChildren)
_ElementChildrenPlusFragment = TypeVar("_ElementChildrenPlusFragment", bound=_ElementChildren | DocumentFragment)
class Element(Node):
__slots__ = (
"ownerDocument",
"parentNode",
"tagName",
"nodeName",
"prefix",
"namespaceURI",
"_localName",
"childNodes",
"_attrs",
"_attrsNS",
"nextSibling",
"previousSibling",
)
nodeType: ClassVar[Literal[1]]
nodeName: str # same as Element.tagName
nodeValue: None
@@ -331,6 +348,7 @@ class Element(Node):
def removeChild(self, oldChild: _ElementChildrenVar) -> _ElementChildrenVar: ... # type: ignore[override]
class Childless:
__slots__ = ()
attributes: None
childNodes: EmptyNodeList
@property
@@ -347,6 +365,7 @@ class Childless:
def replaceChild(self, newChild: _NodesThatAreChildren | DocumentFragment, oldChild: _NodesThatAreChildren) -> NoReturn: ...
class ProcessingInstruction(Childless, Node):
__slots__ = ("target", "data")
nodeType: ClassVar[Literal[7]]
nodeName: str # same as ProcessingInstruction.target
nodeValue: str # same as ProcessingInstruction.data
@@ -373,6 +392,7 @@ class ProcessingInstruction(Childless, Node):
def writexml(self, writer: SupportsWrite[str], indent: str = "", addindent: str = "", newl: str = "") -> None: ...
class CharacterData(Childless, Node):
__slots__ = ("_data", "ownerDocument", "parentNode", "previousSibling", "nextSibling")
nodeValue: str
attributes: None
@@ -397,6 +417,7 @@ class CharacterData(Childless, Node):
def replaceData(self, offset: int, count: int, arg: str) -> None: ...
class Text(CharacterData):
__slots__ = ()
nodeType: ClassVar[Literal[3]]
nodeName: Literal["#text"]
nodeValue: str # same as CharacterData.data, the content of the text node
@@ -448,6 +469,7 @@ class Comment(CharacterData):
def writexml(self, writer: SupportsWrite[str], indent: str = "", addindent: str = "", newl: str = "") -> None: ...
class CDATASection(Text):
__slots__ = ()
nodeType: ClassVar[Literal[4]] # type: ignore[assignment]
nodeName: Literal["#cdata-section"] # type: ignore[assignment]
nodeValue: str # same as CharacterData.data, the content of the CDATA Section
@@ -460,6 +482,7 @@ class CDATASection(Text):
def writexml(self, writer: SupportsWrite[str], indent: str = "", addindent: str = "", newl: str = "") -> None: ...
class ReadOnlySequentialNamedNodeMap(Generic[_N]):
__slots__ = ("_seq",)
def __init__(self, seq: Sequence[_N] = ()) -> None: ...
def __len__(self) -> int: ...
def getNamedItem(self, name: str) -> _N | None: ...
@@ -474,6 +497,7 @@ class ReadOnlySequentialNamedNodeMap(Generic[_N]):
def length(self) -> int: ...
class Identified:
__slots__ = ("publicId", "systemId")
publicId: str | None
systemId: str | None
@@ -565,6 +589,7 @@ class DOMImplementation(DOMImplementationLS):
def getInterface(self, feature: str) -> Self | None: ...
class ElementInfo:
__slots__ = ("tagName",)
tagName: str
def __init__(self, name: str) -> None: ...
def getAttributeType(self, aname: str) -> TypeInfo: ...
@@ -577,6 +602,7 @@ class ElementInfo:
_DocumentChildrenPlusFragment = TypeVar("_DocumentChildrenPlusFragment", bound=_DocumentChildren | DocumentFragment)
class Document(Node, DocumentLS):
__slots__ = ("_elem_info", "doctype", "_id_search_stack", "childNodes", "_id_cache")
nodeType: ClassVar[Literal[9]]
nodeName: Literal["#document"]
nodeValue: None
+2
View File
@@ -44,9 +44,11 @@ class DOMBuilder:
def parseWithContext(self, input: DOMInputSource, cnode: Node, action: Literal[1, 2, 3, 4]) -> NoReturn: ...
class DOMEntityResolver:
__slots__ = ("_opener",)
def resolveEntity(self, publicId: str | None, systemId: str) -> DOMInputSource: ...
class DOMInputSource:
__slots__ = ("byteStream", "characterStream", "stringData", "encoding", "publicId", "systemId", "baseURI")
byteStream: SupportsRead[bytes] | None
characterStream: SupportsRead[str] | None
stringData: str | None