L iLfddlmZddlZddlmZddlmZddlmZddlmZddlmZddlm Z dd lm Z dd lm Z dd lm Z dd lm Z dd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlm Z e rddlm!Z!ddlm"Z"ddlm#Z#ddl$m%Z%ed e!Z&ed"e!Z'Gd#d$ee&Z(Gd%d&e(e&Z)d'd( d1d)Z*Gd*d+Z+d'd( d2d,Z,d'd( d3d-Z-Gd.d/ee&e'fZ. d4d0Z/e.Z0 e-Z1 e,Z2 e*Z3y)5) annotationsN)Any)Callable)Dict)Generic)List)Optional)Sequence)Tuple)Type) TYPE_CHECKING)TypeVar)Union)base) collection)collection_adapter)exc)util) coercions) expression)roles)Missing) MissingOr)Literal)AttributeEventToken)Mapper)CollectionAdapter) ColumnElement_KT)bound_VTc8eZdZdZdZddZ d dZd dZd dZy) _PlainColumnGetterzPlain column getter, stores collection of Column objects directly. Serializes to a :class:`._SerializableColumnGetterV2` which has more expensive __call__() performance and some rare caveats. )cols compositec8||_t|dkD|_yNr)r&lenr')selfr&s f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sqlalchemy/orm/mapped_collection.py__init__z_PlainColumnGetter.__init__:s TQc@tj|jSN)_SerializableColumnGetterV2_reduce_from_colsr&r+s r, __reduce__z_PlainColumnGetter.__reduce__>s +<>: a&C{  s$B N)r&Sequence[ColumnElement[_KT]]returnNonerFcTuple[Type[_SerializableColumnGetterV2[_KT]], Tuple[Sequence[Tuple[Optional[str], Optional[str]]]]]r6z Mapper[_KT]rFrE)r>r!rFz&MissingOr[Union[_KT, Tuple[_KT, ...]]]) __name__ __module__ __qualname____doc__ __slots__r-r4r7rDr.r,r%r%.s.&I'H Hr.r%cJeZdZdZdZ ddZ d dZed dZd dZ y) r1a<Updated serializable getter which deals with multi-table mapped classes. Two extremely unusual cases are not supported. Mappings which have tables across multiple metadata objects, or which are mapped to non-Table selectables linked across inheriting mappers may fail to function here. )colkeysc8||_t|dkD|_yr))rRr*r')r+rRs r,r-z$_SerializableColumnGetterV2.__init__is W)r.c4|j|jffSr0) __class__rRr3s r,r4z&_SerializableColumnGetterV2.__reduce__os ~~ ..r.chdd}|Dcgc]}|j||f}}t|ffScc}w)Ncxt|jtjsy|jjSr0) isinstancetabler TableClauserB)cs r, _table_keyzA_SerializableColumnGetterV2._reduce_from_cols.._table_key|s'aggz'='=>ww{{"r.)r[zColumnElement[_KT]rFz Optional[str])rBr1)clsr&r\r[rRs r,r2z-_SerializableColumnGetterV2._reduce_from_colsws;  # 488aAEE:a=)88*WJ669s/cg}t|jdd}|jD]a\}}||||vr)|j|jj|7|j|j |j|c|S)Nmetadata)getattr local_tablerRappendr[tables)r+r6r&r_ckeytkeys r,r7z!_SerializableColumnGetterV2._colss)+6--z4@,, ;JD$|x/4x3G F..0067 HOOD133D9:  ;  r.N)rRz-Sequence[Tuple[Optional[str], Optional[str]]]rFrGrH)r&rErFrIrJ) rKrLrMrNrOr-r4 classmethodr2r7rPr.r,r1r1[sK I*D* * / / 7 7r.r1Fignore_unpopulated_attributectj|Dcgc](}tjtj |d*}}t |}t||Scc}w)aA dictionary-based collection type with column-based keying. .. versionchanged:: 2.0 Renamed :data:`.column_mapped_collection` to :class:`.column_keyed_dict`. Returns a :class:`.KeyFuncDict` factory which will produce new dictionary keys based on the value of a particular :class:`.Column`-mapped attribute on ORM mapped instances to be added to the dictionary. .. note:: the value of the target attribute must be assigned with its value at the time that the object is being added to the dictionary collection. Additionally, changes to the key attribute are **not tracked**, which means the key in the dictionary is not automatically synchronized with the key value on the target object itself. See :ref:`key_collections_mutations` for further details. .. seealso:: :ref:`orm_dictionary_collection` - background on use :param mapping_spec: a :class:`_schema.Column` object that is expected to be mapped by the target mapper to a particular attribute on the mapped class, the value of which on a particular instance is to be used as the key for a new dictionary entry for that instance. :param ignore_unpopulated_attribute: if True, and the mapped attribute indicated by the given :class:`_schema.Column` target attribute on an object is not populated at all, the operation will be silently skipped. By default, an error is raised. .. versionadded:: 2.0 an error is raised by default if the attribute being used for the dictionary key is determined that it was never populated with any value. The :paramref:`_orm.column_keyed_dict.ignore_unpopulated_attribute` parameter may be set which will instead indicate that this condition should be ignored, and the append operation silently skipped. This is in contrast to the behavior of the 1.x series which would erroneously populate the value in the dictionary with an arbitrary key value of ``None``. mapping_spec)argnamerg)rto_listrexpectrColumnArgumentRoler%_mapped_collection_cls)rjrhqr&keyfuncs r,column_keyed_dictrrsd`l+   111nM D !&G !%A   s-Ac(eZdZdZddZddZddZy) _AttrGetter) attr_namegettercF||_tj||_yr0)ruoperator attrgetterrv)r+rus r,r-z_AttrGetter.__init__s")))4 r.c*|j|}|tj|}|j}|j|j vr@|j }|j|jtj}|tS|StS|Sr0) rvrr9r6ruattrsr<getNO_VALUEr)r+ mapped_objectrCr?mpdict_s r,rDz_AttrGetter.__call__s|kk-( ;'' 6EB~~) ii >;"N  r.c(t|jffSr0)rtrur3s r,r4z_AttrGetter.__reduce__sT^^---r.N)rustr)r~rrFr)rFz$Tuple[Type[_AttrGetter], Tuple[str]])rKrLrMrOr-rDr4rPr.r,rtrts'I5 .r.rtc.tt||S)adA dictionary-based collection type with attribute-based keying. .. versionchanged:: 2.0 Renamed :data:`.attribute_mapped_collection` to :func:`.attribute_keyed_dict`. Returns a :class:`.KeyFuncDict` factory which will produce new dictionary keys based on the value of a particular named attribute on ORM mapped instances to be added to the dictionary. .. note:: the value of the target attribute must be assigned with its value at the time that the object is being added to the dictionary collection. Additionally, changes to the key attribute are **not tracked**, which means the key in the dictionary is not automatically synchronized with the key value on the target object itself. See :ref:`key_collections_mutations` for further details. .. seealso:: :ref:`orm_dictionary_collection` - background on use :param attr_name: string name of an ORM-mapped attribute on the mapped class, the value of which on a particular instance is to be used as the key for a new dictionary entry for that instance. :param ignore_unpopulated_attribute: if True, and the target attribute on an object is not populated at all, the operation will be silently skipped. By default, an error is raised. .. versionadded:: 2.0 an error is raised by default if the attribute being used for the dictionary key is determined that it was never populated with any value. The :paramref:`_orm.attribute_keyed_dict.ignore_unpopulated_attribute` parameter may be set which will instead indicate that this condition should be ignored, and the append operation silently skipped. This is in contrast to the behavior of the 1.x series which would erroneously populate the value in the dictionary with an arbitrary key value of ``None``. rg)rort)rurhs r,attribute_keyed_dictrsV "I%A r.ct||S)aA dictionary-based collection type with arbitrary keying. .. versionchanged:: 2.0 Renamed :data:`.mapped_collection` to :func:`.keyfunc_mapping`. Returns a :class:`.KeyFuncDict` factory with a keying function generated from keyfunc, a callable that takes an entity and returns a key value. .. note:: the given keyfunc is called only once at the time that the target object is being added to the collection. Changes to the effective value returned by the function are not tracked. .. seealso:: :ref:`orm_dictionary_collection` - background on use :param keyfunc: a callable that will be passed the ORM-mapped instance which should then generate a new key to use in the dictionary. If the value returned is :attr:`.LoaderCallableStatus.NO_VALUE`, an error is raised. :param ignore_unpopulated_attribute: if True, and the callable returns :attr:`.LoaderCallableStatus.NO_VALUE` for a particular instance, the operation will be silently skipped. By default, an error is raised. .. versionadded:: 2.0 an error is raised by default if the callable being used for the dictionary key returns :attr:`.LoaderCallableStatus.NO_VALUE`, which in an ORM attribute context indicates an attribute that was never populated with any value. The :paramref:`_orm.mapped_collection.ignore_unpopulated_attribute` parameter may be set which will instead indicate that this condition should be ignored, and the append operation silently skipped. This is in contrast to the behavior of the 1.x series which would erroneously populate the value in the dictionary with an arbitrary key value of ``None``. rg)ro)rqrhs r,keyfunc_mappingrsX ".J r.cPeZdZdZdd d fdZe d d dZ ddZejd d ddZ e je j d dd Ze je j d dd ZxZS) KeyFuncDictaBase for ORM mapped dictionary classes. Extends the ``dict`` type with additional methods needed by SQLAlchemy ORM collection classes. Use of :class:`_orm.KeyFuncDict` is most directly by using the :func:`.attribute_keyed_dict` or :func:`.column_keyed_dict` class factories. :class:`_orm.KeyFuncDict` may also serve as the base for user-defined custom dictionary classes. .. versionchanged:: 2.0 Renamed :class:`.MappedCollection` to :class:`.KeyFuncDict`. .. seealso:: :func:`_orm.attribute_keyed_dict` :func:`_orm.column_keyed_dict` :ref:`orm_dictionary_collection` :ref:`orm_custom_collection` Frgc:||_||_t| |y)ahCreate a new collection with keying provided by keyfunc. keyfunc may be any callable that takes an object and returns an object for use as a dictionary key. The keyfunc will be called every time the ORM needs to add a member by value-only (such as when loading instances from the database) or remove a member. The usual cautions about dictionary keying apply- ``keyfunc(object)`` should return the same output for the life of the collection. Keying based on mutable properties can result in unreachable instances "lost" in the collection. N)rqrhsuperr-)r+rqrh dict_argsrUs r,r-zKeyFuncDict.__init___s!& ,H) )$r.c>t|}|j||Sr0)rupdate)r]rqvaluesadapterrs r, _unreducezKeyFuncDict._unreducevs!%0$8 & r.cdtj|jt|t |ffSr0)rrrqr<rr3s r,r4zKeyFuncDict.__reduce__s2  ! ! T "4(   r.zsqlalchemy.orm.attributesc tj|j}tjj }t ||jsd}n?|j|jvr|j|j}n |j}|rtjd|dytjd|dtj|dtj|d|d ) Nzunknown relationshipz0Attribute keyed dictionary value for attribute 'z' was None; this will raise in a future release. To skip this assignment entirely, Set the "ignore_unpopulated_attribute=True" parameter on the mapped collection factory.z1In event triggered from population of attribute 'za' (potentially from a backref), can't populate value in KeyFuncDict; dictionary key derived from z= is not populated. Ensure appropriate state is set up on the z object before assigning to the z attribute. To skip this assignment entirely, Set the "ignore_unpopulated_attribute=True" parameter on the mapped collection factory.)rr9r6r preloadedorm_attributesrXrrBr{warnsa_excInvalidRequestError instance_str)r+r> initiator warn_onlyr6 attributes relationships r,_raise_for_unpopulatedz"KeyFuncDict._raise_for_unpopulateds$$U+22^^22 )Z%C%CD1L ]]fll *$ll9==9:L$==L  II*^,>? ,,*^, !% 1 1% 89:((/01++7.9> ?  r.c |j|}|tjur"|js|j ||dn-y|t ur$|js|j ||dd}ny|j |||y)z9Add an item by value, consulting the keyfunc for the key.FrNT)rqrr}rhrr __setitem__r+r> _sa_initiatorrBs r,setzKeyFuncDict.setsll5! $-- 44++=E, G^44++=D, e]3r.c \|j|}|tjur!|js|j ||dy|t ur$|js|j ||dd}ny|||k7r"t jd|d||d|d|j||y) zr!r0Union[AttributeEventToken, Literal[None, False]]rrrFrG)r>r!rrrFrG)rKrLrMrNr-rfrr4rpreload_modulerrappenderinternally_instrumentedrremoverr __classcell__)rUs@r,rrEso:.3 %%%%'+ %  %. 04 %  -  !       T45GK))D)  )  )6)V''KO44H4  4(46''KO!-!-H!-  !-(!-r.rcHGfddtttf}|S)Nc&eZdZdfd ZxZS)/_mapped_collection_cls.._MKeyfuncMappedc,t|g|diy)Nrh)rr-)r+rrUrhrqs r,r-z8_mapped_collection_cls.._MKeyfuncMapped.__init__s& G   .J r.)rrrFrG)rKrLrMr-r)rUrhrqs@r,_MKeyfuncMappedrs   r.r)rr!)rqrhrs`` r,roros +c3h/ r.)rjz&Union[Type[_KT], Callable[[_KT], _VT]]rhrrFType[KeyFuncDict[_KT, _KT]])rurrhrrFzType[KeyFuncDict[Any, Any]])rqrrhrrFzType[KeyFuncDict[_KT, Any]])rqrrhrrFr)4 __future__rrxtypingrrrrrr r r r r rrr collectionsrrrrrsqlrrrutil.langhelpersrr util.typingrrrr sql.elementsr r!r#r%r1rrrtrrrroMappedCollectionmapped_collectionattribute_mapped_collectioncolumn_mapped_collectionrPr.r,rs# #+&(!%., e3 e3**Z2"4S"92p*/686#'6! 6r..6=B..59. .h*/. !.#'.! .by-$sCx.y-x  ! AE   $3-r.