U
    [h                     @  s   U d 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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mZ d d%l,m-Z- d d&l,m.Z. d d'l/m0Z0 d d(l1m2Z2 d d)l1m3Z3 d d*l1m4Z4 ejr*dd+l5m6Z6 dd,l5m7Z7 dd-lm8Z8 dd.lm9Z9 dd/lm:Z: dd0l;m<Z< dd1l=m>Z> dd2l?m@Z@ dd3lAmBZB dd4lAmCZC dd5lDmEZE d d6lFmGZG d d7lHmIZI d d8lJmKZK d d9lJmLZL d d:l/mMZM d d;l/mNZN d d<lOmPZP d d=lQmRZR d d>lSmTZT d d?l1mUZU d d@l1mVZV d dAl1mWZW d dBl1mXZX edCe	dDZYedEe	dDZZG dFdG dGe$eY Z[ee\dHdIdJdKf Z]edLe	dDZ^e_ Z`dMeadN< G dOdP dPe#e^ e"e[e^ ZbG dQdR dRebeY eeY ZcG dSdT dTe[eY ZdG dUdV dVe[eY ZeG dWdX dXeeeY eeY ZfdS )YzDescriptor properties are more "auxiliary" properties
that exist as configurational elements, but don't participate
as actively in the load/persist ORM loop.

    )annotations)is_dataclassN)Any)Callable)Dict)List)NoReturn)Optional)Sequence)Tuple)Type)TYPE_CHECKING)TypeVar)Union   )
attributes)util)_DeclarativeMapped)LoaderCallableStatus)Mapped)PassiveFlag)SQLORMOperations)_AttributeOptions)_IntrospectsAnnotations)_MapsColumns)MapperProperty)PropComparator)	_none_set)de_stringify_annotation   )event)exc)schema)sql
expression)	operators)BindParameter)get_args)
is_fwd_ref)	is_pep593)_InstanceDict)_RegistryType)History)InstrumentedAttribute)QueryableAttribute)ORMCompileState)_ClassScanMapperConfig)Mapper)ColumnPropertyMappedColumn)InstanceState)
Connection)Row)_DMLColumnArgument)	_InfoType)
ClauseList)ColumnElement)OperatorType)Column)Select)_AnnotationScanType)CallableReference)DescriptorReference)RODescriptorReference_T)Zbound_PTc                   @  sf   e Zd ZU dZdZded< dZdZded< dd	d
dZe	j
fdddddddZdddddZdS )DescriptorPropertyzO:class:`.MapperProperty` which proxies access to a
    user-defined descriptor.NOptional[str]docFzDescriptorReference[Any]
descriptor!Sequence[QueryableAttribute[Any]]returnc                 C  s   t dd S )Nz?This MapperProperty does not implement column loader strategiesNotImplementedErrorself rQ   E./venv/lib/python3.8/site-packages/sqlalchemy/orm/descriptor_props.py_column_strategy_attrsf   s    z)DescriptorProperty._column_strategy_attrsInstanceState[Any]r+   r   r-   statedict_passiverL   c                 C  s
   t  d S NrM   rP   rV   rW   rX   rQ   rQ   rR   get_historyk   s    zDescriptorProperty.get_historyMapper[Any]NonemapperrL   c                   s   G fdddt j}jd krHt jjd } j|rH|_jd krddddfdd}dddfd	d
}dddfdd}t|||d_t jj	jjj fddj
d}|j|_ jj| d S )Nc                      sZ   e Zd ZdZdZdZedd fddZddd	d
Ze	j
fddddd fddZdS )z7DescriptorProperty.instrument_class.<locals>._ProxyImplFTboolrK   c                   s    j S rY   )uses_objectsrO   proprQ   rR   ra   {   s    zDDescriptorProperty.instrument_class.<locals>._ProxyImpl.uses_objectsstrkeyc                 S  s
   || _ d S rY   re   )rP   rf   rQ   rQ   rR   __init__   s    z@DescriptorProperty.instrument_class.<locals>._ProxyImpl.__init__rT   r+   r   r-   rU   c                   s     |||S rY   )r[   rZ   rb   rQ   rR   r[      s    zCDescriptorProperty.instrument_class.<locals>._ProxyImpl.get_historyN)__name__
__module____qualname__Zaccepts_scalar_loaderZload_on_unexpireZ
collectionpropertyra   rg   r   PASSIVE_OFFr[   rQ   rb   rQ   rR   
_ProxyImplv   s   rm   r   r]   )objvaluerL   c                   s   t |  j| d S rY   )setattrname)rn   ro   rO   rQ   rR   fset   s    z1DescriptorProperty.instrument_class.<locals>.fset)rn   rL   c                   s   t |  j d S rY   )delattrrq   rn   rO   rQ   rR   fdel   s    z1DescriptorProperty.instrument_class.<locals>.fdelc                   s   t |  jS rY   )getattrrq   rt   rO   rQ   rR   fget   s    z1DescriptorProperty.instrument_class.<locals>.fget)rw   rr   ru   c                     s
     S rY   )_comparator_factoryrQ   )r_   rP   rQ   rR   <lambda>       z5DescriptorProperty.instrument_class.<locals>.<lambda>)rH   Zoriginal_property)r   ZAttributeImplrI   rv   class_rf   Z_is_userland_descriptorrk   Zcreate_proxied_attributeparentrH   implclass_managerZinstrument_attribute)rP   r_   rm   Zdescrr   ru   rw   
proxy_attrrQ   )r_   rc   rP   rR   instrument_classs   s*    


z#DescriptorProperty.instrument_class)rh   ri   rj   __doc__rH   __annotations__ra   Z_links_to_entityrS   r   rl   r[   r   rQ   rQ   rQ   rR   rF   [   s   
	rF   z
Column[_T]zMappedColumn[_T]zInstrumentedAttribute[_T]z
Mapped[_T]_CCzFweakref.WeakKeyDictionary[Type[Any], Callable[[Any], Tuple[Any, ...]]]_composite_gettersc                      s@  e Zd ZU dZded< ded< ded< ded	< dcd
ddd
d
d
ddddddddddd	 fddZddd fddZdddd d!Zdd"d#d$Ze	 Z
dd"d%d&Zed'd(d)d*dd+d,d-d-ddd.
d/d0Zdd"d1d2Zed'ed3d(d)d*dd+dd4d5d6Zejd7d"d8d9Zejed:d;d"d<d=Zd7d"d>d?Zejed:d@d"dAdBZedCd"dDdEZedFd"dGdHZed:dd"dIdJZdd"dKdLZejdMd"dNdOZdPd"dQdRZejfdSdTdUdVdWdXdYZ ddZdd[d\Z!G d]d^ d^e"j#e$ Z%G d_d` d`e&e' Z(d+d"dadbZ)  Z*S )dCompositePropertyzDefines a "composite" mapped attribute, representing a collection
    of columns as one attribute.

    :class:`.CompositeProperty` is constructed using the :func:`.composite`
    function.

    .. seealso::

        :ref:`mapper_composite`

    z$Union[Type[_CC], Callable[..., _CC]]composite_classz#Tuple[_CompositeAttrType[Any], ...]attrsz=CallableReference[Optional[Callable[[_CC], Tuple[Any, ...]]]]_generated_composite_accessorzType[Comparator[_CC]]comparator_factoryNF)attribute_optionsactive_historydeferredgroupr   infozCUnion[None, Type[_CC], Callable[..., _CC], _CompositeAttrType[Any]]z_CompositeAttrType[Any]Optional[_AttributeOptions]r`   rG   zOptional[Type[Comparator[_CC]]]Optional[_InfoType]r   )	_class_or_attrr   r   r   r   r   r   r   kwargsc          
        s   t  j|d t|tttjfr4|f| | _d | _n|| _|| _|| _	|| _
|| _|d k	r^|n| jj| _d | _|d k	r| j| t|  |   |   d S N)r   )superrg   
isinstancer   rd   r#   r<   r   r   r   r   r   	__class__
Comparatorr   r   r   updater   set_creation_order_create_descriptor_init_accessor)
rP   r   r   r   r   r   r   r   r   r   r   rQ   rR   rg      s&    
zCompositeProperty.__init__r\   r]   r^   c                   s   t  | |   d S rY   )r   r   _setup_event_handlersrP   r_   r   rQ   rR   r      s    z"CompositeProperty.instrument_classr   zTuple[Any, ...]ro   rL   c              
   C  s`   | j r|  |S z
|j}W n: tk
rT } ztd| jj d|W 5 d }~X Y nX | S d S )NzComposite class zX is not a dataclass and does not define a __composite_values__() method; can't get state)r   __composite_values__AttributeErrorsa_excInvalidRequestErrorr   rh   )rP   ro   ZaccessorZaerQ   rQ   rR   _composite_values_from_instance   s    

z1CompositeProperty._composite_values_from_instancerK   c                 C  s   |    dS )zwInitialization which occurs after the :class:`.Composite`
        has been associated with its parent mapper.

        N)_setup_arguments_on_columnsrO   rQ   rQ   rR   do_init  s    zCompositeProperty.do_initc                   sT   dddfdd dddd fdd}ddd fd	d
}t  ||_dS )ztCreate the Python descriptor that will serve as
        the access point on instances of the mapped class.

        r   )instancerL   c                   s   t  }t  }j|krx fddjD }j|krx|jd k	sPt|sxj| |j< |jj	
|jjg |jd S )Nc                   s   g | ]}t  |qS rQ   )rv   .0rf   r   rQ   rR   
<listcomp>#  s    zFCompositeProperty._create_descriptor.<locals>.fget.<locals>.<listcomp>)r   instance_dictinstance_staterf   _attribute_keysr   
issupersetr   managerdispatchrefresh_COMPOSITE_FGETget)r   rW   rV   valuesrO   r   rR   rw     s$    



	
  z2CompositeProperty._create_descriptor.<locals>.fgetr]   )r   ro   rL   c                   s   t | }t | }|jj }|jjr2 | }n|jtj	}|jj
D ]}|||||j}qJ||j< |d krjD ]}t| |d  qxn(tj|D ]\}}t| || qd S rY   )r   r   r   r   rf   r   _active_historyr   r   NO_VALUEsetr}   r   rp   zipr   )r   ro   rW   rV   attrpreviousfnrf   rw   rP   rQ   rR   rr   6  s"    




z2CompositeProperty._create_descriptor.<locals>.fsetc                   s   t | }t | }|jj }|jjr@ | }|jd  n|jtj	}|jj }|j
|||j jD ]}t| |d  qtd S rY   )r   r   r   r   rf   r   r   popr   r   remover}   r   rp   )r   rV   rW   r   r   rf   r   rQ   rR   ru   M  s    


z2CompositeProperty._create_descriptor.<locals>.fdelN)rk   rI   )rP   rr   ru   rQ   r   rR   r     s    z$CompositeProperty._create_descriptorsqlalchemy.orm.propertiesr1   r,   z	Type[Any]rd   zOptional[Type[Mapped[Any]]]zOptional[_AnnotationScanType])
	decl_scanregistryclsoriginating_modulerf   mapped_container
annotationextracted_mapped_annotationis_dataclass_fieldrL   c
                 C  s  t jjj}
| jd kr(|d kr(| || |}t|r@t|d }|r| jd krt|t	sdt
|ddr|d krt|dr||jnt	|}td| d| dt|||dd}|| _t| jr| ||||| n0| jD ](}t||
tjfr|jd krtd	q|   d S )
Nr   T)Zcheck_generic__forward_arg__zCan't use forward ref z9 for composite class argument; set up the type as Mapped[])Zinclude_genericzIComposite class column arguments must be named unless a dataclass is used)r   	preloadedorm_propertiesr5   r   Z_raise_for_requiredr*   r(   r   rd   r)   hasattrr   r   ArgumentErrorr   r   _setup_for_dataclassr   r"   r>   rq   r   )rP   r   r   r   r   rf   r   r   r   r   r5   argumentZstr_argr   rQ   rQ   rR   declarative_scan_  s\    
    
    
z"CompositeProperty.declarative_scanc                   s   t | jr`t| jds`t| j}tjdd |j D   t	|jdkrZ fdd| _
n | _
| jd k	rt| jtr| jtkr| j
d k	r| j
t| j< nt| jdrdd t| j< d S )Nr   c                 S  s   g | ]
}|j qS rQ   )rq   )r   prQ   rQ   rR   r     s     z4CompositeProperty._init_accessor.<locals>.<listcomp>r   c                   s
    | fS rY   rQ   rt   getterrQ   rR   ry     rz   z2CompositeProperty._init_accessor.<locals>.<lambda>c                 S  s   |   S rY   r   rt   rQ   rQ   rR   ry     rz   )r   r   r   inspect	signatureoperator
attrgetter
parametersr   lenr   r   typer   )rP   insprQ   r   rR   r     s.     


z CompositeProperty._init_accessorzsqlalchemy.orm.decl_base)r   r   r   r   rf   rL   c              
   C  s   t jjj}t jj}t| j}t	|j
 | jD ]\}	}
|	d krntdt| j d| jj dt|j
 |
d kr| }
|  j|
f7  _t|
|r|
||||||	j|	j q2t|
tjr2||	j|
 q2d S )Nznumber of composite attributes z3 exceeds that of the number of attributes in class  )r   r   r   r5   Zorm_decl_baser   r   r   	itertoolszip_longestr   r   r   r   r   r   rh   r   Zdeclarative_scan_for_compositerq   r   r"   r>   Z_undefer_column_name)rP   r   r   r   r   rf   r5   	decl_baser   Zparamr   rQ   rQ   rR   r     s4    

 $
	z&CompositeProperty._setup_for_dataclassrJ   c                   s    fdd j D S )Nc                   s   g | ]}t  jj|jqS rQ   )rv   r|   r{   rf   r   rc   rO   rQ   rR   r     s     z:CompositeProperty._comparable_elements.<locals>.<listcomp>propsrO   rQ   rO   rR   _comparable_elements  s    z&CompositeProperty._comparable_elementszorm.propertieszSequence[MapperProperty[Any]]c                 C  s   g }t jjj}| jD ]}t|tr4| jj|dd}nLt|t	j
rN| jj| }n2t||rh| jj|j }nt|tjr||j}nd }t|tstd||| q|S )NF)Z_configure_mapperszYComposite expects Column objects or mapped attributes/attribute names as arguments, got: )r   r   r   r5   r   r   rd   r|   Zget_propertyr"   r>   _columntopropertycolumnr   r.   rk   r   r   r   append)rP   r   r5   r   rc   rQ   rQ   rR   r     s$    




zCompositeProperty.propsc                 C  s   | j S rY   )r   rO   rQ   rQ   rR   rS      s    z(CompositeProperty._column_strategy_attrszSequence[Column[Any]]c                   s   t jjj  fdd| jD S )Nc                   s0   g | ](}t |tj frt | r(|jn|qS rQ   )r   r"   r>   r   )r   ar4   rQ   rR   r     s   z-CompositeProperty.columns.<locals>.<listcomp>)r   r   r   r5   r   rO   rQ   r4   rR   columns  s    

zCompositeProperty.columnszOptional[MapperProperty[_CC]]c                 C  s   | S rY   rQ   rO   rQ   rQ   rR   mapper_property_to_assign  s    z+CompositeProperty.mapper_property_to_assignz$List[Tuple[schema.Column[Any], int]]c                 C  s   dd | j D S )Nc                 S  s   g | ]}|j d kr|dfqS )Nr   )table)r   crQ   rQ   rR   r     s     
 z7CompositeProperty.columns_to_assign.<locals>.<listcomp>)r   rO   rQ   rQ   rR   columns_to_assign  s    z#CompositeProperty.columns_to_assignc                 C  sP   t jjj}| jD ]:}t||s"qn|}| j|_| jrB| j|_d|_| j	|_	qdS )zwPropagate configuration arguments made on this composite
        to the target columns, for those that apply.

        ))r   T)Z
instrumentTN)
r   r   r   r3   r   r   r   r   Zstrategy_keyr   )rP   r3   rc   ZcproprQ   rQ   rR   r     s    


z-CompositeProperty._setup_arguments_on_columnsc                   s
  dddd fdd}ddddd fd	d
}ddddddfdd ddddfdd}dddddfdd}t jjd|dd t jjd|dd t jjd|ddd t jjd|ddd t jjd|ddd jjj }|j|j_j|jj_dS ) z>Establish events that populate/expire the composite attribute.rT   r0   r]   )rV   contextrL   c                   s    | |d dd d S )NF
is_refreshrQ   )rV   r   )_load_refresh_handlerrQ   rR   load_handler,  s    z=CompositeProperty._setup_event_handlers.<locals>.load_handlerzOptional[Sequence[str]])rV   r   to_loadrL   c                   s.   |rj hj|r* | ||dd d S )NTr   )rf   unionr   intersection)rV   r   r   r   rP   rQ   rR   refresh_handler1  s    z@CompositeProperty._setup_event_handlers.<locals>.refresh_handlerr`   )rV   r   r   r   rL   c                   s`    j }|r|jkr"j|kr"d S jD ]}||kr( d S q(j fddjD  |j< d S )Nc                   s   g | ]} j | qS rQ   )dictr   rV   rQ   rR   r   [  s     zZCompositeProperty._setup_event_handlers.<locals>._load_refresh_handler.<locals>.<listcomp>)r   r   rf   r   r   )rV   r   r   r   rW   krO   r   rR   r   =  s    
zFCompositeProperty._setup_event_handlers.<locals>._load_refresh_handler)rV   keysrL   c                   s,   |d kst  j|r(| j jd  d S rY   )r   r   r   r   r   rf   )rV   r   rO   rQ   rR   expire_handler^  s    z?CompositeProperty._setup_event_handlers.<locals>.expire_handlerr\   r7   )r_   
connectionrV   rL   c                   s   |j  jd dS )zAfter an insert or update, some columns may be expired due
            to server side defaults, or re-populated due to client side
            defaults.  Pop out the composite value here so that it
            recreates.

            N)r   r   rf   )r_   r   rV   rO   rQ   rR   insert_update_handlerd  s    zFCompositeProperty._setup_event_handlers.<locals>.insert_update_handlerZafter_insertT)rawZafter_updateload)r   Z	propagater   ZexpireN)	r    Zlistenr|   r~   rf   r   r}   r   r   )rP   r   r   r   r   r   rQ   r   rR   r   )  sR    !                  
z'CompositeProperty._setup_event_handlersSequence[str]c                 C  s   dd | j D S )Nc                 S  s   g | ]
}|j qS rQ   re   r   rQ   rQ   rR   r     s     z5CompositeProperty._attribute_keys.<locals>.<listcomp>r   rO   rQ   rQ   rR   r     s    z!CompositeProperty._attribute_keysz Callable[[Dict[str, Any]], None]c                   sJ   j rj nddddddd jD  dd	d
 fdd}|S )Nr   z
Tuple[Any])valrL   c                 S  s   |   S rY   r   )r  rQ   rQ   rR   
get_values  s    zOCompositeProperty._populate_composite_bulk_save_mappings_fn.<locals>.get_valuesc                 S  s   g | ]
}|j qS rQ   re   r   rQ   rQ   rR   r     s     zOCompositeProperty._populate_composite_bulk_save_mappings_fn.<locals>.<listcomp>zDict[str, Any]r]   )	dest_dictrL   c              	     s*   |  dd t | jD  d S )Nc                 S  s   i | ]\}}||qS rQ   rQ   )r   rf   r  rQ   rQ   rR   
<dictcomp>  s    zaCompositeProperty._populate_composite_bulk_save_mappings_fn.<locals>.populate.<locals>.<dictcomp>)r   r   r   rf   )r  r   r  rP   rQ   rR   populate  s     zMCompositeProperty._populate_composite_bulk_save_mappings_fn.<locals>.populate)r   r   )rP   r  rQ   r  rR   )_populate_composite_bulk_save_mappings_fn  s    
z;CompositeProperty._populate_composite_bulk_save_mappings_fnrT   r+   r   r-   rU   c                 C  s   g }g }d}| j D ]j}|j}|j| j||}	|	 r<d}|	 }
|
rT||
 n
|d |	j	rr||	j	 q|d q|rt
| j| gd| j| gS t
d| j| gdS dS )z>Provided for userland code that uses attributes.get_history().FTNrQ   )r   rf   r   r}   r[   Zhas_changesnon_deletedextendr   deletedr   r-   r   )rP   rV   rW   rX   Zaddedr  Zhas_historyrc   rf   Zhistr
  rQ   rQ   rR   r[     s,    



zCompositeProperty.get_historyzComposite.Comparator[_CC]c                 C  s   |  | |S rY   )r   r   rQ   rQ   rR   rx     s    z%CompositeProperty._comparator_factoryc                      s8   e Zd Zddd fddZdddd	d
ddZ  ZS )z!CompositeProperty.CompositeBundlezComposite[_T]r;   )	property_exprc                   s   || _ t j|jf|  d S rY   )rk   r   rg   rf   )rP   r  r  r   rQ   rR   rg     s    z*CompositeProperty.CompositeBundle.__init__zSelect[Any]z#Sequence[Callable[[Row[Any]], Any]]r  zCallable[[Row[Any]], Any])queryprocslabelsrL   c                   s   ddd fdd}|S )NzRow[Any]r   )rowrL   c                   s   j j fddD  S )Nc                   s   g | ]}| qS rQ   rQ   )r   procr  rQ   rR   r     s     zXCompositeProperty.CompositeBundle.create_row_processor.<locals>.proc.<locals>.<listcomp>)rk   r   r  r  rP   r  rR   r    s    zDCompositeProperty.CompositeBundle.create_row_processor.<locals>.procrQ   )rP   r  r  r  r  rQ   r  rR   create_row_processor  s    z6CompositeProperty.CompositeBundle.create_row_processor)rh   ri   rj   rg   r  __classcell__rQ   rQ   r   rR   CompositeBundle  s   r  c                   @  s   e Zd ZU dZdZded< ejddddZd	dd
dZ	ejd	dddZ
dddddZejddddZdddddZdddddZdddddZdddddZdddd d!Zdddd"d#Zd$ddd%d&d'ZdS )(zCompositeProperty.Comparatora  Produce boolean, comparison, and other operators for
        :class:`.Composite` attributes.

        See the example in :ref:`composite_operations` for an overview
        of usage , as well as the documentation for :class:`.PropComparator`.

        .. seealso::

            :class:`.PropComparator`

            :class:`.ColumnOperators`

            :ref:`types_operators`

            :attr:`.TypeEngine.comparator_factory`

        Nz%RODescriptorReference[Composite[_PT]]rc   r;   rK   c                 C  s   t j| jddiS )Nr   F)r%   r;   r   rO   rQ   rQ   rR   clauses  s
    z$CompositeProperty.Comparator.clausesz&CompositeProperty.CompositeBundle[_PT]c                 C  s   | j S rY   r$   rO   rQ   rQ   rR   __clause_element__  s    z/CompositeProperty.Comparator.__clause_element__c                 C  s*   | j | j| j| jjd}t| j|S )N)ZparententityZparentmapperZ	proxy_key)r  Z	_annotateZ_parententityrc   rf   r   r  )rP   r  rQ   rQ   rR   r%     s    z'CompositeProperty.Comparator.expressionr   z(Sequence[Tuple[_DMLColumnArgument, Any]]r   c                 C  sz   t |tr|j}|d kr,dd | jjD }n>t | jjtrVt || jjrV| j|}nt	d| j|f t
t| j|S )Nc                 S  s   g | ]}d qS rY   rQ   r   rQ   rQ   rR   r     s     zDCompositeProperty.Comparator._bulk_update_tuples.<locals>.<listcomp>z)Can't UPDATE composite attribute %s to %r)r   r'   ro   rc   r   r   r   r   r   r   listr   r   )rP   ro   r   rQ   rQ   rR   _bulk_update_tuples  s"    
 z0CompositeProperty.Comparator._bulk_update_tuplesrJ   c                   s(    j r fdd jjD S  jjS d S )Nc                   s   g | ]}t  jj|jqS rQ   )rv   _adapt_to_entityZentityrf   r   rO   rQ   rR   r   +  s   zECompositeProperty.Comparator._comparable_elements.<locals>.<listcomp>)r  rc   r   rO   rQ   rO   rR   r   (  s
    
z1CompositeProperty.Comparator._comparable_elementszColumnElement[bool])otherrL   c                 C  s   |  tj|S rY   )_comparer&   eqrP   r  rQ   rQ   rR   __eq__2  s    z#CompositeProperty.Comparator.__eq__c                 C  s   |  tj|S rY   )r  r&   ner!  rQ   rQ   rR   __ne__5  s    z#CompositeProperty.Comparator.__ne__c                 C  s   |  tj|S rY   )r  r&   ltr!  rQ   rQ   rR   __lt__8  s    z#CompositeProperty.Comparator.__lt__c                 C  s   |  tj|S rY   )r  r&   gtr!  rQ   rQ   rR   __gt__;  s    z#CompositeProperty.Comparator.__gt__c                 C  s   |  tj|S rY   )r  r&   ler!  rQ   rQ   rR   __le__>  s    z#CompositeProperty.Comparator.__le__c                 C  s   |  tj|S rY   )r  r&   ger!  rQ   rQ   rR   __ge__A  s    z#CompositeProperty.Comparator.__ge__r=   )r   r  rL   c                   st   |d krd gt jj }nj|} fddtjj|D }jrjjd k	sXtfdd|D }tj	| S )Nc                   s   g | ]\}} ||qS rQ   rQ   )r   r   b)r   rQ   rR   r   S  s   z9CompositeProperty.Comparator._compare.<locals>.<listcomp>c                   s   g | ]}  |qS rQ   )adapter)r   xrO   rQ   rR   r   Y  s     )
r   rc   r   r   r   r  r.  AssertionErrorr#   and_)rP   r   r  r   ZcomparisonsrQ   )r   rP   rR   r  K  s    
z%CompositeProperty.Comparator._compare)rh   ri   rj   r   __hash__r   r   memoized_propertyr  r  r%   r  r   r"  r$  r&  r(  r*  r,  r  rQ   rQ   rQ   rR   r     s$   

	
r   c                 C  s   t | jjjd | j S )N.)rd   r|   r{   rh   rf   rO   rQ   rQ   rR   __str__\  s    zCompositeProperty.__str__)N)+rh   ri   rj   r   r   rg   r   r   r   objectr   r   r   preload_moduler   r   r   r3  r   r   rS   Znon_memoized_propertyr   rk   r   r   r   r   r   r	  r   rl   r[   rx   orm_utilZBundlerD   r  r   rE   r   r5  r  rQ   rQ   r   rR   r      sd   
 &(J";)_%xr   c                   @  s   e Zd ZdZdZdS )	CompositeaS  Declarative-compatible front-end for the :class:`.CompositeProperty`
    class.

    Public constructor is the :func:`_orm.composite` function.

    .. versionchanged:: 2.0 Added :class:`_orm.Composite` as a Declarative
       compatible subclass of :class:`_orm.CompositeProperty`.

    .. seealso::

        :ref:`mapper_composite`

    TNrh   ri   rj   r   Zinherit_cacherQ   rQ   rQ   rR   r9  `  s   r9  c                      s6   e Zd ZdZdddddZdd fd	d
Z  ZS )ConcreteInheritedPropertya4  A 'do nothing' :class:`.MapperProperty` that disables
    an attribute on a concrete subclass that is only present
    on the inherited mapper, not the concrete classes' mapper.

    Cases where this occurs include:

    * When the superclass mapper is mapped against a
      "polymorphic union", which includes all attributes from
      all subclasses.
    * When a relationship() is configured on an inherited mapper,
      but not on the subclass mapper.  Concrete mappers require
      that relationship() is configured explicitly on each
      subclass.

    r\   zType[PropComparator[_T]]r^   c                 C  sP   d }| j  D ]*}|j| j }t|dd d k	r|j} q:q|d k	sFt|||S )Nr   )r|   Ziterate_to_rootZ_propsrf   rv   r   r0  )rP   r_   Zcomparator_callablemr   rQ   rQ   rR   rx     s    z-ConcreteInheritedProperty._comparator_factoryr]   rK   c                   s<   t    dd fddG  fddd}|  _d S )Nr   rK   c                     s   t d j j jf d S )NzgConcrete %s does not implement attribute %r at the instance level.  Add this property explicitly to %s.)r   r|   rf   rQ   rO   rQ   rR   warn  s
    z0ConcreteInheritedProperty.__init__.<locals>.warnc                      sT   e Zd ZdddddfddZddddfddZddddd	 fd
dZdS )zDConcreteInheritedProperty.__init__.<locals>.NoninheritedConcretePropr   r   )srn   ro   rL   c                   s
      d S rY   rQ   )r>  rn   ro   r=  rQ   rR   __set__  s    zLConcreteInheritedProperty.__init__.<locals>.NoninheritedConcreteProp.__set__)r>  rn   rL   c                   s
      d S rY   rQ   )r>  rn   r?  rQ   rR   
__delete__  s    zOConcreteInheritedProperty.__init__.<locals>.NoninheritedConcreteProp.__delete__)r>  rn   ownerrL   c                   s   |d kr j S   d S rY   )rI   )r>  rn   rB  rP   r=  rQ   rR   __get__  s    zLConcreteInheritedProperty.__init__.<locals>.NoninheritedConcreteProp.__get__N)rh   ri   rj   r@  rA  rD  rQ   rC  rQ   rR   NoninheritedConcreteProp  s   rE  )r   rg   rI   )rP   rE  r   rC  rR   rg     s    
z"ConcreteInheritedProperty.__init__)rh   ri   rj   r   rx   rg   r  rQ   rQ   r   rR   r;  s  s   r;  c                	      s   e Zd ZU dZded< d)dddddd	d
d fddZesLeddddZe	j
ddddZddddZdddddZejfdddd d!d"d#Ze	d$ddd%d&d'd(Z  ZS )*SynonymPropertya8  Denote an attribute name as a synonym to a mapped property,
    in that the attribute will mirror the value and expression behavior
    of another attribute.

    :class:`.Synonym` is constructed using the :func:`_orm.synonym`
    function.

    .. seealso::

        :ref:`synonyms` - Overview of synonyms

    z"Optional[Type[PropComparator[_T]]]r   Nrd   zOptional[bool]zOptional[Any]r   r   rG   )rq   
map_columnrI   r   r   r   rH   c                   sj   t  j|d || _|| _|| _|| _|r2|| _n|rF|jrF|j| _nd | _|r\| j	| t
|  d S r   )r   rg   rq   rG  rI   r   rH   r   r   r   r   r   )rP   rq   rG  rI   r   r   r   rH   r   rQ   rR   rg     s    


zSynonymProperty.__init__r`   rK   c                 C  s   t | jj| jjjS rY   )rv   r|   r{   rq   r}   ra   rO   rQ   rQ   rR   ra     s    zSynonymProperty.uses_objectsz/Union[MapperProperty[_T], SQLORMOperations[_T]]c                 C  sh   t | jj| j}t|dr&t|jtsbt|tj	r8|j
S t|trF|S td| jjj| j|f |jS )Nrk   zGsynonym() attribute "%s.%s" only supports ORM mapped attributes, got %r)rv   r|   r{   rq   r   r   rk   r   r   r/   Z
comparatorr   r   r   rh   )rP   r   rQ   rQ   rR   _proxied_object  s     
zSynonymProperty._proxied_objectrJ   c                 C  s   t | jj| jfS rY   )rv   r|   r{   rq   rO   rQ   rQ   rR   rS     s    z&SynonymProperty._column_strategy_attrsr\   zSQLORMOperations[_T]r^   c                 C  s<   | j }t|tr4| jr$| ||}n|||}|S |S d S rY   )rH  r   r   r   )rP   r_   rc   comprQ   rQ   rR   rx     s    
z#SynonymProperty._comparator_factoryrT   r+   r   r-   rU   c                 C  s"   t | jj| j}|jj|||dS )N)rX   )rv   r|   r{   rq   r}   r[   )rP   rV   rW   rX   r   rQ   rQ   rR   r[     s    zSynonymProperty.get_historyr   r]   )r|   initrL   c                 C  s   t jj}| jr| j|jjkr:td| j	|jj
| jf nN|jj| j |jkr|j|jj| j  j| j	krtd| j| j	| j	| jf ||jj| j }|j| j	||dd | j|_|| _d S )Nz>Can't compile synonym '%s': no column on table '%s' named '%s'zpCan't call map_column=True for synonym %r=%r, a ColumnProperty already exists keyed to the name %r for column %rT)rJ  Z	setparent)r   r   r   rG  rf   Zpersist_selectabler   r   r   rq   descriptionr   r3   Z_configure_propertyZ_mapped_by_synonymr|   )rP   r|   rJ  
propertiesr   rQ   rQ   rR   
set_parent  s@    
zSynonymProperty.set_parent)NNNNNN)rh   ri   rj   r   r   rg   r   rk   ra   r   r3  rH  rS   rx   r   rl   r[   r7  rM  r  rQ   rQ   r   rR   rF    s(   
       	rF  c                   @  s   e Zd ZdZdZdS )SynonymaK  Declarative front-end for the :class:`.SynonymProperty` class.

    Public constructor is the :func:`_orm.synonym` function.

    .. versionchanged:: 2.0 Added :class:`_orm.Synonym` as a Declarative
       compatible subclass for :class:`_orm.SynonymProperty`

    .. seealso::

        :ref:`synonyms` - Overview of synonyms

    TNr:  rQ   rQ   rQ   rR   rN  5  s   rN  )gr   Z
__future__r   Zdataclassesr   r   r   r   typingr   r   r   r   r   r	   r
   r   r   r   r   r   weakref r   r   r8  baser   r   r   r   r   Z
interfacesr   r   r   r   r   r   r   r    r!   r   r"   r#   r%   r&   Zsql.elementsr'   Zutil.typingr(   r)   r*   Z_typingr+   r,   r-   r.   r/   r   r0   r   r1   r_   r2   rL  r3   r5   rV   r6   Zengine.baser7   Z
engine.rowr8   Zsql._typingr9   r:   r;   r<   Zsql.operatorsr=   Z
sql.schemar>   Zsql.selectabler?   r@   rA   rB   rC   rD   rE   rF   rd   Z_CompositeAttrTyper   WeakKeyDictionaryr   r   r   r9  r;  rF  rN  rQ   rQ   rQ   rR   <module>   s   M	

       ,8 