o
    T�¿jX  ã                   @  s”  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jdkrRd<dd„Znd<dd„ZeeB eB eB e
B ej B Z!d=dd„Z"dddœd>d d!„Z#ed" Z$d#e%d$< 	 e&ee$ƒƒZ'd%e%d&< G d'd(„ d(eƒZ(G d)d*„ d*e)ƒZ*G d+d,„ d,e
ƒZ+e+j,Z,	 ee+j, Z-d#e%d-< 	 G d.d/„ d/eƒZ.d0d1œd?d3d4„Z/d@d8d9„Z0dd1œdAd:d;„Z1dS )BzEHigh-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)é   é   Úobjr   ÚreturnÚboolc                C  s
   t  | ¡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©r   © r!   úZ/home/dinkstrade/pdmp/venv/lib/python3.10/site-packages/typing_inspection/introspection.pyr      s   
r   c                C  s   t  | ¡p	| tju S r   )r   r   ÚtypesÚ	UnionTyper    r!   r!   r"   r   >   s   ÚvalueÚNonec                C  s*   t | tƒ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Ú_literal_allowed_typesr   ÚNoneTypeÚ	TypeError)r%   r!   r!   r"   Ú_literal_type_checka   s   ÿr+   FÚeager©Ú
type_checkÚunpack_type_aliasesÚ
annotationr.   r/   ú#Literal['skip', 'lenient', 'eager']úGenerator[Any]c         	   	   c  s`  � |dkr)d}| j D ]}|rt|ƒ |du s|tju r#|s dV  d}q
|V  q
dS g }| j D ]Y}t |¡rjz|j}W n tyW   |dkrF‚ |rLt|ƒ | |t|ƒf¡ Y q.w t	|||d�}| 
dd„ |D ƒ¡ q.|rpt|ƒ |tju r~| dtjf¡ q.| |t|ƒf¡ q.zt |¡}W n ty£   d	d„ |D ƒE dH  Y dS w d
d„ |D ƒE dH  dS )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,   r-   c                 s  s   � | ]	}|t |ƒfV  qd S ©N)Útype)Ú.0Úar!   r!   r"   Ú	<genexpr>Å   s   € z%get_literal_values.<locals>.<genexpr>c                 s  ó   � | ]\}}|V  qd S r4   r!   ©r6   ÚpÚ_r!   r!   r"   r8   Ò   ó   € c                 s  r9   r4   r!   r:   r!   r!   r"   r8   Ô   r=   )Ú__args__r+   r   r)   Úis_typealiastypeÚ	__value__Ú	NameErrorÚappendr5   r   ÚextendÚdictÚfromkeysr*   )	r0   r.   r/   Ú	_has_noneÚargÚvalues_and_typeÚalias_valueÚsub_argsÚdctr!   r!   r"   r   g   sN   €:
ø


úÿ
þr   )ÚrequiredÚnot_requiredÚ	read_onlyÚ	class_varÚinit_varÚfinalr   r   úset[Qualifier]Ú_all_qualifiersc                   @  s^   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d„ƒZdS )r   zœThe 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   rR   c                 C  sz   | 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 r0tƒ S | t j	u r7t
S t| ƒ dS )zIThe allowed [type qualifiers][type qualifier] for this annotation source.rQ   rO   >   rQ   rP   rO   >   rL   rN   rM   N)r   ÚASSIGNMENT_OR_VARIABLEÚCLASSÚ	DATACLASSÚ
TYPED_DICTÚNAMED_TUPLEÚFUNCTIONÚBAREÚsetÚANYrS   r   ©Úselfr!   r!   r"   Úallowed_qualifiers<  s   




z#AnnotationSource.allowed_qualifiersN)r   rR   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rT   rU   rV   rW   rX   rY   r\   rZ   Úpropertyr_   r!   r!   r!   r"   r   ß   s(    

r   c                   @  s&   e Zd ZU dZded< 	 d	dd„ZdS )
r   z-The provided [type qualifier][] is forbidden.r   Ú	qualifierr   r&   c                C  s
   || _ d S r4   )re   )r^   re   r!   r!   r"   Ú__init__V  s   
zForbiddenQualifier.__init__N)re   r   r   r&   )r`   ra   rb   rc   Ú__annotations__rf   r!   r!   r!   r"   r   P  s
   
 r   c                   @  s&   e Zd Zeƒ Zddd„Zddd„ZdS )	Ú_UnknownTypeEnumr   Ústrc                 C  ó   dS )NÚUNKNOWNr!   r]   r!   r!   r"   Ú__str__]  ó   z_UnknownTypeEnum.__str__c                 C  rj   )Nz	<UNKNOWN>r!   r]   r!   r!   r"   Ú__repr__`  rm   z_UnknownTypeEnum.__repr__N)r   ri   )r`   ra   rb   r   rk   rl   rn   r!   r!   r!   r"   rh   Z  s    
rh   Ú_UnknownTypec                   @  s.   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 | _UnknownTyper5   rR   Ú
qualifiersz	list[Any]ÚmetadataN)r`   ra   rb   rc   rg   r!   r!   r!   r"   r   k  s   
 r   r3   ©r/   Úannotation_sourcec               C  sî  |j }tƒ }g }	 t| |d�\} }|r|| }qt| ƒ}|dur™t |¡r8d|vr-tdƒ‚| d¡ | jd } n{t 	|¡rPd|vrEtdƒ‚| d¡ | jd } nct 
|¡rhd|vr]tdƒ‚| d¡ | jd } nKt |¡r€d|vrutdƒ‚| d¡ | jd } n3t |¡r˜d	|vr�tdƒ‚| d	¡ | jd } nnt| tƒr²d
|vr¦td
ƒ‚| d
¡ tt| jƒ} nnq	t 	| ¡rÉd|vrÁtdƒ‚| d¡ t} n(t | ¡rÞd|vrÖtdƒ‚| d¡ t} n| 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'])
        ```
    Trr   NrO   r   rQ   rL   rM   rN   rP   )r_   r[   Ú_unpack_annotatedr   r   Úis_classvarr   Úaddr>   Úis_finalÚis_requiredÚis_notrequiredÚis_readonlyr'   r   r   r   r5   rk   r   )r0   rs   r/   r_   rp   rq   Ú_metaÚoriginr!   r!   r"   r   ƒ  sv   8











Ö
-



r   úLiteral['lenient', 'eager']Úcheck_annotatedútuple[Any, list[Any]]c           	      C  s.  t | ƒ}|r$t |¡r$| j}t| jƒ}t||dd�\}}|| }||fS t | ¡rSz| j}W n t	y?   |dkr:‚ Y | g fS w t||dd�\}}|rO||fS | g fS t |¡r“z|j}W n t	yn   |dkri‚ Y | g fS w z|| j
 }W n	 ty   Y nw t||dd�\}}|r�||fS | g fS | g fS )NF©r/   r~   r,   T)r   r   Úis_annotatedÚ
__origin__ÚlistÚ__metadata__Ú_unpack_annotated_innerr?   r@   rA   r>   r*   )	r0   r/   r~   r|   Úannotated_typerq   Úsub_metar%   Útypr!   r!   r"   r…   ÿ  sZ   

ÿ

ÿ+Ô
ÿ

ÿç	ý
ÿr…   c               C  s<   |dkrt  t| ƒ¡r| jt| jƒfS | g fS t| |dd�S )Nr3   Tr€   )r   r�   r   r‚   rƒ   r„   r…   )r0   r/   r!   r!   r"   rt   B  s
   rt   )r   r   r   r   )r%   r   r   r&   )r0   r   r.   r   r/   r1   r   r2   )r0   r   rs   r   r/   r1   r   r   )r0   r   r/   r}   r~   r   r   r   )r0   r   r/   r1   r   r   )2rc   Ú
__future__r   Úsysr#   Úcollections.abcr   Údataclassesr   Úenumr   r   r   Útypingr   r	   r
   r   r   Útyping_extensionsr   r   r   Ú r   Ú__all__Úversion_infor   ÚintÚbytesri   r   r)   r(   r+   r   r   rg   r[   rS   r   Ú	Exceptionr   rh   rk   ro   r   r   r…   rt   r!   r!   r!   r"   Ú<module>   sF    


" 

ûpq

û
|Dÿ