o
    ?HhG                     @  s  d dl mZ d dl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m.Z. ed)e	d*Z/ed+Z0ed,dd-Z1ed.dd/Z2ed0Z3ed1dd-Z4ej5r!e6ej%e%gZ7ne6e%gZ7ej8r1d d2l9m:Z: ne;dZ:ed3Z<e#Z=e$Z>eee	 e?eee.d4f Z@G d5d6 d6e(ZAG d7d8 d8e(e/ ZBG d9d: d:e(e0e4f ZCe%d; ZDdd<dd=ddMdNZEddPdQZFddVdWZGdddXdd_d`ZHddaddcddZIddedfZJddgddhdiZKddkdlZLddodpZMddqdrZNddtduZOddwdxZPddzd{ZQdd}d~ZR	<ddddZSedddZTedddZTedddZTdddZTdddZUdddZV	<ddddZWdddZXdddZYdddZZdddZ[dddddZ\dddZ]G dd de(Z^ede^d*Z_G dd dee_ Z`ede^ddZaG dd deea Zbedee
de	f  d*ZcG dd deec ZddS )    )annotationsN)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_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 )ArgsTypeProcotolz_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=   V/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/sqlalchemy/util/typing.pyr4   [   s   
 r4   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

    r5   r6   Type[_T]
__origin__Nr7   r=   r=   r=   r>   r?   e   s   
 r?   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>   keysz       zSupportsKeysAndGetItem.keys_SupportsKeysAndGetItem__kr+   r1   c                 C  rE   rF   r=   )rG   rJ   r=   r=   r>   __getitem__|   rI   z"SupportsKeysAndGetItem.__getitem__N)rC   rD   )rJ   r+   rC   r1   )r8   r9   r:   rH   rK   r=   r=   r=   r>   rB   y   s    
rB   *Fstr_cleanup_fninclude_generic_already_seencls	Type[Any]
annotation_AnnotationScanTypeoriginating_modulestrlocals_Mapping[str, Any]rN   #Optional[Callable[[str, str], str]]rO   boolrP   Optional[Set[Any]]rC   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.

    rW   in_classNc              
   3  s&    | ]}t | d V  qdS )rM   Nde_stringify_annotation.0elemrP   rQ   rO   rW   rU   rN   r=   r>   	<genexpr>   s    

z*de_stringify_annotation.<locals>.<genexpr>)
is_fwd_ref__forward_arg__
isinstancerV   eval_expression
is_generic
is_literalsetaddtupler6   _copy_generic_annotation_with)	rQ   rS   rU   rW   rN   rO   rP   original_annotationelementsr=   rc   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=   )rg   rV   r   r`   r=   r=   r>   
<listcomp>   s    z,fixup_container_fwd_refs.<locals>.<listcomp>)ri   typing_get_origindictrk   listcollections_abc
MutableSetMutableMappingMutableSequencer   Sequencerematchrepr__class_getitem__rm   typing_get_argsrq   r=   r=   r>   fixup_container_fwd_refs   s2   	r   GenericProtocol[_T]rp   r5   r@   c                 C  s   t | dr
| |S | j| S )N	copy_with)hasattrr   rA   )rS   rp   r=   r=   r>   rn      s   


rn   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   
setdefaultr8   updateeval	Exception)	r   r   rW   r]   base_globalskecls_namespacerS   errr=   r=   r>   rh      s:   

rh   rW   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 )rh   r   r   r   r   r   builtins)r   r   rW   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 )Nr8   )r   r   getattr)r   r   objr=   r=   r>   resolve_name_to_real_class_nameD  s   r   rN   c                  s   t  fdd|jD  S )Nc              	     s   g | ]}t  |i d qS )r   r^   )ra   annorQ   rU   rN   r=   r>   rr   V  s    z/de_stringify_union_elements.<locals>.<listcomp>)make_union_typer6   )rQ   rS   rU   rW   rN   r=   r   r>   de_stringify_union_elementsM  s
   r   Optional[_AnnotationScanType]c                 C  s   | d uo	t | tu S rF   )rs   r   r   r=   r=   r>   	is_pep593c     r   r   TypeGuard[Iterable[Any]]c                 C  s   t | tjot | ttf S rF   )rg   rv   r
   rV   bytes)r   r=   r=   r>   is_non_string_iterableg  s   r   c                 C  s   t | tv S rF   )r   _LITERAL_TYPESr   r=   r=   r>   rj   m  s   rj   TypeGuard[NewType]c                 C  
   t | dS )N__supertype__r   r   r=   r=   r>   
is_newtypeq     
r   TypeGuard[GenericProtocol[Any]]c                 C  s   t | do	t | dS )Nr6   rA   r   r   r=   r=   r>   ri   y  r   ri   TypeGuard[TypeAliasType]c                 C  s
   t | tS rF   )rg   r(   r   r=   r=   r>   	is_pep695}  r   r   r   c                 C  s    | j }t|r|j }t|s|S rF   )r   r   )rq   
super_typer=   r=   r>   flatten_newtype  s
   r   check_genericTypeGuard[ForwardRef]c                 C  s2   t | trdS |rt| rtdd | jD S dS )NTc                 s  s    | ]}t |d V  qdS )TN)re   )ra   argr=   r=   r>   rd     s    zis_fwd_ref.<locals>.<genexpr>F)rg   r   ri   anyr6   )rq   r   r=   r=   r>   re     s
   
re   c                 C  rE   rF   r=   r   r=   r=   r>   de_optionalize_union_types     r   c                 C  rE   rF   r=   r   r=   r=   r>   r     r   c                 C  rE   rF   r=   r   r=   r=   r>   r     s   c                 C  sB   t | rt| S t| rt| j}|t |t t| S | S )zmGiven a type, filter out ``Union`` types that include ``NoneType``
    to not include the ``NoneType``.

    )	re   "de_optionalize_fwd_ref_union_typesis_optionalrk   r6   discardr2   NoneFwdr   )rq   typr=   r=   r>   r     s   


r   c                 C  s   | j }td|}|r4|ddkrt|dS |ddkr2td|d}tdd |D  S | S td	|}d
|v rJtddd |D 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   z,\s*c                 S  s   g | ]
}|d krt |qS )r3   r   r`   r=   r=   r>   rr     s    z6de_optionalize_fwd_ref_union_types.<locals>.<listcomp>z\s*\|\s*r3   |c                 s  s    | ]	}|d kr|V  qdS )r3   Nr=   )ra   pr=   r=   r>   rd     s    z5de_optionalize_fwd_ref_union_types.<locals>.<genexpr>)rf   r{   r|   groupr   splitr   join)rq   rS   mmrp   pipe_tokensr=   r=   r>   r     s   
r   typesc                  G  s   t tt| S )zMake a Union type.

    This is needed by :func:`.de_optionalize_union_types` which removes
    ``NoneType`` from a ``Union``.

    )r   r   r   rK   )r   r=   r=   r>   r     s   r   include_uniondiscard_noneTuple[Type[Any], ...]c                 C  s@   t | rt| j}|r|t |r| ft| S t|S | fS )zPReturn a type as a tuple of individual types, expanding for
    ``Union`` types.)is_unionrk   r6   r   r2   rm   )rq   r   r   r   r=   r=   r>   expand_unions  s   

r   TypeGuard[ArgsTypeProcotol]c                 C  s   t | dddS )Nr   r   	UnionTypeis_origin_ofr   r=   r=   r>   r     s   r   c                 C  s   t | o	tt| v S rF   )r   r2   r   r   r=   r=   r>   is_optional_union  r   r   c                 C  r   )Nr   r   r   r=   r=   r>   r     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)rs   rg   type
issubclass)rq   r   originr=   r=   r>   is_origin_of_cls  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)rs   _get_type_namer9   
startswith)rq   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 )Nr8   _name)r   py310r8   r   )rq   typ_namer=   r=   r>   r   "  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   rC   c                 C  rE   rF   r=   rG   r   r   r=   r=   r>   __get__.  rI   zDescriptorProto.__get__valuer3   c                 C  rE   rF   r=   rG   r   r   r=   r=   r>   __set__0  rI   zDescriptorProto.__set__c                 C  rE   rF   r=   rG   r   r=   r=   r>   
__delete__2  rI   zDescriptorProto.__delete__N)r   r   r   r   rC   r   )r   r   r   r   rC   r3   r   r   rC   r3   )r8   r9   r:   r   r   r   r=   r=   r=   r>   r   -  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   rC   r   c                 C  rE   rF   r=   r   r=   r=   r>   r   E  rI   zDescriptorReference.__get__r   r3   c                 C  rE   rF   r=   r   r=   r=   r>   r   G  rI   zDescriptorReference.__set__c                 C  rE   rF   r=   r   r=   r=   r>   r   I  rI   zDescriptorReference.__delete__N)r   r   r   r   rC   r   )r   r   r   r   rC   r3   r   r8   r9   r:   r;   r   r   r   r   r=   r=   r=   r>   r   8  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   rC   r   c                 C  rE   rF   r=   r   r=   r=   r>   r   Y  rI   zRODescriptorReference.__get__r   r   c                 C  rE   rF   r=   r   r=   r=   r>   r   [  rI   zRODescriptorReference.__set__c                 C  rE   rF   r=   r   r=   r=   r>   r   ]  rI   z RODescriptorReference.__delete__N)r   r   r   r   rC   r   )r   r   r   r   rC   r   )r   r   rC   r   r   r=   r=   r=   r>   r   O  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   rC   r   c                 C  rE   rF   r=   r   r=   r=   r>   r   n  rI   zCallableReference.__get__r   r3   c                 C  rE   rF   r=   r   r=   r=   r>   r   p  rI   zCallableReference.__set__c                 C  rE   rF   r=   r   r=   r=   r>   r   r  rI   zCallableReference.__delete__N)r   r   r   r   rC   r   )r   r   r   r   rC   r3   r   r   r=   r=   r=   r>   r   c  s    

r   )rQ   rR   rS   rT   rU   rV   rW   rX   rN   rY   rO   rZ   rP   r[   rC   rR   )rq   rT   rC   rT   )rS   r   rp   r5   rC   r@   )
r   rV   r   rV   rW   r   r]   r   rC   r   )r   rV   r   rV   rW   r   rC   r   )r   rV   r   rV   rC   rV   )rQ   rR   rS   r4   rU   rV   rW   rX   rN   rY   rC   rR   )rq   r   rC   rZ   )r   r   rC   r   )rq   rT   rC   rZ   )rq   r   rC   r   )rq   rT   rC   r   )rq   rT   rC   r   )rq   r   rC   rR   )F)rq   rT   r   rZ   rC   r   )rq   rV   rC   rV   )rq   rR   rC   rR   )rq   r   rC   rT   )r   rT   rC   rR   )FF)rq   rR   r   rZ   r   rZ   rC   r   )rq   r   rC   r   )rq   r   rC   rZ   )rq   r   r   r   rC   rZ   )rq   r   r   rV   r   r   rC   rZ   )rq   rR   rC   rV   )e
__future__r   r   collections.abcabcrv   r{   r   typingr   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   r1   py38	frozensetr   r   r   r2   r   r   r   rs   rV   rT   r4   r?   rB   _LiteralStarr_   r   rn   rh   r   r   r   r   r   rj   r   ri   r   r   re   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r=   r=   r=   r>   <module>   s   

	
G
)(
"











	


