o
    hqW                     @  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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 d d+lm2Z2 ed,ed-Z3ed.Z4ed/dd0Z5ed1dd2Z6ed3Z7ed4dd0Z8ej9r6d d5l:m;Z; ne<dZ;ed6Z=eee e>eee0d7f Z?G d8d9 d9e*Z@G d:d; d;e*e3 ZAG d<d= d=e*e4e8f ZBe'd> ZCdd?dd@ddPdQZDddSdTZEddYdZZFddd[ddbdcZGdddddfdgZHddhdiZIddkdlZJddodpZKddqdrZLddudvZMddxdyZNdd{d|ZOdd~dZPdddZQ	?	?ddddZRedddZSedddZSedddZSdddZSedddZTedddZTdddZTdddZUdddZVdddZWdddZXdddZYdddddZZdddZ[G 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-ZaG dd deea ZbG dd dZcec ZderheZenedZeedj'Zfd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)LiteralString_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@   j/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sqlalchemy/util/typing.pyr7   U   s   
 r7   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

    r8   r9   Type[_T]
__origin__Nr:   r@   r@   r@   rA   rB   _   s   
 rB   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@   rA   keyst       zSupportsKeysAndGetItem.keys_SupportsKeysAndGetItem__kr.   r4   c                 C  rH   rI   r@   )rJ   rM   r@   r@   rA   __getitem__v   rL   z"SupportsKeysAndGetItem.__getitem__N)rF   rG   )rM   r.   rF   r4   )r;   r<   r=   rK   rN   r@   r@   r@   rA   rE   s   s    
rE   *Fstr_cleanup_fninclude_generic_already_seencls	Type[Any]
annotation_AnnotationScanTypeoriginating_modulestrlocals_Mapping[str, Any]rQ   #Optional[Callable[[str, str], str]]rR   boolrS   Optional[Set[Any]]rF   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.

    rZ   in_classNc              
   3  s&    | ]}t | d V  qdS )rP   N)de_stringify_annotation.0elemrS   rT   rR   rZ   rX   rQ   r@   rA   	<genexpr>   s    

z*de_stringify_annotation.<locals>.<genexpr>)
is_fwd_ref__forward_arg__
isinstancerY   eval_expression
is_generic
is_literalsetaddtupler9   _copy_generic_annotation_with)	rT   rV   rX   rZ   rQ   rR   rS   original_annotationelementsr@   re   rA   ra   }   s2   




ra   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@   )ri   rY   r	   rb   r@   r@   rA   
<listcomp>   s    z,fixup_container_fwd_refs.<locals>.<listcomp>)rk   r   dictrm   listcollections_abc
MutableSetMutableMappingMutableSequencer   Sequencerematchrepr__class_getitem__ro   r   rs   r@   r@   rA   fixup_container_fwd_refs   s2   	r   GenericProtocol[_T]rr   r8   rC   c                 C  s   t | dr
| |S | j| S )N	copy_with)hasattrr   rD   )rV   rr   r@   r@   rA   rp      s   


rp   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	NameErrorru   
setdefaultr;   updateeval	Exception)	r   r   rZ   r`   base_globalskecls_namespacerV   errr@   r@   rA   rj      s:   

rj   rZ   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 )rj   r   r   r   r   r   builtins)r   r   rZ   r   r   r@   r@   rA   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@   rA   resolve_name_to_real_class_name>  s   r   Optional[Any]c                 C  s   | d uo
t | tjv S rI   )r   _type_tuplesr   r   r@   r@   rA   	is_pep593G  s   r   r   TypeGuard[Iterable[Any]]c                 C  s   t | tjot | ttf S rI   )ri   rw   r   rY   bytes)r   r@   r@   rA   is_non_string_iterableK  s   r   c                 C  s   t | tjv S rI   )r   r   r    r   r@   r@   rA   rl   Q  s   rl   Optional[_AnnotationScanType]TypeGuard[NewType]c                 C  s
   t | dS )N__supertype__r   r   r@   r@   rA   
is_newtypeU  s   
r   TypeGuard[GenericProtocol[Any]]c                 C  s   t | do	t | dS )Nr9   rD   r   r   r@   r@   rA   rk   ]  s   rk   TypeGuard[TypeAliasType]c                 C  s   t | r	t| jS t| tjS rI   )rk   	is_pep695rD   ri   _type_instancesr)   r   r@   r@   rA   r   a  s   
r   r   c                 C  s    | j }t|r|j }t|s|S rI   )r   r   )rs   
super_typer@   r@   rA   flatten_newtypek  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@   rc   t)recursive_valuer@   rA   rt     s    z:pep695_values.<locals>.recursive_value.<locals>.<listcomp>)rn   r   	__value__is_unionr9   )
inner_typevalue_seenr   r@   rA   r   {  s   
z&pep695_values.<locals>.recursive_valueN)	rm   ri   rv   r   popleftextendrn   r5   NoneFwd)rs   restypesstackr   r@   r   rA   pep695_valuesr  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)rg   )rc   argr   r@   rA   rf     s    
zis_fwd_ref.<locals>.<genexpr>F)ri   rY   r   r	   rk   anyr9   )rs   r   r   r@   r   rA   rg     s   rg   c                 C  rH   rI   r@   r   r@   r@   rA   de_optionalize_union_types     r   c                 C  rH   rI   r@   r   r@   r@   rA   r     r   c                 C  rH   rI   r@   r   r@   r@   rA   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)rg   #_de_optionalize_fwd_ref_union_typesr   includes_noner   py39rm   r9   r   r   appendrn   discardr5   r   make_union_type)rs   typstack_of_unionsu_typrd   r@   r@   rA   r     s&   





r	   return_has_noneLiteral[True]c                 C  rH   rI   r@   rs   r   r@   r@   rA   r     r   r   Literal[False]c                 C  rH   rI   r@   r   r@   r@   rA   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 )r6   r   rb   r@   r@   rA   rt     s    z7_de_optionalize_fwd_ref_union_types.<locals>.<listcomp>Fz\s*\|\s*r6   |c                 s  s    | ]	}|d kr|V  qdS )r6   Nr@   )rc   pr@   r@   rA   rf     s    z6_de_optionalize_fwd_ref_union_types.<locals>.<genexpr>)rh   r|   r}   groupsplitr	   r   joinstripclearlenr   r*   )rs   r   rV   mmg1rr   currentignore_commacharpartspipe_tokenshas_noneanno_strr@   r@   rA   r     sH   


r   c                  G  s
   t | S )zMake a Union type.)r   rN   )r   r@   r@   rA   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tdfv W S  tyN   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 rI   r   r   r@   r@   rA   rf   8      z includes_none.<locals>.<genexpr>r   c                 s  r   rI   r   r   r@   r@   rA   rf   <  r   NF)rg   r   r   r   r   r   r   r   r   r   r   r   r5   	TypeErrorr   r@   r@   rA   r   )  s   

r   c                 C  s2   t | tpt| dp| jdv pt| jd jdv S )NrD   )typingtyping_extensionsr   )ri   typer   r<   __mro__r   r@   r@   rA   	is_a_typeH  s   
r   TypeGuard[ArgsTypeProtocol]c                 C  s   t | ddS )Nr   	UnionType)is_origin_ofr   r@   r@   rA   r   Q  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   ri   r   
issubclass)rs   r   originr@   r@   rA   is_origin_of_clsU  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)rs   r   r  r   r@   r@   rA   r   b  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   )rs   typ_namer@   r@   rA   r  q  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   rF   c                 C  rH   rI   r@   rJ   r	  r  r@   r@   rA   __get__}  rL   zDescriptorProto.__get__r   r6   c                 C  rH   rI   r@   rJ   r	  r   r@   r@   rA   __set__  rL   zDescriptorProto.__set__c                 C  rH   rI   r@   rJ   r	  r@   r@   rA   
__delete__  rL   zDescriptorProto.__delete__N)r	  r
  r  r   rF   r   )r	  r   r   r   rF   r6   r	  r   rF   r6   )r;   r<   r=   r  r  r  r@   r@   r@   rA   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   rF   r  c                 C  rH   rI   r@   r  r@   r@   rA   r    rL   zDescriptorReference.__get__r   r6   c                 C  rH   rI   r@   r  r@   r@   rA   r    rL   zDescriptorReference.__set__c                 C  rH   rI   r@   r  r@   r@   rA   r    rL   zDescriptorReference.__delete__N)r	  r
  r  r   rF   r  )r	  r   r   r  rF   r6   r  r;   r<   r=   r>   r   r  r  r  r@   r@   r@   rA   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   rF   r  c                 C  rH   rI   r@   r  r@   r@   rA   r    rL   zRODescriptorReference.__get__r   r   c                 C  rH   rI   r@   r  r@   r@   rA   r    rL   zRODescriptorReference.__set__c                 C  rH   rI   r@   r  r@   r@   rA   r    rL   z RODescriptorReference.__delete__N)r	  r
  r  r   rF   r  )r	  r   r   r   rF   r   )r	  r   rF   r   r  r@   r@   r@   rA   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   rF   r  c                 C  rH   rI   r@   r  r@   r@   rA   r    rL   zCallableReference.__get__r   r6   c                 C  rH   rI   r@   r  r@   r@   rA   r    rL   zCallableReference.__set__c                 C  rH   rI   r@   r  r@   r@   rA   r    rL   zCallableReference.__delete__N)r	  r
  r  r   rF   r  )r	  r   r   r  rF   r6   r  r  r@   r@   r@   rA   r    s    

r  c                   @  s   e Zd ZdddZdS )	_TypingInstanceskeyrY   rF   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 rI   r@   r   r@   r@   rA   	<setcomp>  s
    z/_TypingInstances.__getattr__.<locals>.<setcomp>)ro   r   r   r   AttributeErrorr   )rJ   r  r   r@   r@   rA   __getattr__  s   



z_TypingInstances.__getattr__N)r  rY   rF   r  )r;   r<   r=   r   r@   r@   r@   rA   r    s    r  )rT   rU   rV   rW   rX   rY   rZ   r[   rQ   r\   rR   r]   rS   r^   rF   rU   )rs   rW   rF   rW   )rV   r   rr   r8   rF   rC   )
r   rY   r   rY   rZ   r   r`   r   rF   r   )r   rY   r   rY   rZ   r   rF   r   )r   rY   r   rY   rF   rY   )rs   r   rF   r]   )r   r   rF   r   )rs   r   rF   r]   )rs   r   rF   r   )rs   rW   rF   r   )rs   rW   rF   r   )rs   r   rF   rU   )rs   rW   rF   r   )FF)rs   rW   r   r]   r   r]   rF   r   )rs   rY   rF   rY   )rs   rU   rF   rU   )rs   r	   r   r   rF   r]   )rs   r	   r   r   rF   rW   )rs   r	   r   r]   rF   r   )r   rW   rF   rU   )rs   r   rF   r   )rs   r   r   r   rF   r]   )rs   r   r  rY   r   r  rF   r]   )rs   rU   rF   rY   )g
__future__r   r   collectionsr   collections.abcabcrw   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.   r/   r1   r3   r4   r  r   r5   r   r   rY   rW   r7   rB   rE   _LiteralStarra   r   rp   rj   r   r   r   r   rl   r   rk   r   r   r   rg   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r  r   r   LITERAL_TYPESr@   r@   r@   rA   <module>   s   
	
G
)(
"
	







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