L iX*UdZddlmZddlZddlZddlmZddlmZddl m Z m Z m Z ddl mZmZmZmZddlmZmZmZmZd d lmZd Zej4d k\sej4d krd(dZnd(dZd)dZddd d*dZedZded< e eeZ!ded<Gdde Z"Gdde#Z$Gdde Z%e%jLZ& ee%jLZ'ded < Gd!d"eZ(d#d$ d+d%Z) d,d&Z*dd$ d-d'Z+y).zEHigh-level introspection utilities, used to inspect type annotations.) annotationsN) Generator)InitVar)EnumIntEnumauto)AnyLiteral NamedTuplecast) TypeAlias assert_neverget_args get_origin)typing_objects)AnnotationSourceForbiddenQualifierInspectedAnnotation Qualifierget_literal_valuesinspect_annotationis_union_origin))r c,tj|SaReturn whether the provided origin is the union form. ```pycon >>> is_union_origin(typing.Union) True >>> is_union_origin(get_origin(int | str)) True >>> is_union_origin(types.UnionType) True ``` !!! note Since Python 3.14, both `Union[, , ...]` and ` | | ...` forms create instances of the same [`typing.Union`][] class. As such, it is recommended to not use this function anymore (provided that you only support Python 3.14 or greater), and instead use the [`typing_objects.is_union()`][typing_inspection.typing_objects.is_union] function directly: ```python from typing import Union, get_origin from typing_inspection import typing_objects typ = int | str # Or Union[int, str] origin = get_origin(typ) if typing_objects.is_union(origin): ... ``` )ris_unionobjs e/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/typing_inspection/introspection.pyrrs:&&s++cTtj|xs|tjuSr)rrtypes UnionTyper s r"rr>s#:&&s+Eseoo/EEr#c t|ttttt t jfs!|t jurt|dyy)zCType check the provided literal value against the legal parameters.zK is not a valid literal value, must be one of: int, bytes, str, Enum, None.N) isinstanceintbytesstrboolrrNoneType TypeError)values r"_literal_type_checkr0^sM usE3dN>> type MyAlias = Literal[1, 2] >>> list(get_literal_values(Literal[MyAlias, 3], unpack_type_aliases="skip")) [MyAlias, 3] ``` - `'lenient'`: Try to parse type aliases, and fallback to `'skip'` if the type alias can't be inspected (because of an undefined forward reference). - `'eager'`: Parse type aliases and raise any encountered [`NameError`][] exceptions (the default): ```pycon >>> type MyAlias = Literal[1, 2] >>> list(get_literal_values(Literal[MyAlias, 3], unpack_type_aliases="eager")) [1, 2, 3] ``` Note: While `None` is [equivalent to][none] `type(None)`, the runtime implementation of [`Literal`][typing.Literal] does not de-duplicate them. This function makes sure this de-duplication is applied: ```pycon >>> list(get_literal_values(Literal[NoneType, None])) [None] ``` Example: ```pycon >>> type Ints = Literal[1, 2] >>> list(get_literal_values(Literal[1, Ints], unpack_type_alias="skip")) ["a", Ints] >>> list(get_literal_values(Literal[1, Ints])) [1, 2] >>> list(get_literal_values(Literal[1.0], type_check=True)) Traceback (most recent call last): ... TypeError: 1.0 is not a valid literal value, must be one of: int, bytes, str, Enum, None. ``` skipFNTr2c36K|]}|t|fywN)type).0as r" z%get_literal_values..s*JAAtAw<*Jsr1c3&K|] \}}| ywr8r:p_s r"r<z%get_literal_values..s*da*c3&K|] \}}| ywr8r>r?s r"r<z%get_literal_values..s6da6rB)__args__r0rr-is_typealiastype __value__rextend NameErrorappendr9dictfromkeysr.) annotationr3r4 _has_noneargvalues_and_type alias_valuesub_argsdcts r"rrgstf$ && C#C({c^%<%<< J   8:&& =C ..s3 K"%--K 2# Pc H$***J*JJ',.111#**D.2I2I+JK#**Cc+;<5 =8 +--0C +c* * *5!=*g5!+C0#**Cc+;< =4 +  76o6 6 6 7sgA.F 1 D+=BF E, F %E*&F +8E'#F &E''F ,F FF F  F  F )required not_required read_only class_varinit_varfinalr rset[Qualifier]_all_qualifiersceZdZdZeZ eZ eZ eZ eZ eZ eZ eZ e ddZy)rzThe source of an annotation, e.g. a class or a function. Depending on the source, different [type qualifiers][type qualifier] may be (dis)allowed. cn|tjurdhS|tjurddhS|tjurhdS|tjurhdS|tj tj tjfvr tS|tjurtSt|y)zIThe allowed [type qualifiers][type qualifier] for this annotation source.rXrV>rXrWrV>rSrUrTN) rASSIGNMENT_OR_VARIABLECLASS DATACLASS TYPED_DICT NAMED_TUPLEFUNCTIONBAREsetANYrZrselfs r"allowed_qualifiersz#AnnotationSource.allowed_qualifiers<s #:: :9  %++ +[) ) %// /5 5 %00 0< < &224D4M4MO_OdOde e5L %)) )" "  r#N)returnrY)__name__ __module__ __qualname____doc__rr]r^r_r`rarbrercpropertyrhr>r#r"rrs "V FE I J &K vH &C 6D r#rc&eZdZUdZded< ddZy)rz-The provided [type qualifier][] is forbidden.r qualifierc||_yr8)rp)rgrps r"__init__zForbiddenQualifier.__init__Vs "r#N)rprriNone)rjrkrlrm__annotations__rrr>r#r"rrPs7"#r#rc*eZdZeZddZddZy)_UnknownTypeEnumcy)NUNKNOWNr>rfs r"__str__z_UnknownTypeEnum.__str__]sr#cy)Nz r>rfs r"__repr__z_UnknownTypeEnum.__repr__`sr#N)rir+)rjrkrlrrxryr{r>r#r"rvrvZsfGr#rv _UnkownTypec4eZdZUdZded< ded< ded<y) rz'The result of the inspected annotation.zAny | _UnkownTyper9rY qualifiersz list[Any]metadataN)rjrkrlrmrtr>r#r"rrks$1  J!r#rr6r4c:|j}t}g} t||\}}|r||z}t|}|\t j |r1d|vr t d|jd|jd}nbt j|r1d|vr t d|jd|jd}nt j|r0d|vr t d|jd|jd}nt j|r0d|vr t d|jd|jd}nt j|r0d|vr t d|jd|jd}nMnNt|tr;d|vr t d|jdtt |j"}nnt j|r'd|vr t d|jdt$}njt j |r'd|vr t d|jdt$}n.|tur&d|vr t d|jdt$}t'|||S) a Inspect an [annotation expression][], extracting any [type qualifier][] and metadata. An [annotation expression][] is a [type expression][] optionally surrounded by one or more [type qualifiers][type qualifier] or by [`Annotated`][typing.Annotated]. This function will: - Unwrap the type expression, keeping track of the type qualifiers. - Unwrap [`Annotated`][typing.Annotated] forms, keeping track of the annotated metadata. Args: annotation: The annotation expression to be inspected. annotation_source: The source of the annotation. Depending on the source (e.g. a class), different type qualifiers may be (dis)allowed. To allow any type qualifier, use [`AnnotationSource.ANY`][typing_inspection.introspection.AnnotationSource.ANY]. unpack_type_aliases: What to do when encountering [PEP 695](https://peps.python.org/pep-0695/) [type aliases][type-aliases]. Can be one of: - `'skip'`: Do not try to parse type aliases (the default): ```pycon >>> type MyInt = Annotated[int, 'meta'] >>> inspect_annotation(MyInt, annotation_source=AnnotationSource.BARE, unpack_type_aliases='skip') InspectedAnnotation(type=MyInt, qualifiers={}, metadata=[]) ``` - `'lenient'`: Try to parse type aliases, and fallback to `'skip'` if the type alias can't be inspected (because of an undefined forward reference): ```pycon >>> type MyInt = Annotated[Undefined, 'meta'] >>> inspect_annotation(MyInt, annotation_source=AnnotationSource.BARE, unpack_type_aliases='lenient') InspectedAnnotation(type=MyInt, qualifiers={}, metadata=[]) >>> Undefined = int >>> inspect_annotation(MyInt, annotation_source=AnnotationSource.BARE, unpack_type_aliases='lenient') InspectedAnnotation(type=int, qualifiers={}, metadata=['meta']) ``` - `'eager'`: Parse type aliases and raise any encountered [`NameError`][] exceptions. Returns: The result of the inspected annotation, where the type expression, used qualifiers and metadata is stored. Example: ```pycon >>> inspect_annotation( ... Final[Annotated[ClassVar[Annotated[int, 'meta_1']], 'meta_2']], ... annotation_source=AnnotationSource.CLASS, ... ) ... InspectedAnnotation(type=int, qualifiers={'class_var', 'final'}, metadata=['meta_1', 'meta_2']) ``` rrVrrXrSrTrUrW)rhrd_unpack_annotatedrr is_classvarraddrDis_final is_requiredis_notrequired is_readonlyr(rr r r9rxr)rLannotation_sourcer4rhr~r_metaorigins r"rrsup+==!$JH -jNab E x'H J'  ))&1&88,[99{+'003 ((0"44,W55w''003 ++F3%77,Z88z*'003 ..v6!);;,^<<~.'003 ++F3&88,^<<{+'003   G ,!33(44 NN: &c:??3J U Zz* , ,$W- -w  # #J / 0 0$[1 1{# w  / /$Z0 0z" z:x @@r#cVt|}|rPtj|r;|j}t |j }t ||d\}}||z}||fStj|r( |j}t ||d\}}|r||fS|gfStj|r8 |j} ||j}t ||d\}}|r||fS|gfS|gfS#t$r |dk(rY|gfSwxYw#t$rYBwxYw#t$r |dk(rY|gfSwxYw)NFr4check_annotatedTr1) rr is_annotated __origin__list __metadata___unpack_annotated_innerrErFrHrDr.) rLr4rrannotated_typersub_metar/typs r"rrs #F>66v>#.. //0 $; 0CUZ$  h&x''  ( ( 4 "((E 4+>PTMCH}$r> !  ( ( 0 "$$E j112 4+>PTMCH}$r> ! r>Y "g-.V r>Y B   "g-.0 r>3 s64 C,1 D>D,DD DDD('D(c|dk(rCtjt|r!|jt |j fS|gfSt ||dS)Nr6Tr)rrrrrrr)rLr4s r"rrBsVf$  & &z*'= >(($z/F/F*GG Gr> ! ":CVhl mmr#)r!r rir,)r/r rirs)rLr r3r,r4#Literal['skip', 'lenient', 'eager']rizGenerator[Any])rLr rrr4rrir)rLr r4zLiteral['lenient', 'eager']rr,rituple[Any, list[Any]])rLr r4rrir),rm __future__rsysr%collections.abcr dataclassesrenumrrrtypingr r r r typing_extensionsr rrrr__all__ version_inforr0rrrtrdrZr Exceptionrrvrxr|rrrrr>r#r"rsK" %$$11KK w#"2"2W"<,DF@o?F m+m+ m+ = m+  m+`hi 9i"%hy&9":: nwnb##t  " "C !1!9!9: Y:Z"*":@F yAyA( yA = yA  yAx??*E?X\??HW^ n n0S n nr#