o
    æûõjîZ  ã                   @  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"dAdd„Z#dddœdBd#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&d0< 	 G d1d2„ d2eƒZ/d3d4œdCd6d7„Z0dDd;d<„Z1dd4œdEd=d>„Z2dS )FzEHigh-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!   úR/opt/steambot/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+   Úaliasc                C  s   | j }t |¡rt|ƒS |S ©N)Ú	__value__r   Úis_typealiastypeÚ_unpack_type_alias_chain)r,   r%   r!   r!   r"   r0   g   s   r0   FÚeager©Ú
type_checkÚunpack_type_aliasesÚ
annotationr3   r4   ú#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 | j D ]}|tju s8t |¡r: nq,|rG| j D ]}t|ƒ q@| j E dH  dS g }| j D ]Z}t |¡r‘zt|ƒ}W n ty~   |dkrm‚ |rst|ƒ | |t|ƒf¡ Y qTw t	|||d�}| 
dd„ |D ƒ¡ qT|r—t|ƒ |tju r¥| dtjf¡ qT| |t|ƒf¡ qTz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.
        ```
    ÚskipFNTr1   r2   c                 s  s   � | ]	}|t |ƒfV  qd S r-   )Útype)Ú.0Úar!   r!   r"   Ú	<genexpr>Ö   s   € z%get_literal_values.<locals>.<genexpr>c                 s  ó   � | ]\}}|V  qd S r-   r!   ©r:   ÚpÚ_r!   r!   r"   r<   ã   ó   € c                 s  r=   r-   r!   r>   r!   r!   r"   r<   å   rA   )Ú__args__r+   r   r)   r/   r0   Ú	NameErrorÚappendr9   r   ÚextendÚdictÚfromkeysr*   )	r5   r3   r4   Ú	_has_noneÚargÚvalues_and_typeÚalias_valueÚsub_argsÚdctr!   r!   r"   r   l   s`   €:
ø

ÿ



úÿ
þ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   rT   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.rS   rQ   >   rS   rR   rQ   >   rN   rP   rO   N)r   ÚASSIGNMENT_OR_VARIABLEÚCLASSÚ	DATACLASSÚ
TYPED_DICTÚNAMED_TUPLEÚFUNCTIONÚBAREÚsetÚANYrU   r   ©Úselfr!   r!   r"   Úallowed_qualifiersM  s   




z#AnnotationSource.allowed_qualifiersN)r   rT   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rV   rW   rX   rY   rZ   r[   r^   r\   Úpropertyra   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 r-   )rg   )r`   rg   r!   r!   r"   Ú__init__g  s   
zForbiddenQualifier.__init__N)rg   r   r   r&   )rb   rc   rd   re   Ú__annotations__rh   r!   r!   r!   r"   r   a  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__n  ó   z_UnknownTypeEnum.__str__c                 C  rl   )Nz	<UNKNOWN>r!   r_   r!   r!   r"   Ú__repr__q  ro   z_UnknownTypeEnum.__repr__N)r   rk   )rb   rc   rd   r   rm   rn   rp   r!   r!   r!   r"   rj   k  s    
rj   Ú_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 | _UnknownTyper9   rT   Ú
qualifiersz	list[Any]ÚmetadataN)rb   rc   rd   re   ri   r!   r!   r!   r"   r   |  s   
 r   r8   ©r4   Ú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'])
        ```
    Trt   NrQ   r   rS   rN   rO   rP   rR   )ra   r]   Ú_unpack_annotatedr   r   Úis_classvarr   ÚaddrB   Úis_finalÚis_requiredÚis_notrequiredÚis_readonlyr'   r   r   r   r9   rm   r   )r5   ru   r4   ra   rr   rs   Ú_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©r4   r€   r1   T)r   r   Úis_annotatedÚ
__origin__ÚlistÚ__metadata__Ú_unpack_annotated_innerr/   r.   rC   rB   r*   )	r5   r4   r€   r~   Úannotated_typers   Ú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 )Nr8   Tr‚   )r   rƒ   r   r„   r…   r†   r‡   )r5   r4   r!   r!   r"   rv   S  s
   rv   )r   r   r   r   )r%   r   r   r&   )r,   r   r   r   )r5   r   r3   r   r4   r6   r   r7   )r5   r   ru   r   r4   r6   r   r   )r5   r   r4   r   r€   r   r   r�   )r5   r   r4   r6   r   r�   )3re   Ú
__future__r   Úsysr#   Úcollections.abcr   Údataclassesr   Úenumr   r   r   Útypingr   r	   r
   r   r   Útyping_extensionsr   r   r   Ú r   Ú__all__Úversion_infor   ÚintÚbytesrk   r   r)   r(   r+   r0   r   r   ri   r]   rU   r   Ú	Exceptionr   rj   rm   rq   r   r   r‡   rv   r!   r!   r!   r"   Ú<module>   sH    

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