o
    ì6Wj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 ddlmZmZmZmZ d	d
lmZ dZejdksQejdk rWd=dd„Znd=dd„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/d0„ d0eƒZ(d1d2œd@d4d5„Z)dAd9d:„Z*dd2œdBd;d<„Z+dS )CzEHigh-level introspection utilities, used to inspect type annotations.é    )ÚannotationsN)Ú	Generator)Ú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)é   é   )r   é
   Ú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"   ún/home/esfera/Documents/content_generation/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  s8   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&   r"   r"   r#   Ú_literal_type_check^   s   ÿ
ür.   FÚeager©Ú
type_checkÚunpack_type_aliasesÚ
annotationr1   r2   ú#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/   r0   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 r7   r"   ©r9   ÚpÚ_r"   r"   r#   r;   Ò   ó   € c                 s  r<   r7   r"   r=   r"   r"   r#   r;   Ô   r@   )Ú__args__r.   r   r,   Úis_typealiastypeÚ	__value__Ú	NameErrorÚappendr8   r   ÚextendÚdictÚfromkeysr-   )	r3   r1   r2   Ú	_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   rU   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.rT   rR   >   rT   rS   rR   >   rO   rQ   rP   N)r   ÚASSIGNMENT_OR_VARIABLEÚCLASSÚ	DATACLASSÚ
TYPED_DICTÚNAMED_TUPLEÚFUNCTIONÚBAREÚsetÚANYrV   r   ©Úselfr"   r"   r#   Úallowed_qualifiers<  s   




z#AnnotationSource.allowed_qualifiersN)r   rU   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rW   rX   rY   rZ   r[   r\   r_   r]   Úpropertyrb   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 r7   )rh   )ra   rh   r"   r"   r#   Ú__init__V  s   
zForbiddenQualifier.__init__N)rh   r   r   r'   )rc   rd   re   rf   Ú__annotations__ri   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   r+   c                 C  ó   dS )NÚUNKNOWNr"   r`   r"   r"   r#   Ú__str__]  ó   z_UnknownTypeEnum.__str__c                 C  rl   )Nz	<UNKNOWN>r"   r`   r"   r"   r#   Ú__repr__`  ro   z_UnknownTypeEnum.__repr__N)r   r+   )rc   rd   re   r   rm   rn   rp   r"   r"   r"   r#   rk   Z  s    
rk   Ú_UnkownTypec                   @  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 | _UnkownTyper8   rU   Ú
qualifiersz	list[Any]ÚmetadataN)rc   rd   re   rf   rj   r"   r"   r"   r#   r   k  s   
 r   r6   ©r2   Ú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   NrR   r   rT   rO   rP   rQ   rS   )rb   r^   Ú_unpack_annotatedr   r   Úis_classvarr   ÚaddrA   Úis_finalÚis_requiredÚis_notrequiredÚis_readonlyr(   r   r   r   r8   rm   r   )r3   ru   r2   rb   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©r2   r€   r/   T)r   r   Úis_annotatedÚ
__origin__ÚlistÚ__metadata__Ú_unpack_annotated_innerrB   rC   rD   rA   r-   )	r3   r2   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 )Nr6   Tr‚   )r   rƒ   r   r„   r…   r†   r‡   )r3   r2   r"   r"   r#   rv   B  s
   rv   )r   r   r   r   )r&   r   r   r'   )r3   r   r1   r   r2   r4   r   r5   )r3   r   ru   r   r2   r4   r   r   )r3   r   r2   r   r€   r   r   r�   )r3   r   r2   r4   r   r�   ),rf   Ú
__future__r   Úsysr$   Úcollections.abcr   Údataclassesr   Úenumr   r   r   Útypingr   r	   r
   r   Útyping_extensionsr   r   r   r   Ú r   Ú__all__Úversion_infor   r.   r   r   rj   r^   rV   r   Ú	Exceptionr   rk   rm   rq   r   r   r‡   rv   r"   r"   r"   r#   Ú<module>   sD    

"
 ûpq

û
|Dÿ