zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
U
¸ý°dsMã@sddlmZddlZddlmZddlmZddlmZddlmZddlmZddlm    Z    dd    lm
Z
dd
lm Z dd lm Z dd lm Z dd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZe r.ddlmZddlmZddlm Z ededZ!ededZ"edeegefdZ#Gd d!„d!ee!ƒZ$Gd"d#„d#e$e!ƒZ%d$d%œd&d'd(d)œd*d+„Z&e'ƒZ(Gd,d-„d-ƒZ)d$d%œd.d'd(d/œd0d1„Z*d$d%œdd'd2d3œd4d5„Z+Gd6d7„d7ee!e"fƒZ,dd'd(d3œd8d9„Z-e,Z.e+Z/e*Z0e&Z1dS):é)Ú annotationsN)ÚAny)ÚCallable)ÚDict)ÚGeneric)ÚList)ÚOptional)ÚSequence)ÚTuple)ÚType)Ú TYPE_CHECKING)ÚTypeVar)ÚUnioné)Úbase)Ú
collectioné)Úexc)Úutil)Ú    coercions)Ú
expression)Úroles)ÚLiteral)ÚAttributeEventToken)ÚMapper)Ú ColumnElementÚ_KT)ÚboundÚ_VTÚ_Fc@sReZdZdZdZdddœdd„Zdd    œd
d „Zd dd œdd„Zdddœdd„ZdS)Ú_PlainColumnGetterzÖPlain column getter, stores collection of Column objects
    directly.
 
    Serializes to a :class:`._SerializableColumnGetterV2`
    which has more expensive __call__() performance
    and some rare caveats.
 
    )ÚcolsÚ    compositeúSequence[ColumnElement[_KT]]ÚNone©r!ÚreturncCs||_t|ƒdk|_dS©Nr)r!Úlenr")Úselfr!©r*úWd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\sqlalchemy/orm/mapped_collection.pyÚ__init__9sz_PlainColumnGetter.__init__úcTuple[Type[_SerializableColumnGetterV2[_KT]], Tuple[Sequence[Tuple[Optional[str], Optional[str]]]]]©r&cCs t |j¡S©N)Ú_SerializableColumnGetterV2Ú_reduce_from_colsr!©r)r*r*r+Ú
__reduce__=sz_PlainColumnGetter.__reduce__ú Mapper[_KT]©Úmapperr&cCs|jSr/)r!)r)r6r*r*r+Ú_colsEsz_PlainColumnGetter._colsrzUnion[_KT, Tuple[_KT, ...]])Úvaluer&csXt |¡‰t ˆ¡‰‡‡fdd„| ˆ¡Dƒ}|jr<t|ƒS|d}|dkrPtS|SdS)Ncsg|]}ˆ ˆˆj|¡‘qSr*)Z_get_state_attr_by_columnÚdict)Ú.0Úcol©ÚmÚstater*r+Ú
<listcomp>Lsÿz/_PlainColumnGetter.__call__.<locals>.<listcomp>r)rÚinstance_stateZ _state_mapperr7r"ÚtupleÚ_UNMAPPED_AMBIGUOUS_NONE)r)r8ÚkeyÚobjr*r<r+Ú__call__Hs
 
 þz_PlainColumnGetter.__call__N)    Ú__name__Ú
__module__Ú __qualname__Ú__doc__Ú    __slots__r,r3r7rEr*r*r*r+r -s     r c@sVeZdZdZdZdddœdd„Zdd    œd
d „Zed dd œdd„ƒZdd dœdd„Z    dS)r0a<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.
 
    )Úcolkeysz-Sequence[Tuple[Optional[str], Optional[str]]]r$)rKr&cCs||_t|ƒdk|_dSr')rKr(r")r)rKr*r*r+r,hsz$_SerializableColumnGetterV2.__init__r-r.cCs|j|jffSr/)Ú    __class__rKr2r*r*r+r3nsz&_SerializableColumnGetterV2.__reduce__r#r%cs,dddœdd„‰‡fdd„|Dƒ}t|ffS)NzColumnElement[_KT]z Optional[str])Úcr&cSst|jtjƒsdS|jjSdSr/)Ú
isinstanceÚtablerZ TableClauserC)rMr*r*r+Ú
_table_key}szA_SerializableColumnGetterV2._reduce_from_cols.<locals>._table_keycsg|]}|jˆ|ƒf‘qSr*)rC)r:rM©rPr*r+r?ƒszA_SerializableColumnGetterV2._reduce_from_cols.<locals>.<listcomp>)r0)Úclsr!rKr*rQr+r1vsz-_SerializableColumnGetterV2._reduce_from_colsr4r5cCshg}t|jddƒ}|jD]J\}}|dks8|dks8||krL| |jj|¡q| |j|j|¡q|S)NÚmetadata)ÚgetattrZ local_tablerKÚappendrMZtables)r)r6r!rSZckeyZtkeyr*r*r+r7†sz!_SerializableColumnGetterV2._colsN)
rFrGrHrIrJr,r3Ú classmethodr1r7r*r*r*r+r0Zs r0F©Úignore_unpopulated_attributez&Union[Type[_KT], Callable[[_KT], _VT]]ÚboolzType[KeyFuncDict[_KT, _KT]])Ú mapping_specrXr&cCs(dd„t |¡Dƒ}t|ƒ}t||dS)aÿA 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``.
 
 
    cSsg|]}tjtj|dd‘qS)rZ)Úargname)rÚexpectrZColumnArgumentRole)r:Úqr*r*r+r?¿sÿz%column_keyed_dict.<locals>.<listcomp>rW)rZto_listr Ú_mapped_collection_cls)rZrXr!Úkeyfuncr*r*r+Úcolumn_keyed_dict‘s.þþr`c@s<eZdZdZddœdd„Zdddœdd    „Zd
d œd d „ZdS)Ú _AttrGetter)Ú    attr_nameÚgetterÚstr)rbcCs||_t |¡|_dSr/)rbÚoperatorÚ
attrgetterrc)r)rbr*r*r+r,Ðsz_AttrGetter.__init__r)Ú mapped_objectr&cCsZ| |¡}|dkrVt |¡}|j}|j|jkrR|j}| |jtj¡}|dkrVt    Snt    S|Sr/)
rcrr@r6rbÚattrsr9ÚgetÚNO_VALUErB)r)rgrDr>ÚmpZdict_r*r*r+rEÔs
 
 z_AttrGetter.__call__z$Tuple[Type[_AttrGetter], Tuple[str]]r.cCs t|jffSr/)rarbr2r*r*r+r3ãsz_AttrGetter.__reduce__N)rFrGrHrJr,rEr3r*r*r*r+raÍsrard)rbrXr&cCstt|ƒ|d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``.
 
 
    rW)r^ra)rbrXr*r*r+Úattribute_keyed_dictçs+þrlzType[KeyFuncDict[_KT, Any]])r_rXr&cCs t||dS)aÞA 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``.
 
 
    rW)r^)r_rXr*r*r+Úkeyfunc_mappings,ÿrmcs¶eZdZdZddœdddddœ‡fd    d
„Zedd d d œdd„ƒZddœdd„Ze     d¡d dddddœdd„ƒZ
e j e j d!ddddœdd„ƒƒZe je j d"ddddœdd„ƒƒZ‡ZS)#Ú KeyFuncDicta©Base 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`
 
 
    FrWrrrYr$)r_Ú    dict_argsrXr&cs||_||_tƒj|ŽdS)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)r_rXÚsuperr,)r)r_rXro©rLr*r+r,cszKeyFuncDict.__init__zDict[_KT, _KT]z'KeyFuncDict[_KT, _KT]')r_Úvaluesr&cCst|ƒ}| |¡|Sr/)rnÚupdate)rRr_rrrkr*r*r+Ú    _unreducezs
zKeyFuncDict._unreducezeTuple[Callable[[_KT, _KT], KeyFuncDict[_KT, _KT]], Tuple[Any, Union[Dict[_KT, _KT], Dict[_KT, _KT]]]]r.cCstj|jt|ƒffSr/)rnrtr_r9r2r*r*r+r3‚szKeyFuncDict.__reduce__zsqlalchemy.orm.attributesNrz2Union[AttributeEventToken, Literal[(None, False)]])r8Ú    initiatorÚ    warn_onlyr&c Cs”t |¡j}tjj}t||jƒs&d}n"|j|j    krB|j    |j›}n|j}|r`t 
d|›d¡n0t   d|›dt  |¡›dt  |¡›d|›d    ¡‚dS)    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.)rr@r6rZ    preloadedZorm_attributesrNrrCrhÚwarnÚsa_excÚInvalidRequestErrorZ instance_str)r)r8rurvr6Ú
attributesZ relationshipr*r*r+Ú_raise_for_unpopulatedŠs   
ÿ    (ÿz"KeyFuncDict._raise_for_unpopulated)r8Ú _sa_initiatorr&cCsl| |¡}|tjkr2|js,|j||ddqZdSn(|tkrZ|jsV|j||ddd}ndS| |||¡dS)z9Add an item by value, consulting the keyfunc for the key.F©rvNT)r_rrjrXr{rBÚ __setitem__©r)r8r|rCr*r*r+Úset¶s&    
 
ÿÿzKeyFuncDict.setcCsŠ| |¡}|tjkr.|js*|j||dddS|tkrV|jsR|j||ddd}ndS|||krzt d||||f¡‚| ||¡dS)z<Remove an item by value, consulting the keyfunc for the key.Fr}NTz·Can not remove '%s': collection holds '%s' for key '%s'. Possible cause: is the KeyFuncDict key function based on mutable properties or properties that only obtain values after flush?)    r_rrjrXr{rBrxryÚ __delitem__rr*r*r+ÚremoveÓs2    
 
ÿÿ  ýÿzKeyFuncDict.remove)N)N)N)rFrGrHrIr,rVrtr3rZpreload_moduler{rZappenderZinternally_instrumentedr€Zremoverr‚Ú __classcell__r*r*rqr+rnIs"üý+ýýrncs"G‡‡fdd„dtttfƒ}|S)Ncs(eZdZdddœ‡‡‡fdd„ Z‡ZS)z/_mapped_collection_cls.<locals>._MKeyfuncMappedrr$)ror&cstƒjˆf|ždˆiŽdS)NrX)rpr,)r)ro)rLrXr_r*r+r,ýsÿþýz8_mapped_collection_cls.<locals>._MKeyfuncMapped.__init__)rFrGrHr,rƒr*©rXr_rqr+Ú_MKeyfuncMappedüsr…)rnr)r_rXr…r*r„r+r^ùsr^)2Ú
__future__rreÚtypingrrrrrrr    r
r r r rÚrÚ collectionsrrrxrZsqlrrrZ util.typingrrrZ sql.elementsrrrrr r0r`ÚobjectrBrarlrmrnr^ZMappedCollectionZmapped_collectionZattribute_mapped_collectionZcolumn_mapped_collectionr*r*r*r+Ú<module>s\                          -:ý9ÿ4ý11