o
    gQ                     @  s  d dl mZ d dlZd dlmZ d dlmZ d dlZd dl	Z	d dl
Z
d dl
mZ d dl
mZ d dl
mZ d dl
mZ d dl
mZ d d	l
mZ d d
l
mZ d dl
mZ d dl
mZ d dl
mZ d dl
mZ d dl
mZ d dl
mZ d dl
mZ d dl
mZ d dl
mZ d dl
mZ d dl
mZ ddlmZ 	 d dlm Z  d dlm!Z! d dlm"Z" d dlm#Z# d dlm$Z$ d dlm%Z% d dlm&Z& d d lm'Z' d d!lm(Z( d d"lm)Z) d d#lm*Z* d d$lm+Z+ d d%lm,Z, d d&lm-Z- d d'lm.Z. d d(lm/Z/ d d)lm0Z0 d d*lm1Z1 ed+ed,Z2ed-Z3ed.dd/Z4ed0dd1Z5ed2Z6ed3dd/Z7ej8r-e9e
j'e'gZ:ne9e'gZ:ej;r=d d4l<m=Z= ne>dZ=ed5Z?eee e@eee0d6f ZAG d7d8 d8e*ZBG d9d: d:e*e2 ZCG d;d< d<e*e3e7f ZDe'd= ZEdd>dd?ddOdPZFddRdSZGddXdYZHdddZddadbZIddcddedfZJddgdhZKddjdkZLddndoZMddpdqZNddtduZOddwdxZPddzd{ZQdd}d~ZRdddZS	>	>ddddZTedddZUedddZUedddZUdddZUedddZVedddZVdddZVdddZWdddZXdddZYdddZZdddddZ[dddZ\G dd de*Z]ede]d,Z^G dd dee^ Z_ede]ddZ`G dd dee` Zaedeedef  d,ZbG dd deeb ZcdS )    )annotationsN)deque)Any)Callable)cast)Dict
ForwardRef)Generic)Iterable)Mapping)NewType)NoReturn)Optional)overload)Set)Tuple)Type)TYPE_CHECKING)TypeVar)Union   )compatT)	Annotated)Concatenate)dataclass_transform)Final)final)get_args)
get_origin)Literal)NotRequired)	ParamSpec)Protocol)SupportsIndex)	TypeAlias)	TypedDict)	TypeGuard)Self)TypeAliasType)Never_T)bound_KT_KT_co)	covariant
_KT_contra)contravariant_VT_VT_co)NoneTypeNonezGenericProtocol[Any]c                   @  s   e Zd ZU dZded< dS )ArgsTypeProtocolz_protocol for types that have ``__args__``

    there's no public interface for this AFAIK

    Tuple[_AnnotationScanType, ...]__args__N__name__
__module____qualname____doc____annotations__ r?   r?   P/var/www/html/api-tag/env/lib/python3.10/site-packages/sqlalchemy/util/typing.pyr6   Z   s   
 r6   c                   @  s"   e Zd ZU dZded< ded< dS )GenericProtocolzXprotocol for generic types.

    this since Python.typing _GenericAlias is private

    r7   r8   Type[_T]
__origin__Nr9   r?   r?   r?   r@   rA   d   s   
 rA   c                   @  s    e Zd ZdddZddd	Zd
S )SupportsKeysAndGetItemreturnIterable[_KT]c                 C     d S Nr?   )selfr?   r?   r@   keysy       zSupportsKeysAndGetItem.keys_SupportsKeysAndGetItem__kr-   r3   c                 C  rG   rH   r?   )rI   rL   r?   r?   r@   __getitem__{   rK   z"SupportsKeysAndGetItem.__getitem__N)rE   rF   )rL   r-   rE   r3   )r:   r;   r<   rJ   rM   r?   r?   r?   r@   rD   x   s    
rD   *Fstr_cleanup_fninclude_generic_already_seencls	Type[Any]
annotation_AnnotationScanTypeoriginating_modulestrlocals_Mapping[str, Any]rP   #Optional[Callable[[str, str], str]]rQ   boolrR   Optional[Set[Any]]rE   c          	        s   |}t |r	|j}t|trr|}t|d}rOt|rOt|sO du r.t  | v r4|S  | t	 fdd|j
D }t||S |S )a;  Resolve annotations that may be string based into real objects.

    This is particularly important if a module defines "from __future__ import
    annotations", as everything inside of __annotations__ is a string. We want
    to at least have generic containers like ``Mapped``, ``Union``, ``List``,
    etc.

    rY   in_classNc              
   3  s&    | ]}t | d V  qdS )rO   N)de_stringify_annotation.0elemrR   rS   rQ   rY   rW   rP   r?   r@   	<genexpr>   s    

z*de_stringify_annotation.<locals>.<genexpr>)
is_fwd_ref__forward_arg__
isinstancerX   eval_expression
is_generic
is_literalsetaddtupler8   _copy_generic_annotation_with)	rS   rU   rW   rY   rP   rQ   rR   original_annotationelementsr?   rd   r@   r`      s2   




r`   type_c              	   C  sb   t | r/t| ttttjtjtjtj	tj
fv r/tdt| s/t| tdd t| D S | S )zfCorrect dict['x', 'y'] into dict[ForwardRef('x'), ForwardRef('y')]
    and similar for list, set

    z3typing.(?:Dict|List|Set|.*Mapping|.*Sequence|.*Set)c                 S  s"   g | ]}t |trt|n|qS r?   )rh   rX   r	   ra   r?   r?   r@   
<listcomp>   s    z,fixup_container_fwd_refs.<locals>.<listcomp>)rj   r   dictrl   listcollections_abc
MutableSetMutableMappingMutableSequencer   Sequencerematchrepr__class_getitem__rn   r   rr   r?   r?   r@   fixup_container_fwd_refs   s2   	r   GenericProtocol[_T]rq   r7   rB   c                 C  s   t | dr
| |S | j| S )N	copy_with)hasattrr   rC   )rU   rq   r?   r?   r@   ro      s   


ro   r^   
expressionmodule_nameOptional[Mapping[str, Any]]r_   Optional[Type[Any]]r   c          	   
   C  s   zt j| j}W n ty } ztd| d|  |d }~ww z'|d ur?t|j}||j| || t	| ||}W |S t	| ||}W |S  t
y[ } ztd| |d }~ww )NModule z9 isn't present in sys.modules; can't evaluate expression z"Could not de-stringify annotation )sysmodules__dict__KeyError	NameErrorrt   
setdefaultr:   updateeval	Exception)	r   r   rY   r_   base_globalskecls_namespacerU   errr?   r?   r@   ri      s:   

ri   rY   namec                C  s   d| v rt | ||dS ztj| j}W n ty* } ztd| d|  |d }~ww z||  W S  ty] } z!ztj|  W W  Y d }~S  tyM   Y nw td|  d| |d }~ww )N.r   r   z2 isn't present in sys.modules; can't resolve name zCould not locate name z in module )ri   r   r   r   r   r   builtins)r   r   rY   r   r   r?   r?   r@   eval_name_only!  s:   	
r   c                 C  s2   zt | |}W n ty   |  Y S w t|d| S )Nr:   )r   r   getattr)r   r   objr?   r?   r@   resolve_name_to_real_class_nameC  s   r   Optional[Any]c                 C  s   | d uo	t | tu S rH   )r   r   r   r?   r?   r@   	is_pep593L     r   r   TypeGuard[Iterable[Any]]c                 C  s   t | tjot | ttf S rH   )rh   rv   r   rX   bytes)r   r?   r?   r@   is_non_string_iterableP  s   r   c                 C  s   t | tv S rH   )r   LITERAL_TYPESr   r?   r?   r@   rk   V     rk   Optional[_AnnotationScanType]TypeGuard[NewType]c                 C  s
   t | dS )N__supertype__r   r   r?   r?   r@   
is_newtypeZ     
r   TypeGuard[GenericProtocol[Any]]c                 C  s   t | do	t | dS )Nr8   rC   r   r   r?   r?   r@   rj   b  r   rj   TypeGuard[TypeAliasType]c                 C  s
   t | tS rH   )rh   r)   r   r?   r?   r@   	is_pep695f  r   r   r   c                 C  s    | j }t|r|j }t|s|S rH   )r   r   )rr   
super_typer?   r?   r@   flatten_newtypej  s
   r   Set[Any]c                   s~   t    fdd| }t|tr<t  }t|}|r:| }t|tr+|| n||tthv r5dn| |s|S |hS )zExtracts the value from a TypeAliasType, recursively exploring unions
    and inner TypeAliasType to flatten them into a single set.

    Forward references are not evaluated, so no recursive exploration happens
    into them.
    c                   sH   |  v r| S   |  t| s| S | j}t|s|S fdd|jD S )Nc                   s   g | ]} |qS r?   r?   rb   t)recursive_valuer?   r@   rs     s    z:pep695_values.<locals>.recursive_value.<locals>.<listcomp>)rm   r   	__value__is_unionr8   )rr   value_seenr   r?   r@   r   z  s   
z&pep695_values.<locals>.recursive_valueN)	rl   rh   ru   r   popleftextendrm   r4   NoneFwd)rr   restypesstackr   r?   r   r@   pep695_valuesq  s   

r   check_genericcheck_for_plain_stringTypeGuard[ForwardRef]c                   sH    r	t | tr	dS t | trdS |r"t| r"t fdd| jD S dS )NTc                 3  s    | ]
}t |d  dV  qdS )Tr   N)rf   )rb   argr   r?   r@   re     s    
zis_fwd_ref.<locals>.<genexpr>F)rh   rX   r	   rj   anyr8   )rr   r   r   r?   r   r@   rf     s   
rf   c                 C  rG   rH   r?   r   r?   r?   r@   de_optionalize_union_types     r   c                 C  rG   rH   r?   r   r?   r?   r@   r     r   c                 C  rG   rH   r?   r   r?   r?   r@   r        c                 C  s   t | r	t| dS t| rRt| rRtjrt| j}n*t| g}t }|r?|	 }|jD ]}t|r7|
| q+|| q+|s$|d |t |t t| S | S )zGiven a type, filter out ``Union`` types that include ``NoneType``
    to not include the ``NoneType``.

    Contains extra logic to work on non-flattened unions, unions that contain
    ``None`` (seen in py38, 37)

    FN)rf   #_de_optionalize_fwd_ref_union_typesr   includes_noner   py39rl   r8   r   r   appendrm   discardr4   r   make_union_type)rr   typstack_of_unionsu_typrc   r?   r?   r@   r     s&   





r	   return_has_noneLiteral[True]c                 C  rG   rH   r?   rr   r   r?   r?   r@   r     r   r   Literal[False]c                 C  rG   rH   r?   r   r?   r?   r@   r     r    Union[_AnnotationScanType, bool]c                 C  sp  | j }td|}|r|ddd }|dkr$|rdS t|dS |dkrd	|dv rlg }g }d
}|dD ]0}|d	krE|d7 }n |dkrN|d8 }n|d
kre|dkre|d|  |	  q:|| q:n	td|d}dd |D }	|rt
|t
|	kS |	rt|	 S tS |rdS | S td|}
d|
v }|r|S |rddd |
D }|rt|S tS | S )zreturn the non-optional type for Optional[], Union[None, ...], x|None,
    etc. without de-stringifying forward refs.

    unfortunately this seems to require lots of hardcoded heuristics

    z^(.+?)\[(.+)\]$r   r   r   T   r   [r   ], z,\s*c                 S  s   g | ]
}|d krt |qS )r5   r   ra   r?   r?   r@   rs     s    z7_de_optionalize_fwd_ref_union_types.<locals>.<listcomp>Fz\s*\|\s*r5   |c                 s  s    | ]	}|d kr|V  qdS )r5   Nr?   )rb   pr?   r?   r@   re     s    z6_de_optionalize_fwd_ref_union_types.<locals>.<genexpr>)rg   r{   r|   groupsplitr	   r   joinstripclearlenr   r*   )rr   r   rU   mmg1rq   currentignore_commacharpartspipe_tokenshas_noneanno_strr?   r?   r@   r     sH   


r   c                  G  s
   t | S )zMake a Union type.)r   rM   )r   r?   r?   r@   r   #  s   
r   c                 C  s   t | r	t| dS t| rtdd t| D S t| r$tt| d S t| r3tdd t| D S t	| r<t| j
S | ttdfv S )a.  Returns if the type annotation ``type_`` allows ``None``.

    This function supports:
    * forward refs
    * unions
    * pep593 - Annotated
    * pep695 - TypeAliasType (does not support looking into
    fw reference of other pep695)
    * NewType
    * plain types like ``int``, ``None``, etc
    Tc                 s      | ]}t |V  qd S rH   r   r   r?   r?   r@   re   7      z includes_none.<locals>.<genexpr>r   c                 s  r   rH   r   r   r?   r?   r@   re   ;  r   N)rf   r   r   r   r   r   r   r   r   r   r   r   r4   r   r?   r?   r@   r   (  s   

r   TypeGuard[ArgsTypeProtocol]c                 C  s   t | ddS )Nr   	UnionType)is_origin_ofr   r?   r?   r@   r   A  r   r   	class_obj'Union[Tuple[Type[Any], ...], Type[Any]]c                 C  s(   t | }|du r
dS t|tot||S )z[return True if the given type has an __origin__ that shares a base
    with the given classNF)r   rh   type
issubclass)rr   r   originr?   r?   r@   is_origin_of_clsE  s   r   )modulenamesr   Optional[str]c                G  s4   t | }|du r
dS t||v o|du p|j|S )z\return True if the given type has an __origin__ with the given name
    and optional module.NF)r   _get_type_namer;   
startswith)rr   r   r   r   r?   r?   r@   r   R  s   r   c                 C  s0   t jr| jS t| dd }|d u rt| dd }|S )Nr:   _name)r   py310r:   r   )rr   typ_namer?   r?   r@   r   a  s   r   c                   @  s*   e Zd ZdddZdd
dZdddZdS )DescriptorProtoinstanceobjectownerr   rE   c                 C  rG   rH   r?   rI   r  r  r?   r?   r@   __get__m  rK   zDescriptorProto.__get__r   r5   c                 C  rG   rH   r?   rI   r  r   r?   r?   r@   __set__o  rK   zDescriptorProto.__set__c                 C  rG   rH   r?   rI   r  r?   r?   r@   
__delete__q  rK   zDescriptorProto.__delete__N)r  r  r  r   rE   r   )r  r   r   r   rE   r5   r  r   rE   r5   )r:   r;   r<   r  r
  r  r?   r?   r?   r@   r  l  s    

r  _DESCc                   @  6   e Zd ZdZerddd	ZdddZdddZdS dS )DescriptorReferencea  a descriptor that refers to a descriptor.

    used for cases where we need to have an instance variable referring to an
    object that is itself a descriptor, which typically confuses typing tools
    as they don't know when they should use ``__get__`` or not when referring
    to the descriptor assignment as an instance variable. See
    sqlalchemy.orm.interfaces.PropComparator.prop

    r  r  r  r   rE   r  c                 C  rG   rH   r?   r  r?   r?   r@   r    rK   zDescriptorReference.__get__r   r5   c                 C  rG   rH   r?   r	  r?   r?   r@   r
    rK   zDescriptorReference.__set__c                 C  rG   rH   r?   r  r?   r?   r@   r    rK   zDescriptorReference.__delete__N)r  r  r  r   rE   r  )r  r   r   r  rE   r5   r  r:   r;   r<   r=   r   r  r
  r  r?   r?   r?   r@   r  w  s    


r  _DESC_co)r,   r/   c                   @  r  )RODescriptorReferenceza descriptor that refers to a descriptor.

    same as :class:`.DescriptorReference` but is read-only, so that subclasses
    can define a subtype as the generically contained element

    r  r  r  r   rE   r  c                 C  rG   rH   r?   r  r?   r?   r@   r    rK   zRODescriptorReference.__get__r   r   c                 C  rG   rH   r?   r	  r?   r?   r@   r
    rK   zRODescriptorReference.__set__c                 C  rG   rH   r?   r  r?   r?   r@   r    rK   z RODescriptorReference.__delete__N)r  r  r  r   rE   r  )r  r   r   r   rE   r   )r  r   rE   r   r  r?   r?   r?   r@   r    s    

r  _FN.c                   @  r  )CallableReferenceza descriptor that refers to a callable.

    works around mypy's limitation of not allowing callables assigned
    as instance variables


    r  r  r  r   rE   r  c                 C  rG   rH   r?   r  r?   r?   r@   r    rK   zCallableReference.__get__r   r5   c                 C  rG   rH   r?   r	  r?   r?   r@   r
    rK   zCallableReference.__set__c                 C  rG   rH   r?   r  r?   r?   r@   r    rK   zCallableReference.__delete__N)r  r  r  r   rE   r  )r  r   r   r  rE   r5   r  r  r?   r?   r?   r@   r    s    

r  )rS   rT   rU   rV   rW   rX   rY   rZ   rP   r[   rQ   r\   rR   r]   rE   rT   )rr   rV   rE   rV   )rU   r   rq   r7   rE   rB   )
r   rX   r   rX   rY   r   r_   r   rE   r   )r   rX   r   rX   rY   r   rE   r   )r   rX   r   rX   rE   rX   )rr   r   rE   r\   )r   r   rE   r   )rr   r   rE   r\   )rr   r   rE   r   )rr   rV   rE   r   )rr   rV   rE   r   )rr   r   rE   rT   )rr   rV   rE   r   )FF)rr   rV   r   r\   r   r\   rE   r   )rr   rX   rE   rX   )rr   rT   rE   rT   )rr   r	   r   r   rE   r\   )rr   r	   r   r   rE   rV   )rr   r	   r   r\   rE   r   )r   rV   rE   rT   )rr   r   rE   r   )rr   r   r   r   rE   r\   )rr   r   r   rX   r   r   rE   r\   )rr   rT   rE   rX   )d
__future__r   r   collectionsr   collections.abcabcrv   r{   r   typingr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   typing_extensionsr   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r-   r.   r0   r2   r3   py38	frozensetr   r  r   r4   r   r   rX   rV   r6   rA   rD   _LiteralStarr`   r   ro   ri   r   r   r   r   rk   r   rj   r   r   r   rf   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r?   r?   r?   r@   <module>   s   

	
G
)(
"
	






'
)

6



