Skip to content

Commit

Permalink
Keep only versioned imports for not-builtin types
Browse files Browse the repository at this point in the history
  - Builtins (e.g. Tuple) will be replaced when dropping Py3.8
    support, removing _typing module will catch all imports that
    have to be fixed to avoid deprecation warning in the future.
  • Loading branch information
brunato committed Sep 9, 2024
1 parent 45c8edf commit 34dd4ef
Show file tree
Hide file tree
Showing 46 changed files with 118 additions and 96 deletions.
27 changes: 8 additions & 19 deletions elementpath/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,20 @@
# @author Davide Brunato <[email protected]>
#
"""
Version related imports for subscriptable types for type annotations.
Version related imports for subscriptable types for type annotations (no builtins).
"""
import sys
from typing import Any, cast, ClassVar, Generic, Hashable, ItemsView, Optional, \
NoReturn, Protocol, Sized, SupportsFloat, TYPE_CHECKING, Tuple, Type, \
TypeVar, Union

if sys.version_info < (3, 9):
from typing import Callable, Counter, Deque, Dict, List, Iterable, Iterator, \
Mapping, Match, MutableMapping, MutableSequence, Pattern, Sequence, Set
from typing import Callable, Counter, Deque, Iterable, Iterator, \
Mapping, Match, MutableMapping, MutableSequence, MutableSet, Pattern, Sequence
else:
from collections import deque, Counter
from collections import deque as Deque, Counter # noqa
from collections.abc import Callable, Iterable, Iterator, Mapping, MutableMapping, \
MutableSequence, Sequence
MutableSequence, MutableSet, Sequence
from re import Match, Pattern

Deque = deque
Dict = dict
List = list
Set = set

Never = NoReturn

__all__ = ['Any', 'Callable', 'cast', 'ClassVar', 'Counter', 'Deque', 'Dict', 'Generic',
'Hashable', 'Iterable', 'ItemsView', 'Iterator', 'List', 'Match',
'Mapping', 'MutableMapping', 'MutableSequence', 'Never', 'Optional',
'Pattern', 'Protocol', 'Sequence', 'Set', 'Sized', 'SupportsFloat',
'Union', 'Tuple', 'Type', 'TYPE_CHECKING', 'TypeVar']
__all__ = ['Callable', 'Counter', 'Deque', 'Iterable', 'Iterator', 'Match',
'Mapping', 'MutableMapping', 'MutableSequence', 'MutableSet',
'Pattern', 'Sequence']
7 changes: 4 additions & 3 deletions elementpath/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
"""
Common type hints aliases for elementpath.
"""
from ._typing import Any, List, MutableMapping, Never, Optional, Tuple, Type, \
TYPE_CHECKING, TypeVar, Union
from typing import Any, List, Optional, NoReturn, Tuple, Type, TYPE_CHECKING, TypeVar, Union

from elementpath._typing import MutableMapping

##
# Type aliases
Expand All @@ -23,7 +24,7 @@
ClassCheckType = Union[Type[Any], Tuple[Type[Any], ...]]

T = TypeVar('T')
Emptiable = Union[T, List[Never]]
Emptiable = Union[T, List[NoReturn]]
SequenceType = Union[T, List[T]]
InputType = Union[None, T, List[T], Tuple[T, ...]]

Expand Down
2 changes: 1 addition & 1 deletion elementpath/collations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import threading
from contextlib import AbstractContextManager
from types import TracebackType
from typing import TYPE_CHECKING, Any, Optional, Tuple, Type, Union
from urllib.parse import urljoin, urlsplit

from elementpath._typing import TYPE_CHECKING, Any, Optional, Tuple, Type, Union
from elementpath.exceptions import xpath_error

if TYPE_CHECKING:
Expand Down
3 changes: 2 additions & 1 deletion elementpath/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
from decimal import Decimal
from functools import cmp_to_key
from itertools import zip_longest
from typing import Any, Optional

from elementpath._typing import Any, Callable, Optional, Iterable, Iterator
from elementpath._typing import Callable, Iterable, Iterator
from elementpath.protocols import ElementProtocol
from elementpath.exceptions import xpath_error
from elementpath.datatypes import UntypedAtomic, AnyURI, AbstractQName
Expand Down
4 changes: 3 additions & 1 deletion elementpath/datatypes/atomic_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
# @author Davide Brunato <[email protected]>
#
from abc import ABCMeta, abstractmethod
from typing import Any, Dict, Optional, Pattern, Tuple, Type
from typing import Any, Dict, Optional, Tuple, Type
import re

from elementpath._typing import Pattern

XSD_NAMESPACE = "http://www.w3.org/2001/XMLSchema"

###
Expand Down
2 changes: 1 addition & 1 deletion elementpath/datatypes/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import re
import codecs

from ..helpers import collapse_white_spaces
from elementpath.helpers import collapse_white_spaces
from .atomic_types import AnyAtomicType
from .untyped import UntypedAtomic

Expand Down
3 changes: 2 additions & 1 deletion elementpath/datatypes/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
import datetime
from calendar import isleap
from decimal import Decimal, Context
from typing import cast, Any, Callable, Dict, Optional, Tuple, Type, TypeVar, Union
from typing import cast, Any, Dict, Optional, Tuple, Type, TypeVar, Union

from elementpath._typing import Callable
from elementpath.helpers import MONTH_DAYS_LEAP, MONTH_DAYS, DAYS_IN_4Y, \
DAYS_IN_100Y, DAYS_IN_400Y, days_from_common_era, adjust_day, \
normalized_seconds, months2days, round_number
Expand Down
2 changes: 1 addition & 1 deletion elementpath/datatypes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import math
from typing import Any, Optional, SupportsFloat, SupportsInt, Union, Type

from ..helpers import NUMERIC_INF_OR_NAN, INVALID_NUMERIC, collapse_white_spaces
from elementpath.helpers import NUMERIC_INF_OR_NAN, INVALID_NUMERIC, collapse_white_spaces
from .atomic_types import AnyAtomicType


Expand Down
2 changes: 1 addition & 1 deletion elementpath/datatypes/proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from decimal import Decimal
from typing import Any, Union, SupportsFloat

from ..helpers import BOOLEAN_VALUES, collapse_white_spaces, get_double
from elementpath.helpers import BOOLEAN_VALUES, collapse_white_spaces, get_double
from .atomic_types import AnyAtomicType
from .untyped import UntypedAtomic
from .numeric import Float10, Integer
Expand Down
2 changes: 1 addition & 1 deletion elementpath/datatypes/qname.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
from typing import Any, Optional

from ..helpers import QNAME_PATTERN
from elementpath.helpers import QNAME_PATTERN
from .atomic_types import AnyAtomicType
from .untyped import UntypedAtomic

Expand Down
2 changes: 1 addition & 1 deletion elementpath/datatypes/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import re
from typing import Any

from ..helpers import NORMALIZE_PATTERN, collapse_white_spaces
from elementpath.helpers import NORMALIZE_PATTERN, collapse_white_spaces
from .atomic_types import AnyAtomicType


Expand Down
2 changes: 1 addition & 1 deletion elementpath/datatypes/untyped.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from decimal import Decimal
from typing import Any, Optional, Tuple, Union

from ..helpers import BOOLEAN_VALUES, get_double
from elementpath.helpers import BOOLEAN_VALUES, get_double
from .atomic_types import AnyAtomicType


Expand Down
2 changes: 1 addition & 1 deletion elementpath/datatypes/uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from urllib.parse import urlparse
from typing import Union

from ..helpers import collapse_white_spaces, WRONG_ESCAPE_PATTERN
from elementpath.helpers import collapse_white_spaces, WRONG_ESCAPE_PATTERN
from .atomic_types import AnyAtomicType
from .untyped import UntypedAtomic
from .numeric import Integer
Expand Down
4 changes: 2 additions & 2 deletions elementpath/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"""
from collections import namedtuple
from decimal import Decimal
from typing import Any, Optional, Type, TYPE_CHECKING

from ._typing import Any, MutableMapping, Optional, Type, TYPE_CHECKING

from elementpath._typing import MutableMapping
from elementpath.aliases import AnyNsmapType
from elementpath.protocols import XsdTypeProtocol
from elementpath.exceptions import xpath_error
Expand Down
4 changes: 2 additions & 2 deletions elementpath/etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import re
import io
import importlib
from typing import cast, Any, Optional, Tuple, Union

from elementpath._typing import cast, Any, Counter, Iterator, Optional, MutableMapping, \
Tuple, Union
from elementpath._typing import Counter, Iterator, MutableMapping
from elementpath.protocols import ElementProtocol, DocumentProtocol

###
Expand Down
3 changes: 2 additions & 1 deletion elementpath/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
from calendar import isleap, leapdays
from decimal import Decimal
from operator import attrgetter
from typing import Any, List, Optional, Union, SupportsFloat
from urllib.parse import urlsplit

from elementpath._typing import Any, Iterator, List, Match, Optional, Union, SupportsFloat
from elementpath._typing import Iterator, Match

###
# Common sets constants
Expand Down
2 changes: 1 addition & 1 deletion elementpath/namespaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# @author Davide Brunato <[email protected]>
#
import re
from typing import cast, Tuple, Union

from elementpath._typing import cast, Tuple, Union
from elementpath.aliases import NamespacesType, NsmapType

# Regex patterns related to names and namespaces
Expand Down
12 changes: 10 additions & 2 deletions elementpath/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"""
Define type hints protocols for XPath related objects.
"""
from typing import overload, Any, Iterator, Iterable, Optional, Sequence, ItemsView, \
from typing import overload, Any, Dict, Iterator, Iterable, Optional, Sequence, ItemsView, \
Protocol, Sized, Hashable, Union, TypeVar, Mapping, Tuple, Set

from elementpath._typing import MutableMapping, Dict
from elementpath._typing import MutableMapping
from elementpath.aliases import NamespacesType, NsmapType

_T = TypeVar("_T")
Expand Down Expand Up @@ -239,6 +239,14 @@ def validate(self, obj: Any, *args: Any, **kwargs: Any) -> None:
"""
...

def decode(self, obj: Any, *args: Any, **kwargs: Any) -> Any:
"""
Decodes an XML object node using the XSD type. The argument *obj* is an element
for complex type nodes or a text value for simple type nodes. Raises a `ValueError`
or a `TypeError` compatible exception if the argument it's not valid.
"""
...

@property
def root_type(self) -> 'XsdTypeProtocol':
"""
Expand Down
3 changes: 2 additions & 1 deletion elementpath/regex/character_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
from itertools import chain
from sys import maxunicode
from collections import Counter
from typing import AbstractSet, Any, Iterator, MutableSet, Optional, Union
from typing import AbstractSet, Any, Optional, Union

from elementpath._typing import Iterator, MutableSet
from .unicode_subsets import RegexError, UnicodeSubset, UNICODE_CATEGORIES, unicode_subset


Expand Down
4 changes: 3 additions & 1 deletion elementpath/regex/codepoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
This module defines Unicode code points helper functions.
"""
from sys import maxunicode
from typing import Iterable, Iterator, Optional, Set, Tuple, Union
from typing import Optional, Set, Tuple, Union

from elementpath._typing import Iterable, Iterator

CHARACTER_CLASS_ESCAPED: Set[int] = {ord(c) for c in r'-|.^?*+{}()[]\\'}
"""Code Points of escaped chars in a character class."""
Expand Down
2 changes: 1 addition & 1 deletion elementpath/regex/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import re
from sys import maxunicode

from ..helpers import OCCURRENCE_INDICATORS
from elementpath.helpers import OCCURRENCE_INDICATORS
from .unicode_subsets import RegexError, UnicodeSubset, unicode_subset
from .character_classes import I_SHORTCUT_REPLACE, C_SHORTCUT_REPLACE, CharacterClass

Expand Down
3 changes: 2 additions & 1 deletion elementpath/regex/unicode_subsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
This module defines Unicode character categories and blocks.
"""
from sys import maxunicode
from typing import cast, Iterable, Iterator, List, MutableSet, Union, Optional
from typing import cast, List, Union, Optional

from elementpath._typing import Iterable, Iterator, MutableSet
from .unicode_categories import RAW_UNICODE_CATEGORIES
from .codepoints import CodePoint, code_point_order, code_point_repr, \
iter_code_points, get_code_point_range
Expand Down
3 changes: 2 additions & 1 deletion elementpath/schema_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
# @author Davide Brunato <[email protected]>
#
from abc import ABCMeta, abstractmethod
from typing import TYPE_CHECKING, Any, Dict, Optional, Set, Union

from elementpath._typing import TYPE_CHECKING, Any, Dict, Optional, Iterator, Set, Union
from elementpath._typing import Iterator
from elementpath.exceptions import ElementPathTypeError
from elementpath.protocols import XsdTypeProtocol, XsdAttributeProtocol, \
XsdElementProtocol, XsdSchemaProtocol
Expand Down
20 changes: 10 additions & 10 deletions elementpath/sequence_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
from itertools import zip_longest
from typing import TYPE_CHECKING, cast, Any, Optional

from .exceptions import ElementPathKeyError, xpath_error
from .helpers import OCCURRENCE_INDICATORS, EQNAME_PATTERN, WHITESPACES_PATTERN
from .namespaces import XSD_NAMESPACE, XSD_ERROR, XSD_ANY_SIMPLE_TYPE, XSD_NUMERIC, \
from elementpath.exceptions import ElementPathKeyError, xpath_error
from elementpath.helpers import OCCURRENCE_INDICATORS, EQNAME_PATTERN, WHITESPACES_PATTERN
from elementpath.namespaces import XSD_NAMESPACE, XSD_ERROR, XSD_ANY_SIMPLE_TYPE, XSD_NUMERIC, \
get_expanded_name
from .datatypes import xsd10_atomic_types, xsd11_atomic_types, AnyAtomicType, \
from elementpath.datatypes import xsd10_atomic_types, xsd11_atomic_types, AnyAtomicType, \
QName, NumericProxy
from .xpath_nodes import XPathNode, DocumentNode, ElementNode, AttributeNode
from . import xpath_tokens
from elementpath.xpath_nodes import XPathNode, DocumentNode, ElementNode, AttributeNode
from elementpath import xpath_tokens

if TYPE_CHECKING:
from .xpath1 import XPath1Parser
from elementpath.xpath_tokens import XPathParserType

XSD_EXTENDED_PREFIX = f'{{{XSD_NAMESPACE}}}'

Expand Down Expand Up @@ -133,7 +133,7 @@ def is_sequence_type_restriction(st1: str, st2: str) -> bool:
return True


def is_instance(obj: Any, type_qname: str, parser: Optional['XPath1Parser'] = None) -> bool:
def is_instance(obj: Any, type_qname: str, parser: Optional['XPathParserType'] = None) -> bool:
"""Checks an instance against an XSD type."""
xsd_version = getattr(parser, 'xsd_version', '1.0')
if not type_qname.startswith('{'):
Expand Down Expand Up @@ -168,7 +168,7 @@ def is_instance(obj: Any, type_qname: str, parser: Optional['XPath1Parser'] = No
raise ElementPathKeyError("unknown type %r" % type_qname)


def is_sequence_type(value: Any, parser: Optional['XPath1Parser'] = None) -> bool:
def is_sequence_type(value: Any, parser: Optional['XPathParserType'] = None) -> bool:
"""Checks if a string is a sequence type specification."""

def is_st(st: str) -> bool:
Expand Down Expand Up @@ -268,7 +268,7 @@ def is_st(st: str) -> bool:

def match_sequence_type(value: Any,
sequence_type: str,
parser: Optional['XPath1Parser'] = None,
parser: Optional['XPathParserType'] = None,
strict: bool = True) -> bool:
"""
Checks a value instance against a sequence type.
Expand Down
3 changes: 2 additions & 1 deletion elementpath/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
import json
from decimal import Decimal, ROUND_UP
from types import ModuleType
from typing import cast, Any, Dict, Optional, Set, Union, Tuple
from xml.etree import ElementTree

from elementpath._typing import cast, Any, Dict, Iterator, Iterable, Optional, Set, Union, Tuple
from elementpath._typing import Iterator, Iterable
from elementpath.exceptions import ElementPathError, xpath_error
from elementpath.namespaces import XSLT_XQUERY_SERIALIZATION_NAMESPACE
from elementpath.datatypes import AnyAtomicType, AnyURI, AbstractDateTime, \
Expand Down
7 changes: 4 additions & 3 deletions elementpath/tdop.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
from abc import ABCMeta
from unicodedata import name as unicode_name
from decimal import Decimal, DecimalException
from typing import Any, cast, overload, Callable, Generic, List, \
Optional, Union, Tuple, Type, Pattern, Match, Iterator, TypeVar
from typing import Any, cast, Dict, List, overload, Generic, Optional, Union, \
Tuple, Type, Iterator, TypeVar

from elementpath._typing import MutableMapping, MutableSequence, Dict
from elementpath._typing import Callable, Match, MutableMapping, \
MutableSequence, Pattern
#
# Simple top-down parser based on Vaughan Pratt's algorithm (Top Down Operator Precedence).
#
Expand Down
4 changes: 3 additions & 1 deletion elementpath/tree_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#
# @author Davide Brunato <[email protected]>
#
from elementpath._typing import cast, Any, Iterator, List, Optional, Union
from typing import cast, Any, List, Optional, Union

from elementpath._typing import Iterator
from elementpath.aliases import NamespacesType
from elementpath.exceptions import ElementPathTypeError
from elementpath.protocols import ElementProtocol, LxmlElementProtocol, \
Expand Down
2 changes: 1 addition & 1 deletion elementpath/validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from xml.etree.ElementTree import Element
from typing import Optional

from ..exceptions import ElementPathRuntimeError
from elementpath.exceptions import ElementPathRuntimeError

try:
import xmlschema
Expand Down
Loading

0 comments on commit 34dd4ef

Please sign in to comment.