Use TypeAlias where possible for type aliases (#7630)

This commit is contained in:
Alex Waygood
2022-04-16 02:01:00 +01:00
committed by GitHub
parent c0e6dd3f3f
commit 740193a8fc
218 changed files with 760 additions and 625 deletions

View File

@@ -1,7 +1,7 @@
import sys
from _typeshed import SupportsRead
from typing import Any, Sequence
from typing_extensions import Literal
from typing_extensions import Literal, TypeAlias
from xml.dom.minidom import Document, DOMImplementation, Element, Text
from xml.sax.handler import ContentHandler
from xml.sax.xmlreader import XMLReader
@@ -15,10 +15,10 @@ PROCESSING_INSTRUCTION: Literal["PROCESSING_INSTRUCTION"]
IGNORABLE_WHITESPACE: Literal["IGNORABLE_WHITESPACE"]
CHARACTERS: Literal["CHARACTERS"]
_DocumentFactory = DOMImplementation | None
_Node = Document | Element | Text
_DocumentFactory: TypeAlias = DOMImplementation | None
_Node: TypeAlias = Document | Element | Text
_Event = tuple[
_Event: TypeAlias = tuple[
Literal[
Literal["START_ELEMENT"],
Literal["END_ELEMENT"],

View File

@@ -1,5 +1,5 @@
from typing import Any, NoReturn
from typing_extensions import Literal
from typing_extensions import Literal, TypeAlias
from urllib.request import OpenerDirector
from xml.dom.expatbuilder import ExpatBuilder, ExpatBuilderNS
from xml.dom.minidom import Node
@@ -18,13 +18,13 @@ __all__ = ["DOMBuilder", "DOMEntityResolver", "DOMInputSource"]
# probably the same as `Options.errorHandler`?
# Maybe `xml.sax.handler.ErrorHandler`?
_DOMBuilderErrorHandlerType = Any | None
_DOMBuilderErrorHandlerType: TypeAlias = Any | None
# probably some kind of IO...
_DOMInputSourceCharacterStreamType = Any | None
_DOMInputSourceCharacterStreamType: TypeAlias = Any | None
# probably a string??
_DOMInputSourceStringDataType = Any | None
_DOMInputSourceStringDataType: TypeAlias = Any | None
# probably a string??
_DOMInputSourceEncodingType = Any | None
_DOMInputSourceEncodingType: TypeAlias = Any | None
class Options:
namespaces: int

View File

@@ -1,11 +1,12 @@
from typing import Callable, Generator, Pattern, TypeVar
from typing_extensions import TypeAlias
from xml.etree.ElementTree import Element
xpath_tokenizer_re: Pattern[str]
_token = tuple[str, str]
_next = Callable[[], _token]
_callback = Callable[[_SelectorContext, list[Element]], Generator[Element, None, None]]
_token: TypeAlias = tuple[str, str]
_next: TypeAlias = Callable[[], _token]
_callback: TypeAlias = Callable[[_SelectorContext, list[Element]], Generator[Element, None, None]]
def xpath_tokenizer(pattern: str, namespaces: dict[str, str] | None = ...) -> Generator[_token, None, None]: ...
def get_parent_map(context: _SelectorContext) -> dict[Element, Element]: ...

View File

@@ -14,7 +14,7 @@ from typing import (
TypeVar,
overload,
)
from typing_extensions import Literal, SupportsIndex, TypeGuard
from typing_extensions import Literal, SupportsIndex, TypeAlias, TypeGuard
if sys.version_info >= (3, 9):
__all__ = [
@@ -101,9 +101,9 @@ else:
]
_T = TypeVar("_T")
_FileRead = StrOrBytesPath | FileDescriptor | SupportsRead[bytes] | SupportsRead[str]
_FileWriteC14N = StrOrBytesPath | FileDescriptor | SupportsWrite[bytes]
_FileWrite = _FileWriteC14N | SupportsWrite[str]
_FileRead: TypeAlias = StrOrBytesPath | FileDescriptor | SupportsRead[bytes] | SupportsRead[str]
_FileWriteC14N: TypeAlias = StrOrBytesPath | FileDescriptor | SupportsWrite[bytes]
_FileWrite: TypeAlias = _FileWriteC14N | SupportsWrite[str]
VERSION: str
@@ -352,7 +352,7 @@ def fromstringlist(sequence: Sequence[str | bytes], parser: XMLParser | None = .
# TreeBuilder is called by client code (they could pass strs, bytes or whatever);
# but we don't want to use a too-broad type, or it would be too hard to write
# elementfactories.
_ElementFactory = Callable[[Any, dict[Any, Any]], Element]
_ElementFactory: TypeAlias = Callable[[Any, dict[Any, Any]], Element]
class TreeBuilder:
if sys.version_info >= (3, 8):