o
    hQ                     @  s~  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rTd<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.d/ d/eZ)d0d1d?d3d4Z*d@d8d9Z+dd1dAd:d;Z,dS )BzEHigh-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                C  s   t | 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    r$   s/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/typing_inspection/introspection.pyr      s   
r   c                C  s
   t | S r   )r   r    r#   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_check7   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   @   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   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   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__/  s   
zForbiddenQualifier.__init__N)rh   r   r   r'   )rc   rd   re   rf   __annotations__ri   r$   r$   r$   r%   r   )  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__6     z_UnknownTypeEnum.__str__c                 C  rl   )Nz	<UNKNOWN>r$   r`   r$   r$   r%   __repr__9  ro   z_UnknownTypeEnum.__repr__N)r   r+   )rc   rd   re   r   rm   rn   rp   r$   r$   r$   r%   rk   3  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Sequence[Any]metadataN)rc   rd   re   rf   rj   r$   r$   r$   r%   r   D  s   
 r   r6   r2   annotation_sourcec               C  s  |j }t }g }	 t| |d\} }|r|| }qt| }|durt|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|rd|vrutd|d | jd } n3t|rd	|vrtd|d	 | jd } nnt| trd
|vrtd
|d
 tt| j} nnq	t	| rd|vrtd|d t} n(t| rd|vrtd|d t} n| tu rd
|vrt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|rz|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     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   r   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