1# 2# Copyright (c), 2021, SISSA (International School for Advanced Studies). 3# All rights reserved. 4# This file is distributed under the terms of the MIT License. 5# See the file 'LICENSE' in the root directory of the present 6# distribution, or http://opensource.org/licenses/MIT. 7# 8# @author Davide Brunato <brunato@sissa.it> 9# 10""" 11Type aliases for static typing analysis. In a type checking context the aliases 12are defined from effective classes imported from package modules. In a runtime 13context the aliases cannot be set from the same bases, due to circular imports, 14so they are set with a common dummy subscriptable type to keep compatibility. 15""" 16from typing import TYPE_CHECKING, Optional, TypeVar 17 18__all__ = ['ElementType', 'ElementTreeType', 'XMLSourceType', 'NamespacesType', 19 'NormalizedLocationsType', 'LocationsType', 'NsmapType', 'ParentMapType', 20 'LazyType', 'SchemaType', 'BaseXsdType', 'SchemaElementType', 21 'SchemaAttributeType', 'SchemaGlobalType', 'GlobalMapType', 'ModelGroupType', 22 'ModelParticleType', 'XPathElementType', 'AtomicValueType', 'NumericValueType', 23 'DateTimeType', 'SchemaSourceType', 'ConverterType', 'ComponentClassType', 24 'ExtraValidatorType', 'DecodeType', 'IterDecodeType', 'JsonDecodeType', 25 'EncodeType', 'IterEncodeType', 'DecodedValueType', 'EncodedValueType'] 26 27if TYPE_CHECKING: 28 from decimal import Decimal 29 from typing import Callable, Dict, List, IO, Iterator, MutableMapping, Tuple, Type, Union 30 31 from elementpath.datatypes import NormalizedString, QName, Float10, Integer, \ 32 Time, Base64Binary, HexBinary, AnyURI, Duration 33 from elementpath.datatypes.datetime import OrderedDateTime 34 35 from .etree import ElementTree 36 from .resources import XMLResource 37 from .converters import XMLSchemaConverter 38 from .validators import XMLSchemaValidationError, XsdComponent, XMLSchemaBase, \ 39 XsdComplexType, XsdSimpleType, XsdElement, XsdAnyElement, XsdAttribute, \ 40 XsdAnyAttribute, XsdAssert, XsdGroup, XsdAttributeGroup, XsdNotation 41 42 ## 43 # Type aliases for ElementTree 44 ElementType = ElementTree.Element 45 ElementTreeType = ElementTree.ElementTree 46 XMLSourceType = Union[str, bytes, IO[str], IO[bytes], ElementType, ElementTreeType] 47 NamespacesType = MutableMapping[str, str] 48 49 ## 50 # Type aliases for XML resources 51 NormalizedLocationsType = List[Tuple[str, str]] 52 LocationsType = Union[Tuple[Tuple[str, str], ...], Dict[str, str], NormalizedLocationsType] 53 NsmapType = Union[List[Tuple[str, str]], MutableMapping[str, str]] 54 ParentMapType = Dict[ElementType, Optional[ElementType]] 55 LazyType = Union[bool, int] 56 57 ## 58 # Type aliases for XSD components 59 SchemaSourceType = Union[str, bytes, IO[str], IO[bytes], ElementTree.Element, 60 ElementTree.ElementTree, XMLResource] 61 SchemaType = XMLSchemaBase 62 BaseXsdType = Union[XsdSimpleType, XsdComplexType] 63 SchemaElementType = Union[XsdElement, XsdAnyElement] 64 SchemaAttributeType = Union[XsdAttribute, XsdAnyAttribute] 65 SchemaGlobalType = Union[XsdNotation, BaseXsdType, XsdElement, 66 XsdAttribute, XsdAttributeGroup, XsdGroup] 67 68 ModelGroupType = XsdGroup 69 ModelParticleType = Union[XsdElement, XsdAnyElement, XsdGroup] 70 ComponentClassType = Union[None, Type[XsdComponent], Tuple[Type[XsdComponent], ...]] 71 XPathElementType = Union[XsdElement, XsdAnyElement, XsdAssert] 72 73 C = TypeVar('C') 74 GlobalMapType = Dict[str, Union[C, Tuple[ElementType, SchemaType]]] 75 76 ## 77 # Type aliases for datatypes 78 AtomicValueType = Union[str, int, float, Decimal, bool, Integer, Float10, NormalizedString, 79 AnyURI, HexBinary, Base64Binary, QName, Duration, OrderedDateTime, Time] 80 NumericValueType = Union[str, bytes, int, float, Decimal] 81 DateTimeType = Union[OrderedDateTime, Time] 82 83 ## 84 # Type aliases for validation/decoding/encoding 85 ConverterType = Union[Type[XMLSchemaConverter], XMLSchemaConverter] 86 ExtraValidatorType = Callable[[ElementType, SchemaType], 87 Optional[Iterator[XMLSchemaValidationError]]] 88 89 D = TypeVar('D') 90 DecodeType = Union[Optional[D], Tuple[Optional[D], List[XMLSchemaValidationError]]] 91 IterDecodeType = Iterator[Union[D, XMLSchemaValidationError]] 92 93 E = TypeVar('E') 94 EncodeType = Union[E, Tuple[E, List[XMLSchemaValidationError]]] 95 IterEncodeType = Iterator[Union[E, XMLSchemaValidationError]] 96 97 JsonDecodeType = Union[str, None, Tuple[XMLSchemaValidationError, ...], 98 Tuple[Union[str, None], Tuple[XMLSchemaValidationError, ...]]] 99 100 DecodedValueType = Union[None, AtomicValueType, List[AtomicValueType]] 101 EncodedValueType = Union[None, str, List[str]] 102 103else: 104 # In runtime use a dummy subscriptable type for compatibility 105 T = TypeVar('T') 106 DummyType = Optional[T] 107 108 module_globals = globals() 109 for name in __all__: 110 module_globals[name] = DummyType 111