Skip to content

Commit

Permalink
refactor: Drop typing.TypeAlias usage
Browse files Browse the repository at this point in the history
Explicit TypeAlias is actually unnecessary in most places, and for remaining ones can also be removed with minimal change.
  • Loading branch information
abelcheung committed Oct 25, 2024
1 parent f565104 commit 71f70e6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 45 deletions.
32 changes: 13 additions & 19 deletions lxml-stubs/_types.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
from os import PathLike
from typing import (
Any,
Expand All @@ -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, ...]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down
9 changes: 2 additions & 7 deletions lxml-stubs/etree/_iterparse.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions lxml-stubs/etree/_xslt.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]
Expand Down
7 changes: 1 addition & 6 deletions lxml-stubs/html/_form.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -27,7 +22,7 @@ from ._element import HtmlElement

_T = TypeVar("_T")

_AnyInputElement: TypeAlias = InputElement | SelectElement | TextareaElement
_AnyInputElement = InputElement | SelectElement | TextareaElement

class FormElement(HtmlElement):
@property
Expand Down
8 changes: 1 addition & 7 deletions lxml-stubs/html/_parse.pyi
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -16,7 +10,7 @@ from .._types import (
)
from ._element import HtmlElement

_HtmlElemParser: TypeAlias = _DefEtreeParsers[HtmlElement]
_HtmlElemParser = _DefEtreeParsers[HtmlElement]

#
# Parser
Expand Down

0 comments on commit 71f70e6

Please sign in to comment.