
    IhQ                       U d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZmZmZ ddlmZmZmZmZ ddlmZmZmZmZ d	d
lmZ dZej6                  dk\  rd'dZnd'dZd(dZddd	 	 	 	 	 	 	 d)dZed   Zde d<   	  e! ee            Z"de d<    G d de      Z# G d de$      Z% G d de      Z&e&jN                  Z'	 ee&jN                     Z(de d<   	  G d  d!e      Z)d"d#	 	 	 	 	 	 	 d*d$Z*	 	 	 	 	 	 	 	 d+d%Z+dd#	 	 	 	 	 d,d&Z,y)-zEHigh-level introspection utilities, used to inspect type annotations.    )annotationsN)	GeneratorSequence)InitVar)EnumIntEnumauto)AnyLiteral
NamedTuplecast)	TypeAliasassert_neverget_args
get_origin   )typing_objects)AnnotationSourceForbiddenQualifierInspectedAnnotation	Qualifierget_literal_valuesinspect_annotationis_union_origin)   
   c               T    t        j                  |       xs | t        j                  u S zReturn whether the provided origin is the union form.

        ```pycon
        >>> is_union_origin(typing.Union)
        True
        >>> is_union_origin(get_origin(int | str))
        True
        ```
        )r   is_uniontypes	UnionTypeobjs    S/var/www/test/myenv/lib/python3.12/site-packages/typing_inspection/introspection.pyr   r      s#     &&s+Eseoo/EE    c               ,    t        j                  |       S r   )r   r   r"   s    r$   r   r   *   s     &&s++r%   c          	         t        | t        t        t        t        t
        t        j                  f      s!| t        j                  urt        |  d      yy)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)	
isinstanceintbytesstrboolr   r   NoneType	TypeError)values    r$   _literal_type_checkr0   7   sM     usE3dN<S<STU0005'!lmnn 1 Vr%   Feager
type_checkunpack_type_aliasesc            #    K   |dk(  rBd}| j                   D ]0  }|rt        |       ||t        j                  u r	|sd d}-| 2 yg }| j                   D ]  }t        j                  |      r4	 |j
                  }t        |||      }|j                  d |D               L|rt        |       |t        j                  u r"|j                  dt        j                  f       |j                  |t        |      f        	 t        j                  |      }d |D        E d{    y# t        $ r3 |dk(  r |rt        |       |j                  |t        |      f       Y w xY w7 D# t        $ r d	 |D        E d{  7   Y yw xY ww)
a=  Yield the values contained in the provided [`Literal`][typing.Literal] [special form][].

    Args:
        annotation: The [`Literal`][typing.Literal] [special form][] to unpack.
        type_check: Whether to check if the literal values are [legal parameters][literal-legal-parameters].
            Raises a [`TypeError`][] otherwise.
        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. Note that this can lead to incorrect results:
              ```pycon
              >>> 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.
        ```
    skipFNTr2   c              3  6   K   | ]  }|t        |      f  y wN)type).0as     r$   	<genexpr>z%get_literal_values.<locals>.<genexpr>   s     *JAAtAw<*Js   r1   c              3  &   K   | ]	  \  }}|  y wr8    r:   p_s      r$   r<   z%get_literal_values.<locals>.<genexpr>   s     *da*   c              3  &   K   | ]	  \  }}|  y wr8   r>   r?   s      r$   r<   z%get_literal_values.<locals>.<genexpr>   s     6da6rB   )__args__r0   r   r-   is_typealiastype	__value__r   extend	NameErrorappendr9   dictfromkeysr.   )	
annotationr3   r4   	_has_noneargvalues_and_typealias_valuesub_argsdcts	            r$   r   r   @   s    t f$	 && 	C#C({c^%<%<< J 			 8:&& 	=C
 ..s3K"%--K  2#
Pc H $***J*JJ',.111#**D.2I2I+JK#**Cc+;<5	=8	+--0C
 +c***5 ! =*g5!+C0#**Cc+;<=4 +	  	76o666	7sg   A.F1D+=BFE, F%E*&F+8E'#F&E''F,F
FF
F	F

F)requirednot_required	read_only	class_varinit_varfinalr   r   set[Qualifier]_all_qualifiersc                      e Zd ZdZ e       Z	  e       Z	  e       Z	  e       Z	  e       Z		  e       Z
	  e       Z	  e       Z	 edd       Zy)r   zThe source of an annotation, e.g. a class or a function.

    Depending on the source, different [type qualifiers][type qualifier] may be (dis)allowed.
    c                n   | t         j                  u rdhS | t         j                  u rddhS | t         j                  u rh dS | t         j                  u rh dS | t         j
                  t         j                  t         j                  fv r
t               S | t         j                  u rt        S t        |        y)zIThe allowed [type qualifiers][type qualifier] for this annotation source.rX   rV   >   rX   rW   rV   >   rS   rU   rT   N)r   ASSIGNMENT_OR_VARIABLECLASS	DATACLASS
TYPED_DICTNAMED_TUPLEFUNCTIONBAREsetANYrZ   r   selfs    r$   allowed_qualifiersz#AnnotationSource.allowed_qualifiers  s     #:::9%+++[))%///55%000<<&224D4M4MO_OdOdee5L%)))""r%   N)returnrY   )__name__
__module____qualname____doc__r	   r]   r^   r_   r`   ra   rb   re   rc   propertyrh   r>   r%   r$   r   r      s    
 "V FE	 I
 J
 &K	 vH &C
 6D
  r%   r   c                  &    e Zd ZU dZded<   	 ddZy)r   z-The provided [type qualifier][] is forbidden.r   	qualifierc                   || _         y r8   )rp   )rg   rp   s     r$   __init__zForbiddenQualifier.__init__/  s	    "r%   N)rp   r   ri   None)rj   rk   rl   rm   __annotations__rr   r>   r%   r$   r   r   )  s    7"#r%   r   c                  *    e Zd Z e       ZddZddZy)_UnknownTypeEnumc                     y)NUNKNOWNr>   rf   s    r$   __str__z_UnknownTypeEnum.__str__6  s    r%   c                     y)Nz	<UNKNOWN>r>   rf   s    r$   __repr__z_UnknownTypeEnum.__repr__9  s    r%   N)ri   r+   )rj   rk   rl   r	   rx   ry   r{   r>   r%   r$   rv   rv   3  s    fGr%   rv   _UnkownTypec                  4    e Zd ZU dZded<   	 ded<   	 ded<   y)	r   z'The result of the inspected annotation.zAny | _UnkownTyper9   rY   
qualifierszSequence[Any]metadataN)rj   rk   rl   rm   rt   r>   r%   r$   r   r   D  s$    1
 J!r%   r   r6   r4   c              :   |j                   }t               }g }	 t        | |      \  } }|r||z   }t        |       }|\t	        j
                  |      r1d|vrt        d      |j                  d       | j                  d   } nbt	        j                  |      r1d|vrt        d      |j                  d       | j                  d   } nt	        j                  |      r0d|vrt        d      |j                  d       | j                  d   } nt	        j                  |      r0d|vrt        d      |j                  d       | j                  d   } nt	        j                  |      r0d|vrt        d      |j                  d       | j                  d   } nMnNt        | t              r;d|vrt        d      |j                  d       t        t         | j"                        } nnt	        j                  |       r'd|vrt        d      |j                  d       t$        } njt	        j
                  |       r'd|vrt        d      |j                  d       t$        } n.| t        u r&d|vrt        d      |j                  d       t$        } 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'])
        ```
    r   rV   r   rX   rS   rT   rU   rW   )rh   rd   _unpack_annotatedr   r   is_classvarr   addrD   is_finalis_requiredis_notrequiredis_readonlyr(   r   r   r
   r9   rx   r   )rL   annotation_sourcer4   rh   r~   r   _metaorigins           r$   r   r   \  su   p +==!$JH
-jNab
Ex'HJ'))&1&88,[99{+'003
((0"44,W55w''003
++F3%77,Z88z*'003
..v6!);;,^<<~.'003
++F3&88,^<<{+'003
 
G,!33(44NN:&c:??3JU Z z*,,$W--w
		#	#J	/00$[11{#
	w	//$Z00z"
z:x@@r%   c                V   t        |       }|rPt        j                  |      r;| j                  }t	        | j
                        }t        ||d      \  }}||z   }||fS t        j                  |       r(	 | j                  }t        ||d      \  }}|r||fS | g fS t        j                  |      r8	 |j                  }	 || j                     }t        ||d      \  }}|r||fS | g fS | g fS # t        $ r |dk(  r Y | g fS w xY w# t        $ r Y Bw xY w# t        $ r |dk(  r Y | g fS w xY w)NFr4   check_annotatedTr1   )r   r   is_annotated
__origin__list__metadata___unpack_annotated_innerrE   rF   rH   rD   r.   )	rL   r4   r   r   annotated_typer   sub_metar/   typs	            r$   r   r     s    
#F>66v>#..
//0
 $;0CUZ$
  h&x''		(	(	4	"((E
 4+>PTMC  H}$r>!		(	(	0	"$$E j112
 4+>PTMC H}$r>!r>Y  	"g- .V r>Y	B     	"g- .0 r>3	s6   4C, 1D >D ,D D	DDD('D(c                  |dk(  rCt        j                  t        |             r!| j                  t	        | j
                        fS | g fS t        | |d      S )Nr6   Tr   )r   r   r   r   r   r   r   )rL   r4   s     r$   r   r     sV     f$&&z*'=>(($z/F/F*GGGr>!":CVhlmmr%   )r#   r
   ri   r,   )r/   r
   ri   rs   )rL   r
   r3   r,   r4   #Literal['skip', 'lenient', 'eager']ri   zGenerator[Any])rL   r
   r   r   r4   r   ri   r   )rL   r
   r4   zLiteral['lenient', 'eager']r   r,   ri   tuple[Any, list[Any]])rL   r
   r4   r   ri   r   )-rm   
__future__r   sysr    collections.abcr   r   dataclassesr   enumr   r   r	   typingr
   r   r   r   typing_extensionsr   r   r   r    r   __all__version_infor   r0   r   r   rt   rd   rZ   r   	Exceptionr   rv   rx   r|   r   r   r   r   r>   r%   r$   <module>r      s   K " 
  /  $ $ 1 1 K K  w
F
,o ?Fm+m+ 	m+
 =m+ m+` hi	9 i "%hy&9": :
nw nb# #t  
"
" C !1!9!9:Y : Z"* ": @FyAyA (	yA
 =yA yAx??*E?X\??H W^	n	n0S	n	nr%   