o
     hW                     @  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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r0d d4l9m:Z: ne;dZ:dd9d:Z<eee e=eee/d;f Z>G d<d= d=e)Z?G d>d? d?e)e2 Z@G d@dA dAe)e3e7f ZAe&dB ZBddCddDddRdSZCddUdVZDdd[d\ZEddd]ddcddZFddeddgdhZGddidjZHddldmZIddpdqZJddrdsZKddvdwZLddydzZMdd|d}ZNdddZOdddZP	C	CddddZQedddZRedddZRedddZRdddZRedddZSedddZSdddZSdddZTdddZUdddZVdddZWdddZXdddddZYdddZZG dd de)Z[ede[d,Z\G dd dee\ Z]ede[ddZ^G dd dee^ Z_edeedef  d,Z`G dd dee` ZaG dd dZbeb ZcerceZdnecZdecj&ZedS )    )annotationsN)deque)Any)Callable)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)LiteralString_T)bound_KT_KT_co)	covariant
_KT_contra)contravariant_VT_VT_co)NoneTypetypr   returnboolc                 C  s   t | to	| jdkS )NNone)
isinstancer   __forward_arg__)r6    r<   I/var/www/html/venv/lib/python3.10/site-packages/sqlalchemy/util/typing.pyis_fwd_noneM      r>   z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<   r<   r=   r@   V   s   
 r@   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

    rA   rB   Type[_T]
__origin__NrC   r<   r<   r<   r=   rI   `   s   
 rI   c                   @  s    e Zd ZdddZddd	Zd
S )SupportsKeysAndGetItemr7   Iterable[_KT]c                 C     d S Nr<   )selfr<   r<   r=   keysu       zSupportsKeysAndGetItem.keys_SupportsKeysAndGetItem__kr.   r4   c                 C  rN   rO   r<   )rP   rS   r<   r<   r=   __getitem__w   rR   z"SupportsKeysAndGetItem.__getitem__N)r7   rM   )rS   r.   r7   r4   )rD   rE   rF   rQ   rT   r<   r<   r<   r=   rL   t   s    
rL   *Fstr_cleanup_fninclude_generic_already_seencls	Type[Any]
annotation_AnnotationScanTypeoriginating_modulestrlocals_Mapping[str, Any]rW   #Optional[Callable[[str, str], str]]rX   rY   Optional[Set[Any]]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.

    r`   in_classNc              
   3  s&    | ]}t | d V  qdS )rV   N)de_stringify_annotation.0elemrY   rZ   rX   r`   r^   rW   r<   r=   	<genexpr>   s    

z*de_stringify_annotation.<locals>.<genexpr>)
is_fwd_refr;   r:   r_   eval_expression
is_generic
is_literalsetaddtuplerB   _copy_generic_annotation_with)	rZ   r\   r^   r`   rW   rX   rY   original_annotationelementsr<   rj   r=   rf   ~   s2   




rf   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<   )r:   r_   r   rg   r<   r<   r=   
<listcomp>   s    z,fixup_container_fwd_refs.<locals>.<listcomp>)rn   r   dictrp   listcollections_abc
MutableSetMutableMappingMutableSequencer   Sequencerematchrepr__class_getitem__rr   r   rv   r<   r<   r=   fixup_container_fwd_refs   s2   	r   GenericProtocol[_T]ru   rA   rJ   c                 C  s   t | dr
| |S | j| S )N	copy_with)hasattrr   rK   )r\   ru   r<   r<   r=   rs      s   


rs   rd   
expressionmodule_nameOptional[Mapping[str, Any]]re   Optional[Type[Any]]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	NameErrorrx   
setdefaultrD   updateeval	Exception)	r   r   r`   re   base_globalskecls_namespacer\   errr<   r<   r=   rm      s:   

rm   r`   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 )rm   r   r   r   r   r   builtins)r   r   r`   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 )NrD   )r   r   getattr)r   r   objr<   r<   r=   resolve_name_to_real_class_name?  s   r   Optional[Any]c                 C  s   | d uo
t | tjv S rO   )r   _type_tuplesr   r   r<   r<   r=   	is_pep593H  s   r   r   TypeGuard[Iterable[Any]]c                 C  s   t | tjot | ttf S rO   )r:   rz   r
   r_   bytes)r   r<   r<   r=   is_non_string_iterableL  s   r   c                 C  s   t | tjv S rO   )r   r   r    r   r<   r<   r=   ro   R  s   ro   Optional[_AnnotationScanType]TypeGuard[NewType]c                 C  s
   t | dS )N__supertype__r   r   r<   r<   r=   
is_newtypeV  s   
r   TypeGuard[GenericProtocol[Any]]c                 C  s   t | do	t | dS )NrB   rK   r   r   r<   r<   r=   rn   ^  r?   rn   TypeGuard[TypeAliasType]c                 C  s   t | r	t| jS t| tjS rO   )rn   	is_pep695rK   r:   _type_instancesr)   r   r<   r<   r=   r   b  s   
r   r   c                 C  s    | j }t|r|j }t|s|S rO   )r   r   )rv   
super_typer<   r<   r=   flatten_newtypel  s
   r   Set[Any]c                   s   t    fdd| }t|tr>t  }t|}|r<| }t|tr+|| n||tu s5t|r7d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<   rh   t)recursive_valuer<   r=   rw     s    z:pep695_values.<locals>.recursive_value.<locals>.<listcomp>)rq   r   	__value__is_unionrB   )
inner_typevalue_seenr   r<   r=   r   |  s   
z&pep695_values.<locals>.recursive_valueN)	rp   r:   ry   r   popleftextendrq   r5   r>   )rv   restypesstackr   r<   r   r=   pep695_valuess  s   

r   check_genericcheck_for_plain_stringTypeGuard[ForwardRef]c                   sJ    r	t | tr	dS t | tj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)rl   )rh   argr   r<   r=   rk     s    
zis_fwd_ref.<locals>.<genexpr>F)r:   r_   r   r   rn   anyrB   )rv   r   r   r<   r   r=   rl     s   rl   c                 C  rN   rO   r<   r   r<   r<   r=   de_optionalize_union_types     r   c                 C  rN   rO   r<   r   r<   r<   r=   r     r   c                 C  rN   rO   r<   r   r<   r<   r=   r        c                 C  s   t | r	t| dS t| rOt| rOtjrt| j}n*t| g}t }|r?|	 }|jD ]}t|r7|
| q+|| q+|s$|d dd |D }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)

    FNc                 S  s    h | ]}|t urt|s|qS r<   )r5   r>   r   r<   r<   r=   	<setcomp>  s     z-de_optionalize_union_types.<locals>.<setcomp>)rl   #_de_optionalize_fwd_ref_union_typesr   includes_noner   py39rp   rB   r   r   appendrq   discardmake_union_type)rv   r6   stack_of_unionsu_typri   r<   r<   r=   r     s$   



r   return_has_noneLiteral[True]c                 C  rN   rO   r<   rv   r   r<   r<   r=   r     r   r   Literal[False]c                 C  rN   rO   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 )r9   r   rg   r<   r<   r=   rw     s    z7_de_optionalize_fwd_ref_union_types.<locals>.<listcomp>Fz\s*\|\s*r9   |c                 s  s    | ]	}|d kr|V  qdS )r9   Nr<   )rh   pr<   r<   r=   rk     s    z6_de_optionalize_fwd_ref_union_types.<locals>.<genexpr>)r;   r   r   groupsplitr   r   joinstripclearlenr   r*   )rv   r   r\   mmg1ru   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   )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 z| tdfv pFt| W S  tyQ   Y dS w )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 rO   r   r   r<   r<   r=   rk   9      z includes_none.<locals>.<genexpr>r   c                 s  r   rO   r   r   r<   r<   r=   rk   =  r   NF)rl   r   r   r   r   r   r   r   r   r   r   r5   r>   	TypeErrorr   r<   r<   r=   r   *  s   

r   c                 C  s2   t | tpt| dp| jdv pt| jd jdv S )NrK   )typingtyping_extensionsr   )r:   typer   rE   __mro__r   r<   r<   r=   	is_a_typeI  s   
r   TypeGuard[ArgsTypeProtocol]c                 C  s   t | ddS )Nr   	UnionType)is_origin_ofr   r<   r<   r=   r   R  s   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   r:   r   
issubclass)rv   r   originr<   r<   r=   is_origin_of_clsV  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_namerE   
startswith)rv   r  r  r   r<   r<   r=   r   c  s   r   c                 C  s0   t jr| jS t| dd }|d u rt| dd }|S )NrD   _name)r   py310rD   r   )rv   typ_namer<   r<   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   r7   c                 C  rN   rO   r<   rP   r  r  r<   r<   r=   __get__~  rR   zDescriptorProto.__get__r   r9   c                 C  rN   rO   r<   rP   r  r   r<   r<   r=   __set__  rR   zDescriptorProto.__set__c                 C  rN   rO   r<   rP   r  r<   r<   r=   
__delete__  rR   zDescriptorProto.__delete__N)r  r  r  r   r7   r   )r  r   r   r   r7   r9   r  r   r7   r9   )rD   rE   rF   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   r7   r  c                 C  rN   rO   r<   r  r<   r<   r=   r    rR   zDescriptorReference.__get__r   r9   c                 C  rN   rO   r<   r  r<   r<   r=   r    rR   zDescriptorReference.__set__c                 C  rN   rO   r<   r  r<   r<   r=   r    rR   zDescriptorReference.__delete__N)r  r  r  r   r7   r  )r  r   r   r  r7   r9   r  rD   rE   rF   rG   r   r  r  r  r<   r<   r<   r=   r    s    


r  _DESC_co)r-   r0   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   r7   r  c                 C  rN   rO   r<   r  r<   r<   r=   r    rR   zRODescriptorReference.__get__r   r   c                 C  rN   rO   r<   r  r<   r<   r=   r    rR   zRODescriptorReference.__set__c                 C  rN   rO   r<   r  r<   r<   r=   r    rR   z RODescriptorReference.__delete__N)r  r  r  r   r7   r  )r  r   r   r   r7   r   )r  r   r7   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   r7   r  c                 C  rN   rO   r<   r  r<   r<   r=   r    rR   zCallableReference.__get__r   r9   c                 C  rN   rO   r<   r  r<   r<   r=   r    rR   zCallableReference.__set__c                 C  rN   rO   r<   r  r<   r<   r=   r    rR   zCallableReference.__delete__N)r  r  r  r   r7   r  )r  r   r   r  r7   r9   r  r  r<   r<   r<   r=   r    s    

r  c                   @  s   e Zd ZdddZdS )	_TypingInstanceskeyr_   r7   tuple[type, ...]c                 C  s@   t dd tt|d tt|d fD }|st||| j|< |S )Nc                 S  s   h | ]}|d ur|qS rO   r<   r   r<   r<   r=   r     s
    z/_TypingInstances.__getattr__.<locals>.<setcomp>)rr   r   r   r   AttributeErrorr   )rP   r  r   r<   r<   r=   __getattr__  s   



z_TypingInstances.__getattr__N)r  r_   r7   r  )rD   rE   rF   r!  r<   r<   r<   r=   r    s    r  )r6   r   r7   r8   )rZ   r[   r\   r]   r^   r_   r`   ra   rW   rb   rX   r8   rY   rc   r7   r[   )rv   r]   r7   r]   )r\   r   ru   rA   r7   rJ   )
r   r_   r   r_   r`   r   re   r   r7   r   )r   r_   r   r_   r`   r   r7   r   )r   r_   r   r_   r7   r_   )rv   r   r7   r8   )r   r   r7   r   )rv   r   r7   r8   )rv   r   r7   r   )rv   r]   r7   r   )rv   r]   r7   r   )rv   r   r7   r[   )rv   r]   r7   r   )FF)rv   r]   r   r8   r   r8   r7   r   )rv   r_   r7   r_   )rv   r[   r7   r[   )rv   r   r   r   r7   r8   )rv   r   r   r   r7   r]   )rv   r   r   r8   r7   r   )r   r]   r7   r[   )rv   r   r7   r   )rv   r   r   r   r7   r8   )rv   r   r  r_   r  r  r7   r8   )rv   r[   r7   r_   )f
__future__r   r   collectionsr   collections.abcabcrz   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r.   r/   r1   r3   r4   r  r   r5   r   r>   r_   r]   r@   rI   rL   _LiteralStarrf   r   rs   rm   r   r   r   r   ro   r   rn   r   r   r   rl   r   r   r   r   r   r   r  r   r  r
  r  r  r  r  r  r  r  r   r   LITERAL_TYPESr<   r<   r<   r=   <module>   s   

	
G
)(
"
	







'
(

6


	


