From 71f70e658f1a485569b66dc533a5fb9d3d64e5ca Mon Sep 17 00:00:00 2001 From: Abel Cheung Date: Fri, 25 Oct 2024 22:06:56 +0000 Subject: [PATCH] refactor: Drop typing.TypeAlias usage Explicit TypeAlias is actually unnecessary in most places, and for remaining ones can also be removed with minimal change. --- lxml-stubs/_types.pyi | 32 +++++++++++++------------------- lxml-stubs/etree/_iterparse.pyi | 9 ++------- lxml-stubs/etree/_xslt.pyi | 7 +------ lxml-stubs/html/_form.pyi | 7 +------ lxml-stubs/html/_parse.pyi | 8 +------- 5 files changed, 18 insertions(+), 45 deletions(-) diff --git a/lxml-stubs/_types.pyi b/lxml-stubs/_types.pyi index 2c04255..6b057ea 100644 --- a/lxml-stubs/_types.pyi +++ b/lxml-stubs/_types.pyi @@ -1,4 +1,3 @@ -import sys from os import PathLike from typing import ( Any, @@ -14,37 +13,32 @@ from typing import ( from _typeshed import SupportsRead, SupportsWrite -if sys.version_info >= (3, 10): - from typing import TypeAlias -else: - from typing_extensions import TypeAlias - from .etree import HTMLParser, QName, XMLParser, _Element, _ElementTree _KT_co = TypeVar("_KT_co", covariant=True) _VT_co = TypeVar("_VT_co", covariant=True) # Dup but deviate from recent _typeshed -Unused: TypeAlias = Any +Unused = Any # ElementTree API is notable of canonicalizing byte / unicode input data. # This type alias should only be used for input arguments, while one would # expect plain str in return type for most part of API (except a few places), # as far as python3 annotation is concerned. # Not to be confused with typing.AnyStr which is TypeVar. -_AnyStr: TypeAlias = str | bytes +_AnyStr = str | bytes -# String argument also support QName in various places -_TextArg: TypeAlias = str | bytes | QName +# String argument also support QName in various places; +# also include aliases semantically indicating the purpose +# of text argument +_TextArg = str | bytes | QName +_TagName = str | bytes | QName +_AttrName = str | bytes | QName +_AttrVal = str | bytes | QName # On the other hand, Elementpath API doesn't do str/byte canonicalization, # only unicode accepted for py3 -_ElemPathArg: TypeAlias = str | QName - -# Aliases semantically indicating the purpose of text argument -_TagName: TypeAlias = _TextArg -_AttrName: TypeAlias = _TextArg -_AttrVal: TypeAlias = _TextArg +_ElemPathArg = str | QName # Due to Mapping having invariant key types, Mapping[A | B, ...] # would fail to validate against either Mapping[A, ...] or Mapping[B, ...] @@ -73,7 +67,7 @@ _StrictNSMap = ( # Some namespace map arguments also accept tuple form # such as in dict() -_NSTuples: TypeAlias = Iterable[tuple[_AnyStr | None, _AnyStr]] +_NSTuples = Iterable[tuple[_AnyStr | None, _AnyStr]] # https://lxml.de/extensions.html#xpath-extension-functions # The returned result of extension function itself is not exactly Any, @@ -162,9 +156,9 @@ class _ElementFactory(Protocol, Generic[_ET_co]): # but checks for exact element *factory functions* instead # (etree.Element() and friends). Python typing system doesn't # support such outlandish usage. Use a generic callable instead. -_TagSelector: TypeAlias = _TagName | Callable[..., _Element] +_TagSelector = _TagName | Callable[..., _Element] -_ElementOrTree: TypeAlias = _ET | _ElementTree[_ET] +_ElementOrTree = _ET | _ElementTree[_ET] # The basic parsers bundled in lxml.etree _DefEtreeParsers = XMLParser[_ET_co] | HTMLParser[_ET_co] diff --git a/lxml-stubs/etree/_iterparse.pyi b/lxml-stubs/etree/_iterparse.pyi index d04b8f7..dd460c3 100644 --- a/lxml-stubs/etree/_iterparse.pyi +++ b/lxml-stubs/etree/_iterparse.pyi @@ -3,11 +3,6 @@ from typing import Collection, Iterable, Iterator, Literal, TypeVar, overload from _typeshed import SupportsRead -if sys.version_info >= (3, 10): - from typing import TypeAlias -else: - from typing_extensions import TypeAlias - if sys.version_info >= (3, 11): from typing import LiteralString else: @@ -32,8 +27,8 @@ _T_co = TypeVar("_T_co", covariant=True) # See https://lxml.de/parsing.html#event-types # Undocumented: 'comment' and 'pi' are actually supported! -_NoNSEventNames: TypeAlias = Literal["start", "end", "comment", "pi"] -_SaxNsEventValues: TypeAlias = tuple[str, str] | None # for start-ns & end-ns event +_NoNSEventNames = Literal["start", "end", "comment", "pi"] +_SaxNsEventValues = tuple[str, str] | None # for start-ns & end-ns event class iterparse(Iterator[_T_co]): """Incremental parser diff --git a/lxml-stubs/etree/_xslt.pyi b/lxml-stubs/etree/_xslt.pyi index 399e879..4417cc6 100644 --- a/lxml-stubs/etree/_xslt.pyi +++ b/lxml-stubs/etree/_xslt.pyi @@ -6,11 +6,6 @@ import abc import sys from typing import Any, ClassVar, Literal, TypedDict, final, overload -if sys.version_info >= (3, 10): - from typing import TypeAlias -else: - from typing_extensions import TypeAlias - if sys.version_info >= (3, 13): from warnings import deprecated else: @@ -30,7 +25,7 @@ from ._serializer import SerialisationError from ._xmlerror import _ListErrorLog from ._xpath import XPath -_Stylesheet_Param: TypeAlias = _XSLTQuotedStringParam | XPath | str +_Stylesheet_Param = _XSLTQuotedStringParam | XPath | str # exported constants LIBXSLT_VERSION: tuple[int, int, int] diff --git a/lxml-stubs/html/_form.pyi b/lxml-stubs/html/_form.pyi index 37968b5..16fecf2 100644 --- a/lxml-stubs/html/_form.pyi +++ b/lxml-stubs/html/_form.pyi @@ -12,11 +12,6 @@ from typing import ( overload, ) -if sys.version_info >= (3, 10): - from typing import TypeAlias -else: - from typing_extensions import TypeAlias - if sys.version_info >= (3, 11): from typing import Never else: @@ -27,7 +22,7 @@ from ._element import HtmlElement _T = TypeVar("_T") -_AnyInputElement: TypeAlias = InputElement | SelectElement | TextareaElement +_AnyInputElement = InputElement | SelectElement | TextareaElement class FormElement(HtmlElement): @property diff --git a/lxml-stubs/html/_parse.pyi b/lxml-stubs/html/_parse.pyi index 9e5d2b5..b71d046 100644 --- a/lxml-stubs/html/_parse.pyi +++ b/lxml-stubs/html/_parse.pyi @@ -1,11 +1,5 @@ -import sys from typing import Any, Iterable, Literal, MutableMapping, overload -if sys.version_info >= (3, 10): - from typing import TypeAlias -else: - from typing_extensions import TypeAlias - from .. import etree from .._types import ( Unused, @@ -16,7 +10,7 @@ from .._types import ( ) from ._element import HtmlElement -_HtmlElemParser: TypeAlias = _DefEtreeParsers[HtmlElement] +_HtmlElemParser = _DefEtreeParsers[HtmlElement] # # Parser