
    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j        dk    rd6dZnd6dZd7dZdddd8d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&j'        Z'	 ee&j'                 Z(d e d*<   	  G d+ d,e          Z)d-d.d9d0Z*d:d4Z+dd.d;d5Z,dS )<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)   
   objr
   returnboolc               F    t          j        |           p| 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r   s    a/var/www/html/prod/cognitive/venv/lib/python3.11/site-packages/typing_inspection/introspection.pyr   r      s!     &s++Eseo/EE    c               *    t          j        |           S r!   )r   r"   r%   s    r&   r   r   *   s     &s+++r'   valueNonec          	         t          | t          t          t          t          t
          t          j        f          s | t          j        urt          |  d          dS dS )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strr   r   r   NoneType	TypeError)r)   s    r&   _literal_type_checkr2   7   s^     usE3dN<STUUo0005mmmnnn	o o00r'   Feager
type_checkunpack_type_aliases
annotationr5   r6   #Literal['skip', 'lenient', 'eager']Generator[Any]c            #  H  K   |dk    r<d}| j         D ]0}|rt          |           ||t          j        u r	|sdV  d},|V  1dS g }| j         D ]}t          j        |          r	 |j        }t          |||          }|                    d |D                        Q# t          $ r? |dk    r |rt          |           |	                    |t          |          f           Y w xY w|rt          |           |t          j        u r"|	                    dt          j        f           |	                    |t          |          f           	 t                              |          }d |D             E d{V  dS # t          $ r d	 |D             E d{V  Y dS w xY 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.
        ```
    skipFNTr4   c              3  8   K   | ]}|t          |          fV  d S N)type).0as     r&   	<genexpr>z%get_literal_values.<locals>.<genexpr>   s,      *J*JAAtAww<*J*J*J*J*J*Jr'   r3   c              3      K   | ]	\  }}|V  
d S r=    r?   p_s      r&   rA   z%get_literal_values.<locals>.<genexpr>   s&      **da******r'   c              3      K   | ]	\  }}|V  
d S r=   rC   rD   s      r&   rA   z%get_literal_values.<locals>.<genexpr>   s&      66da666666r'   )__args__r2   r   r0   is_typealiastype	__value__r   extend	NameErrorappendr>   dictfromkeysr1   )	r7   r5   r6   	_has_noneargvalues_and_typealias_valuesub_argsdcts	            r&   r   r   @   s`     t f$$	 & 	 	C )#C((({c^%<<<  JJJ 							 	 8:& 	= 	=C
 .s33 =K"%-K  2#
Pc     H $***J*J*J*J*JJJJJ ! = = =*g55! 1+C000#**Cc+;<<<<<=  -',,,.111#**D.2I+JKKKK#**Cc+;<<<<	+--00C
 +*c************	  	7 	7 	766o6666666666666	7s%   &BAC('C(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dS )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.
    r   r\   c                B   | 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          |            dS )zIThe allowed [type qualifiers][type qualifier] for this annotation source.r[   rY   >   r[   rZ   rY   >   rV   rX   rW   N)r   ASSIGNMENT_OR_VARIABLECLASS	DATACLASS
TYPED_DICTNAMED_TUPLEFUNCTIONBAREsetANYr]   r   selfs    r&   allowed_qualifiersz#AnnotationSource.allowed_qualifiers  s     #:::9%+++[))%///5555%000<<<<&24D4MO_Odeee55L%)))""r'   N)r   r\   )__name__
__module____qualname____doc__r	   r`   ra   rb   rc   rd   re   rh   rf   propertyrk   rC   r'   r&   r   r      s         
 "TVV DFFE	 I
 J
 $&&K	 tvvH $&&C
 466D
    X  r'   r   c                  (    e Zd ZU dZded<   	 ddZdS )	r   z-The provided [type qualifier][] is forbidden.r   	qualifierr   r*   c                   || _         d S r=   )rr   )rj   rr   s     r&   __init__zForbiddenQualifier.__init__/  s    "r'   N)rr   r   r   r*   )rl   rm   rn   ro   __annotations__rt   rC   r'   r&   r   r   )  s?         77"# # # # # #r'   r   c                  2    e Zd Z e            ZddZddZdS )_UnknownTypeEnumr   r/   c                    dS )NUNKNOWNrC   ri   s    r&   __str__z_UnknownTypeEnum.__str__6  s    yr'   c                    dS )Nz	<UNKNOWN>rC   ri   s    r&   __repr__z_UnknownTypeEnum.__repr__9  s    {r'   N)r   r/   )rl   rm   rn   r	   ry   rz   r|   rC   r'   r&   rw   rw   3  sG        dffG        r'   rw   _UnkownTypec                  6    e Zd ZU dZded<   	 ded<   	 ded<   dS )	r   z'The result of the inspected annotation.zAny | _UnkownTyper>   r\   
qualifierszSequence[Any]metadataN)rl   rm   rn   ro   ru   rC   r'   r&   r   r   D  sF         11 J!!r'   r   r;   r6   annotation_sourcec                 |j         }t                      }g }	 t          | |          \  } }|r||z   }t          |           }|ut	          j        |          r7d|vrt          d          |                    d           | j        d         } nt	          j	        |          r7d|vrt          d          |                    d           | j        d         } n8t	          j
        |          r6d|vrt          d          |                    d           | j        d         } nt	          j        |          r6d|vrt          d          |                    d           | j        d         } nt	          j        |          r6d	|vrt          d          |                    d	           | j        d         } nZn[t          | t                    rCd
|vrt          d
          |                    d
           t          t           | j                  } nnt	          j	        |           r0d|vrt          d          |                    d           t$          } n|t	          j        |           r0d|vrt          d          |                    d           t$          } n8| t          u r/d
|vrt          d
          |                    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'])
        ```
    Tr   NrY   r   r[   rV   rW   rX   rZ   )rk   rg   _unpack_annotatedr   r   is_classvarr   addrH   is_finalis_requiredis_notrequiredis_readonlyr,   r   r   r
   r>   ry   r   )r7   r   r6   rk   r   r   _metaorigins           r&   r   r   \  s#   p +=!$JH*-jNabbb
E 	x'HJ'')&11 &888,[999{+++'03

(00 "444,W555w''''03

+F33 %777,Z888z***'03

.v66 !);;;,^<<<~...'03

+F33 &888,^<<<{+++'03

 
G,, 	!333(444NN:&&&c:?33JJU*Z z** ,,,$W---w

		#J	/	/ 	000$[111{###

	w		///$Z000z"""
z:x@@@r'   Literal['lenient', 'eager']check_annotatedtuple[Any, list[Any]]c                L   t          |           }|rMt          j        |          r9| j        }t	          | j                  }t          ||d          \  }}||z   }||fS t          j        |           r>	 | j        }t          ||d          \  }}|r||fS | g fS # t          $ r
 |dk    r Y nuw xY wt          j        |          r]	 |j        }	 || j
                 }n# t          $ r Y nw xY wt          ||d          \  }}|r||fS | g fS # t          $ r
 |dk    r Y nw xY w| g fS )NFr6   r   Tr3   )r   r   is_annotated
__origin__list__metadata___unpack_annotated_innerrI   rJ   rL   rH   r1   )	r7   r6   r   r   annotated_typer   sub_metar)   typs	            r&   r   r     s    
##F 9">6v>> 9"#.
/00
 $;0CUZ$
 $
 $
  h&x''		(	4	4 -"	"(E
 4+>PT  MC  % H}$r>!  	 	 	"g-- .-	 
	(	0	0 "	"$E j12     4+>PT  MC  %H}$r>!/  	 	 	"g-- .-	2 r>s6   4B B.-B.D C 
C)(C)DDc                  |dk    r@t          j        t          |                     r| j        t	          | j                  fS | g fS t          | |d          S )Nr;   Tr   )r   r   r   r   r   r   r   )r7   r6   s     r&   r   r     sd     f$$&z*'='=>> 	"($z/F*G*GGGr>!":CVhlmmmmr'   )r   r
   r   r   )r)   r
   r   r*   )r7   r
   r5   r   r6   r8   r   r9   )r7   r
   r   r   r6   r8   r   r   )r7   r
   r6   r   r   r   r   r   )r7   r
   r6   r8   r   r   )-ro   
__future__r   sysr#   collections.abcr   r   dataclassesr   enumr   r   r	   typingr
   r   r   r   typing_extensionsr   r   r   r    r   __all__version_infor   r2   r   r   ru   rg   r]   r   	Exceptionr   rw   ry   r}   r   r   r   r   rC   r'   r&   <module>r      s   K K K " " " " " " 



  / / / / / / / /       $ $ $ $ $ $ $ $ $ $ 1 1 1 1 1 1 1 1 1 1 1 1 K K K K K K K K K K K K       w
F 
F 
F 
F 
F
, 
, 
, 
,o o o o ?Fm+ m+ m+ m+ m+ m+` hi	 i i i i "%#hhy&9&9":": : : : :
n n n n nw n n nb# # # # # # # #    t    
" C !1!9: : : : : Z" " " " "* " " ": @FyA yA yA yA yA yAx? ? ? ?H W^	n 	n 	n 	n 	n 	n 	n 	nr'   