
    e_G                    v   U d 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mZ ddlmZ ddlmZ erddlmZ ddlmZ e	e ef         Z!ej"        Z#de$d<    G d d e          Z% G d! d"e%          Z& G d# d$e&          Z' G d% d&e%          Z(i Z)d'e$d(<    ed)d *          Z*dKd/Z+	 dLd0d0dd1dMd:Z,e	 dLdNd>            Z-e	 dLdOd?            Z-	 dLdPdAZ-dQdBZ.dRdGZ/dSdJZ0dS )Ta*  The :class:`.Annotated` class and related routines; creates hash-equivalent
copies of SQL constructs which contain context-specific markers and
associations.

Note that the :class:`.Annotated` concept as implemented in this module is not
related in any way to the pep-593 concept of "Annotated".


    )annotationsN)Any)Callable)cast)Dict)	FrozenSet)Mapping)Optional)overload)Sequence)Tuple)Type)TYPE_CHECKING)TypeVar   )	operators)HasCacheKey)anon_map)ExternallyTraversible)InternalTraversal   )util)Literal)Self)_EntityNamespace)_TraverseInternalsTypeutil.immutabledict[str, Any]EMPTY_ANNOTATIONSc                      e Zd ZU dZeZded<   ded<   ded<   ddZe	 	 ddd            Z	e	 	 ddd            Z		 	 dd dZ	e
j        d!d            Zd"dZdS )#SupportsAnnotations r   _annotationsz*util.generic_fn_descriptor[FrozenSet[Any]]	proxy_setbool_is_immutablevalues_AnnotationDictreturnr   c                    t                      NNotImplementedErrorselfr&   s     9lib/python3.11/site-packages/sqlalchemy/sql/annotation.py	_annotatezSupportsAnnotations._annotate>   s    !###    .Literal[None]clonec                    d S r*   r!   r.   r&   r3   s      r/   _deannotatezSupportsAnnotations._deannotateA   	     	r1   Sequence[str]c                    d S r*   r!   r5   s      r/   r6   zSupportsAnnotations._deannotateI   r7   r1   NFOptional[Sequence[str]]c                    t                      r*   r+   r5   s      r/   r6   zSupportsAnnotations._deannotateQ   s    
 "###r1   Tuple[Any, ...]c                H    t                      }|                     |          S r*   )r   _gen_annotations_cache_key)r.   	anon_map_s     r/   _annotations_cache_keyz*SupportsAnnotations._annotations_cache_keyX   s    JJ	..y999r1   r   c                |     dt          fd fdt           j                  D             D                       fS )Nr"   c              3  |   K   | ]6\  }}|t          |t                    r|                    g           n|fV  7d S r*   )
isinstancer   _gen_cache_key).0keyvaluer   s      r/   	<genexpr>zASupportsAnnotations._gen_annotations_cache_key.<locals>.<genexpr>c   sl         C !%55E((2666	     r1   c                .    g | ]}|j         |         fS r!   )r"   )rE   rF   r.   s     r/   
<listcomp>zBSupportsAnnotations._gen_annotations_cache_key.<locals>.<listcomp>j   s6     # # # $+C01# # #r1   )tuplesortedr"   )r.   r   s   ``r/   r>   z.SupportsAnnotations._gen_annotations_cache_key^   st         # # # #%d&788# # #    
 	
r1   r&   r'   r(   r   ..r&   r2   r3   r$   r(   r   r&   r8   r3   r$   r(   r    NFr&   r:   r3   r$   r(   r    )r(   r<   )r   r   r(   r<   )__name__
__module____qualname__	__slots__r   r"   __annotations__r0   r   r6   r   memoized_propertyr@   r>   r!   r1   r/   r    r    5   s        I1BLBBBB9999$ $ $ $  !$    X  !$    X +/$ $ $ $ $ 
: : : :

 
 
 
 
 
r1   r    c                      e Zd ZU dZded<   erej        dd            Zdd
Z	ddZ
e	 	 ddd            Ze	 	 ddd            Z	 	 dddZdS )SupportsWrappingAnnotationsr!   z*Callable[..., SupportsWrappingAnnotations]_constructorr(   r   c                    d S r*   r!   r.   s    r/   entity_namespacez,SupportsWrappingAnnotations.entity_namespacey   s    Cr1   r&   r'   r   c                8    t                               | |          S )greturn a copy of this ClauseElement with annotations
        updated by the given dictionary.

        	Annotated_as_annotated_instancer-   s     r/   r0   z%SupportsWrappingAnnotations._annotate}       
 //f===r1   c                8    t                               | |          S )hreturn a copy of this ClauseElement with annotations
        replaced by the given dictionary.

        ra   r-   s     r/   _with_annotationsz-SupportsWrappingAnnotations._with_annotations   rd   r1   .r2   r3   r$   c                    d S r*   r!   r5   s      r/   r6   z'SupportsWrappingAnnotations._deannotate   r7   r1   r8   r    c                    d S r*   r!   r5   s      r/   r6   z'SupportsWrappingAnnotations._deannotate   r7   r1   NFr:   c                6    |r|                                  }|S | S )return a copy of this :class:`_expression.ClauseElement`
        with annotations
        removed.

        :param values: optional tuple of individual values
         to remove.

        )_clone)r.   r&   r3   ss       r/   r6   z'SupportsWrappingAnnotations._deannotate   s#      	AHKr1   r(   r   rM   rN   rO   rP   rQ   rR   )rS   rT   rU   rV   rW   r   r   ro_non_memoized_propertyr^   r0   rg   r   r6   r!   r1   r/   rZ   rZ   r   s         I<<<< 		&	 	 	 
'	&	> > > >> > > >  !$    X  !$    X +/      r1   rZ   c                      e Zd ZU ej        sdZdej        fgZde	d<   dd	Z
dd
Ze	 	 ddd            Ze	 	 ddd            Z	 	 dddZdS )SupportsCloneAnnotationsr!   r"   r   %_clone_annotations_traverse_internalsr&   r'   r(   r   c                    |                                  }|j                            |          |_        |j                            dd           |j                            dd           |S )r`   r@   N_generate_cache_key)rl   r"   union__dict__popr.   r&   news      r/   r0   z"SupportsCloneAnnotations._annotate   sa    
 kkmm+11&9914888.555
r1   c                    |                                  }t          j        |          |_        |j                            dd           |j                            dd           |S )rf   r@   Nrt   )rl   r   immutabledictr"   rv   rw   rx   s      r/   rg   z*SupportsCloneAnnotations._with_annotations   s[    
 kkmm-f5514888.555
r1   .r2   r3   r$   c                    d S r*   r!   r5   s      r/   r6   z$SupportsCloneAnnotations._deannotate   r7   r1   r8   r    c                    d S r*   r!   r5   s      r/   r6   z$SupportsCloneAnnotations._deannotate   r7   r1   NFr:   c                    |s| j         rI|                                 }t          j                    |_         |j                            dd           |S | S )rk   r@   N)r"   rl   r   r{   rv   rw   )r.   r&   r3   ry   s       r/   r6   z$SupportsCloneAnnotations._deannotate   sY      	D% 	 ++--C#133CL5t<<<JKr1   rM   rN   rO   rP   rQ   rR   )rS   rT   rU   typingr   rV   r   dp_annotations_keyrr   rW   r0   rg   r   r6   r!   r1   r/   rq   rq      s           	 
*=>E)    	 	 	 		 	 	 	  !$    X  !$    X +/      r1   rq   c                  &   e Zd ZU dZdZed/d            Zd	ed
<   ded<   ded<   d0dZd1dZ	d2dZ
d2dZe	 	 d3d4d            Ze	 	 d3d5d            Z	 	 d6d7d"Zej        sd8d%Zed&             Zd9d'Zd:d)Zd;d*Zd<d,Zej        d=d.            ZdS )>rb   aJ  clones a SupportsAnnotations and applies an 'annotations' dictionary.

    Unlike regular clones, this clone also mimics __hash__() and
    __eq__() of the original element so that it takes its place
    in hashed collections.

    A reference to the original element is maintained, for the important
    reason of keeping its hash value current.  When GC'ed, the
    hash value may be reused, causing conflicts.

    .. note::  The rationale for Annotated producing a brand new class,
       rather than placing the functionality directly within ClauseElement,
       is **performance**.  The __hash__() method is absent on plain
       ClauseElement which leads to significantly reduced function call
       overhead, as the use of sets and dictionaries against ClauseElement
       objects is prevalent, but most are not "annotated".

    FelementrZ   r&   r'   r(   c                    	 t           |j                 } n%# t          $ r t          |j        |           } Y nw xY w | ||          S r*   )annotated_classes	__class__KeyError_new_annotation_type)clsr   r&   s      r/   rc   z Annotated._as_annotated_instance  sY    	?#G$56CC 	? 	? 	?&w'8#>>CCC	?s7F###s    77r   r"   _Annotated__elementint_hashr   
Type[Self]argsr   r   c                6    t                               |           S r*   )object__new__)r   r   s     r/   r   zAnnotated.__new__   s    ~~c"""r1   c                   |j                                         | _         | j                             dd            | j                             dd            || _        t	          j        |          | _        t          |          | _        d S Nr@   rt   )	rv   copyrw   r   r   r{   r"   hashr   )r.   r   r&   s      r/   __init__zAnnotated.__init__#  sw      (--//2D999/666  .v66']]


r1   c                d    | j                             |          }|                     |          }|S r*   )r"   ru   rg   )r.   r&   _valuesry   s       r/   r0   zAnnotated._annotate-  s0    #))&11$$W--
r1   c                   | j                             | j                   }| j                                        |_        |j                            dd            |j                            dd            t          j        |          |_        |S r   )r   r   rv   r   rw   r   r{   r"   r5   s      r/   rg   zAnnotated._with_annotations2  sx    &&t~66++--3T:::0$777!/77r1   .r2   r3   r$   c                    d S r*   r!   r5   s      r/   r6   zAnnotated._deannotate:  r7   r1   r8   c                    d S r*   r!   r5   s      r/   r6   zAnnotated._deannotateB  r7   r1   NTr:   r    c                    | j         S |                     t          j        fd| j                                        D                                 S )Nc                $    i | ]\  }}|v	||S r!   r!   )rE   rF   rG   r&   s      r/   
<dictcomp>z)Annotated._deannotate.<locals>.<dictcomp>T  s4       &Cf,, U,,,r1   )r   rg   r   r{   r"   itemsr5   s    ` r/   r6   zAnnotated._deannotateJ  so    
 >>!))"   *.*;*A*A*C*C     r1   visitorkwc                4     | j         j        j        | |fi |S r*   )r   r   _compiler_dispatch)r.   r   r   s      r/   r   zAnnotated._compiler_dispatch^  s0    >4>+>g !#  r1   c                    | j         j        S r*   )r   r[   r]   s    r/   r[   zAnnotated._constructorc  s    >..r1   c                     | j         j        di |}|| j         u r| S |j                            | j                   |                     || j                  S )Nr!   )r   rl   rv   updater   r"   )r.   r   r3   s      r/   rl   zAnnotated._cloneg  s_    %%++++DN""K N!!$-000>>%):;;;r1   'Tuple[Type[Annotated], Tuple[Any, ...]]c                ,    | j         | j        | j        ffS r*   )r   r   r"   r]   s    r/   
__reduce__zAnnotated.__reduce__r  s    ~0ABBBr1   c                    | j         S r*   )r   r]   s    r/   __hash__zAnnotated.__hash__u  s
    zr1   otherc                    | j         r | j        j                            | |          S t	          |          t	          |           k    S r*   )_is_column_operatorsr   r   __eq__r   )r.   r   s     r/   r   zAnnotated.__eq__x  s?    $ 	->+224???;;$t**,,r1   r   c                v    d| j         v r%t          t          | j         d                   j        S | j        j        S )Nr^   )r"   r   rZ   r^   r   r]   s    r/   r^   zAnnotated.entity_namespace~  sC    !222+!"45  
 >22r1   )r   rZ   r&   r'   r(   rb   )r   r   r   r   r(   r   )r   rZ   r&   r'   rM   rN   rO   )r&   r8   r3   r$   r(   rb   )NTrR   )r   r   r   r   r(   r   )r   r   r(   r   )r(   r   )r(   r   )r   r   r(   r$   rn   )rS   rT   rU   __doc__r   classmethodrc   rW   r   r   r0   rg   r   r6   r   r   r   propertyr[   rl   r   r   r   r   ro   r^   r!   r1   r/   rb   rb      s         & !$ $ $ [$ /...****JJJ# # # ## # # #   
     !$    X  !$    X +/    $  	/	 	 	 	
 
	/ 	/ 
	/	< 	< 	< 	<C C C C   - - - - 
"3 3 3 #"3 3 3r1   rb   z8Dict[Type[SupportsWrappingAnnotations], Type[Annotated]]r   _SA)boundto_annotater   r'   r(   c                L    	 | j         } ||          S # t          $ r | cY S w xY wr*   )r0   AttributeError)r   r   r0   s      r/   _safe_annotater     sL    &)	 y%%%     	s    ##F)detect_subquery_colsind_cols_on_fromclauseannotate_callabler   exclude'Optional[Sequence[SupportsAnnotations]]r   r$   r   r   OOptional[Callable[[SupportsAnnotations, _AnnotationDict], SupportsAnnotations]]c               t    i dfd| t          t           |                     } d| S )	zDeep copy the given ClauseElement, annotating each element
    with the given annotations dictionary.

    Elements within the exclude collection will be cloned but not annotated.

    elemr    r   r   r(   c                   	|d<   t          |           }|v r|         S 
r:t          | d          r*| j                            
          r | j        ddi|}nH| j        k    r;	r| j        r | j        ddi|}n| }r |          }nt          |          }n| }|                               ||<   |S )Nr   r#   r3   )r3   r   r!   )	idhasattrr#   intersectionrl   r"   r%   r   _copy_internals)r   r   id_newelemr   r   r   r3   
cloned_idsr   r   r   s        r/   r3   z_deep_annotate.<locals>.clone  s/    &:!"hh*c?" 	k**	 ++G44	
 "dk44444GGD---# #(: #)dk<<<<<"  C++KEE(kBBG0F 	  	
 	
 	
 "
3r1   Nr   r    r   r   r(   r    r   r   )r   r   r   r   r   r   r3   r   s    `````@@r/   _deep_annotater     sv    * 24J' ' ' ' ' ' ' ' ' ' ' 'R sEE'NN++ENr1   r2   r&   r:   c                    d S r*   r!   r   r&   s     r/   _deep_deannotater     	     Cr1   c                    d S r*   r!   r   s     r/   r   r     r   r1   Optional[_SA]c                d    i dfd| t          t           |                     } d| S )	z2Deep copy the given element, removing annotations.r   r    r   r   r(   c                    rt          |           }n| }|vr4|                     d          }|                               ||<   |S |         S )NT)r&   r3   )r3   )r   r6   r   )r   r   rF   r   r3   clonedr&   s       r/   r3   z_deep_deannotate.<locals>.clone  so     	T((CCCf&&fD&AAG##%#000!F3KN#;r1   Nr   r   )r   r&   r3   r   s    `@@r/   r   r     s]    
 .0F        sEE'NN++ENr1   c                X    |                      |          } |                                  | S )a  Annotate the given ClauseElement and copy its internals so that
    internal objects refer to the new annotated object.

    Basically used to apply a "don't traverse" annotation to a
    selectable, without digging throughout the whole
    structure wasting time.
    )r0   r   )r   r   s     r/   _shallow_annotater     s.     ,,GNr1   r   !Type[SupportsWrappingAnnotations]base_clsType[Annotated]c                   t          | t                    r| S | t          v rt          |          S | j        D ]}|t          v rt          |         } nt	          t
          t                   t          d| j        z  || fi                     xt          | <   }|t                      d| j        z  <   d| j	        v r*t          | j                  dt          j        fgz   |_        nD| j	                            dd          r)t          | j                  dt          j        fgz   |_        | j	                            dd          rd|_        nd| j	        v r| j	        d         |_        t          | t           j                  |_        |S )z[Generates a new class that subclasses Annotated and proxies a given
    element type.

    zAnnotated%s_traverse_internalsr"   inherit_cacheFT)
issubclassrb   r   __mro__r   r   typerS   globalsrv   listr   r   r   getr   r   ColumnOperatorsr   )r   r   super_anno_clss       r/   r   r     s    #y!! &
	!	!	! %%+   &&&(0HE ' )-Y]S\)Hc?B??) ) cX /7GIImcl*+,,'+C,C'D'D.ABH
 (
$$ 
		/5	1	1 
'+C,C'D'D.ABH
 (
$ |// ?!%	CL	(	(!$o!>$.sI4M$N$NH!Or1   target_hierarchyNonec                T    t          j        |           D ]}t          ||           d S r*   )r   walk_subclassesr   )r   r   r   s      r/   _prepare_annotationsr   M  s<     #$455 , ,S(++++, ,r1   )r   r   r   r'   r(   r   r*   )r   r   r   r'   r   r   r   r$   r   r$   r   r   r(   r   )r   r2   r&   r:   r(   r2   )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   )1r   
__future__r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r    r   	cache_keyr   visitorsr   r   r   r   util.typingr   r   baser   r   strr'   
EMPTY_DICTr   rW   r    rZ   rq   rb   r   r   r   r   r   r   r   r   r!   r1   r/   <module>r      s     # " " " " "                                                                                            " " " " " "       + + + + + + ' ' ' ' ' '       ! ! ! ! ! !       1&&&&&&000000#s(#26/  A A A A:
 :
 :
 :
 :
/ :
 :
 :
z: : : : :"5 : : :zJ J J J J: J J JZJ3 J3 J3 J3 J3# J3 J3 J3h       ge0111	& 	& 	& 	& 8<C
 "'#( 	C C C C C CL 
>B    
 
48    
 ?C    8
 
 
 
, , , ,^, , , , , ,r1   