
    ÁjX                    B   U d Z ddlmZ ddlZddlZddl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mZ ddlmZmZmZ d	d
lmZ dZej4                  dk\  rd'dZnd'dZeez  ez  ez  e
z  ej@                  z  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+jX                  Z,	 ee+jX                     Z-de%d<   	  G d  d!e      Z.d"d#	 	 	 	 	 	 	 d*d$Z/	 	 	 	 	 	 	 	 d+d%Z0dd#	 	 	 	 	 d,d&Z1y)-zEHigh-level introspection utilities, used to inspect type annotations.    )annotationsN)	Generator)InitVar)EnumIntEnumauto)AnyLiteral
NamedTuple	TypeAliascast)assert_neverget_args
get_origin   )typing_objects)AnnotationSourceForbiddenQualifierInspectedAnnotation	Qualifierget_literal_valuesinspect_annotationis_union_origin)      c               ,    t        j                  |       S a  Return 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[<t1>, <t2>, ...]` and `<t1> | <t2> | ...` 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):
                ...
            ```
        )r   is_unionobjs    F/tmp/pip-target-k_scjv0h/lib/python/typing_inspection/introspection.pyr   r      s    : &&s++    c               T    t        j                  |       xs | t        j                  u S r   )r   r   types	UnionTyper   s    r!   r   r   >   s#    : &&s+Eseoo/EEr"   c               f    t        | t              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_literal_allowed_typesr   NoneType	TypeError)values    r!   _literal_type_checkr,   a   s6    e34nF]F]9]5'!lmnn :^4r"   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.
        ```
    skipFNTr.   c              3  6   K   | ]  }|t        |      f  y wN)type).0as     r!   	<genexpr>z%get_literal_values.<locals>.<genexpr>   s     *JAAtAw<*Js   r-   c              3  &   K   | ]	  \  }}|  y wr4    r6   p_s      r!   r8   z%get_literal_values.<locals>.<genexpr>   s     *da*   c              3  &   K   | ]	  \  }}|  y wr4   r:   r;   s      r!   r8   z%get_literal_values.<locals>.<genexpr>   s     6da6r>   )__args__r,   r   r)   is_typealiastype	__value__r   extend	NameErrorappendr5   dictfromkeysr*   )	
annotationr/   r0   	_has_noneargvalues_and_typealias_valuesub_argsdcts	            r!   r   r   g   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.rT   rR   >   rT   rS   rR   >   rO   rQ   rP   N)r   ASSIGNMENT_OR_VARIABLECLASS	DATACLASS
TYPED_DICTNAMED_TUPLEFUNCTIONBAREsetANYrV   r   selfs    r!   allowed_qualifiersz#AnnotationSource.allowed_qualifiers<  s     #:::9%+++[))%///55%000<<&224D4M4MO_OdOdee5L%)))""r"   N)returnrU   )__name__
__module____qualname____doc__r   rY   rZ   r[   r\   r]   r^   ra   r_   propertyrd   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 r4   )rl   )rc   rl   s     r!   __init__zForbiddenQualifier.__init__V  s	    "r"   N)rl   r   re   None)rf   rg   rh   ri   __annotations__rn   r:   r"   r!   r   r   P  s    7"#r"   r   c                  *    e Zd Z e       ZddZddZy)_UnknownTypeEnumc                     y)NUNKNOWNr:   rb   s    r!   __str__z_UnknownTypeEnum.__str__]  s    r"   c                     y)Nz	<UNKNOWN>r:   rb   s    r!   __repr__z_UnknownTypeEnum.__repr__`  s    r"   N)re   str)rf   rg   rh   r   rt   ru   rw   r:   r"   r!   rr   rr   Z  s    fGr"   rr   _UnknownTypec                  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 | _UnknownTyper5   rU   
qualifiersz	list[Any]metadataN)rf   rg   rh   ri   rp   r:   r"   r!   r   r   k  s$    1
 J!r"   r   r2   r0   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}   rR   r   rT   rO   rP   rQ   rS   )rd   r`   _unpack_annotatedr   r   is_classvarr   addr@   is_finalis_requiredis_notrequiredis_readonlyr'   r   r   r	   r5   rt   r   )rH   annotation_sourcer0   rd   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r0   check_annotatedTr-   )r   r   is_annotated
__origin__list__metadata___unpack_annotated_innerrA   rB   rD   r@   r*   )	rH   r0   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 )Nr2   Tr   )r   r   r   r   r   r   r   )rH   r0   s     r!   r   r   B  sV     f$&&z*'=>(($z/F/F*GGGr>!":CVhlmmr"   )r    r	   re   bool)r+   r	   re   ro   )rH   r	   r/   r   r0   #Literal['skip', 'lenient', 'eager']re   zGenerator[Any])rH   r	   r   r   r0   r   re   r   )rH   r	   r0   zLiteral['lenient', 'eager']r   r   re   tuple[Any, list[Any]])rH   r	   r0   r   re   r   )2ri   
__future__r   sysr$   collections.abcr   dataclassesr   enumr   r   r   typingr	   r
   r   r   r   typing_extensionsr   r   r    r   __all__version_infor   intbytesrx   r   r)   r(   r,   r   r   rp   r`   rV   r   	Exceptionr   rr   rt   ry   r   r   r   r   r:   r"   r!   <module>r      s   K " 
  %  $ $ < < @ @  w,DF@ us*T1D8>;R;RR o ?Fm+m+ 	m+
 =m+ m+` hi	9 i "%hy&9": :
nw nb# #t  
"
" C!"2":":;i ; Z"* ": @FyAyA (	yA
 =yA yAx??*E?X\??H W^	n	n0S	n	nr"   