U kf @sUdZddlmZddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z dd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlZddlmZddlmZddlmZddlmZddlmZddl m!Z!ejrddl"m#Z#ddl"m$Z$ddl%m&a&ddl%m'a'ddl%m(a(dd l%m)a)dd!l*m+Z+d"d#d$d%d&d'd(d)d*d+g Z,e-Z.egd,fZ/ed-ed.Z0ed/ed.Z1ed0ed.Z2ed1d2d.Z3ed3d4d.Z4Gd5d6d6e!Z5Gd7d,d,e!Z6Gd8d"d"Z7er8d2d9d:d;d#Z8n e9d<Z8Gd=d9d9Z:d{d>d?Z;d@dAdBdCdDZdIdJZ?dKdLZ@dMdNZAdOdPZBd|dQdRZCdSdTZDdUdVZEd}dWdXZFdYdZd[d\ZGdYdZd]d^ZHeIeJfZKd_d_d`dadbdcZLd_d_d`dadddeZMdYdZdfdgZNGdhdidie e0ZOGdjdkdkee0ZPGdldmdme e1e2fZQeReSeOeIePeTeQiZUdneVdo<eReSdpdqdrdseGfeIdtdqdrdseNfeTdudvieHfiZWdweVdx<dydzZXeXeYdS)~aFSupport for collections of mapped entities. The collections package supplies the machinery used to inform the ORM of collection membership changes. An instrumentation via decoration approach is used, allowing arbitrary types (including built-ins) to be used as entity collections without requiring inheritance from a base class. Instrumentation decoration relays membership change events to the :class:`.CollectionAttributeImpl` that is currently managing the collection. The decorators observe function call arguments and return values, tracking entities entering or leaving the collection. Two decorator approaches are provided. One is a bundle of generic decorators that map function arguments and return values to events:: from sqlalchemy.orm.collections import collection class MyClass: # ... @collection.adds(1) def store(self, item): self.data.append(item) @collection.removes_return() def pop(self): return self.data.pop() The second approach is a bundle of targeted decorators that wrap appropriate append and remove notifiers around the mutation methods present in the standard Python ``list``, ``set`` and ``dict`` interfaces. These could be specified in terms of generic decorator recipes, but are instead hand-tooled for increased efficiency. The targeted decorators occasionally implement adapter-like behavior, such as mapping bulk-set methods (``extend``, ``update``, ``__setslice__``, etc.) into the series of atomic mutation events that the ORM requires. The targeted decorators are used internally for automatic instrumentation of entity collection classes. Every collection class goes through a transformation process roughly like so: 1. If the class is a built-in, substitute a trivial sub-class 2. Is this class already instrumented? 3. Add in generic decorators 4. Sniff out the collection interface through duck-typing 5. Add targeted decoration to any undecorated interface method This process modifies the class at runtime, decorating methods and adding some bookkeeping properties. This isn't possible (or desirable) for built-in classes like ``list``, so trivial sub-classes are substituted to hold decoration:: class InstrumentedList(list): pass Collection classes can be specified in ``relationship(collection_class=)`` as types or a function that returns an instance. Collection classes are inspected and instrumented during the mapper compilation phase. The collection_class callable will be executed once to produce a specimen instance, and the type of that specimen will be instrumented. Functions that return built-in types like ``lists`` will be adapted to produce instrumented instances. When extending a known type like ``list``, additional decorations are not generally not needed. Odds are, the extension method will delegate to a method that's already instrumented. For example:: class QueueIsh(list): def push(self, item): self.append(item) def shift(self): return self.pop(0) There's no need to decorate these methods. ``append`` and ``pop`` are already instrumented as part of the ``list`` interface. Decorating them would fire duplicate events, which should be avoided. The targeted decoration tries not to rely on other methods in the underlying collection class, but some are unavoidable. Many depend on 'read' methods being present to properly instrument a 'write', for example, ``__setitem__`` needs ``__getitem__``. "Bulk" methods like ``update`` and ``extend`` may also reimplemented in terms of atomic appends and removes, so the ``extend`` decoration will actually perform many ``append`` operations and not call the underlying method at all. Tight control over bulk operation and the firing of events is also possible by implementing the instrumentation internally in your methods. The basic instrumentation package works under the general assumption that collection mutation will not raise unusual exceptions. If you want to closely orchestrate append and remove events with exception management, internal instrumentation may be the answer. Within your method, ``collection_adapter(self)`` will retrieve an object that you can use for explicit control over triggering append and remove events. The owning object and :class:`.CollectionAttributeImpl` are also reachable through the adapter, allowing for some very sophisticated behavior. ) annotationsN)Any)Callable)cast) Collection)Dict)Iterable)List)NoReturn)Optional)Set)Tuple)Type) TYPE_CHECKING)TypeVar)Union)NO_KEY)exc)utilNO_ARG)inspect_getfullargspec)Protocol)AttributeEventToken)CollectionAttributeImplattribute_keyed_dictcolumn_keyed_dictkeyfunc_mapping KeyFuncDict) InstanceState collectioncollection_adapterr"r rr$mapped_collectioncolumn_mapped_collectionattribute_mapped_collectionMappedCollection_AdaptedCollectionProtocol_T)bound_KT_VT_COLzCollection[Any]_FNCallable[..., Any]c@seZdZdddddZdS)_CollectionConverterProtocolr1r&returncCsdSN)selfr&r8r8J/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/orm/collections.py__call__z%_CollectionConverterProtocol.__call__N)__name__ __module__ __qualname__r;r8r8r8r:r4sr4c@s6eZdZUded<ded<ded<ded<ded <d S) r,CollectionAdapter _sa_adapterr3 _sa_appender _sa_removerzCallable[..., Iterable[Any]] _sa_iteratorr4 _sa_converterN)r=r>r?__annotations__r8r8r8r:r,s c@seZdZdZeddZeddZeddZedd Zee d d d d Z eddZ eddZ eddZeddZdS)r&auDecorators for entity collection classes. The decorators fall into two groups: annotations and interception recipes. The annotating decorators (appender, remover, iterator, converter, internally_instrumented) indicate the method's purpose and take no arguments. They are not written with parens:: @collection.appender def append(self, append): ... The recipe decorators all require parens, even those that take no arguments:: @collection.adds('entity') def insert(self, position, entity): ... @collection.removes_return() def popitem(self): ... cCs d|_|S)aTag the method as the collection appender. The appender method is called with one positional argument: the value to append. The method will be automatically decorated with 'adds(1)' if not already decorated:: @collection.appender def add(self, append): ... # or, equivalently @collection.appender @collection.adds(1) def add(self, append): ... # for mapping type, an 'append' may kick out a previous value # that occupies that slot. consider d['a'] = 'foo'- any previous # value in d['a'] is discarded. @collection.appender @collection.replaces(1) def add(self, entity): key = some_key_func(entity) previous = None if key in self: previous = self[key] self[key] = entity return previous If the value to append is not allowed in the collection, you may raise an exception. Something to remember is that the appender will be called for each object mapped by a database query. If the database contains rows that violate your collection semantics, you will need to get creative to fix the problem, as access via the collection will not work. If the appender method is internally instrumented, you must also receive the keyword argument '_sa_initiator' and ensure its promulgation to collection events. appender_sa_instrument_rolefnr8r8r:rGs)zcollection.appendercCs d|_|S)aTag the method as the collection remover. The remover method is called with one positional argument: the value to remove. The method will be automatically decorated with :meth:`removes_return` if not already decorated:: @collection.remover def zap(self, entity): ... # or, equivalently @collection.remover @collection.removes_return() def zap(self, ): ... If the value to remove is not present in the collection, you may raise an exception or return None to ignore the error. If the remove method is internally instrumented, you must also receive the keyword argument '_sa_initiator' and ensure its promulgation to collection events. removerrHrJr8r8r:rLszcollection.removercCs d|_|S)zTag the method as the collection remover. The iterator method is called with no arguments. It is expected to return an iterator over all collection members:: @collection.iterator def __iter__(self): ... iteratorrHrJr8r8r:rMs zcollection.iteratorcCs d|_|S)aTag the method as instrumented. This tag will prevent any decoration from being applied to the method. Use this if you are orchestrating your own calls to :func:`.collection_adapter` in one of the basic SQLAlchemy interface methods, or to prevent an automatic ABC method decoration from wrapping your implementation:: # normally an 'extend' method on a list-like class would be # automatically intercepted and re-implemented in terms of # SQLAlchemy events and append(). your implementation will # never be called, unless: @collection.internally_instrumented def extend(self, items): ... T)_sa_instrumentedrJr8r8r:internally_instrumented&sz"collection.internally_instrumentedz1.3zThe :meth:`.collection.converter` handler is deprecated and will be removed in a future release. Please refer to the :class:`.AttributeEvents.bulk_replace` listener interface in conjunction with the :func:`.event.listen` function.cCs d|_|S)aTag the method as the collection converter. This optional method will be called when a collection is being replaced entirely, as in:: myobj.acollection = [newvalue1, newvalue2] The converter method will receive the object being assigned and should return an iterable of values suitable for use by the ``appender`` method. A converter must not assign values or mutate the collection, its sole job is to adapt the value the user provides into an iterable of values for the ORM's use. The default converter implementation will use duck-typing to do the conversion. A dict-like collection will be convert into an iterable of dictionary values, and other types will simply be iterated:: @collection.converter def convert(self, other): ... If the duck-typing of the object does not match the type of this collection, a TypeError is raised. Supply an implementation of this method if you want to expand the range of possible types that can be assigned in bulk or perform validation on the values about to be assigned. converterrHrJr8r8r:rP;s%zcollection.convertercsfdd}|S)aMark the method as adding an entity to the collection. Adds "add to collection" handling to the method. The decorator argument indicates which method argument holds the SQLAlchemy-relevant value. Arguments can be specified positionally (i.e. integer) or by name:: @collection.adds(1) def push(self, item): ... @collection.adds('entity') def do_stuff(self, thing, entity=None): ... csdf|_|S)Nfire_append_event_sa_instrument_beforerJargr8r: decoratorts z"collection.adds..decoratorr8rUrVr8rTr:addscs zcollection.addscsfdd}|S)aMark the method as replacing an entity in the collection. Adds "add to collection" and "remove from collection" handling to the method. The decorator argument indicates which method argument holds the SQLAlchemy-relevant value to be added, and return value, if any will be considered the value to remove. Arguments can be specified positionally (i.e. integer) or by name:: @collection.replaces(2) def __setitem__(self, index, item): ... csdf|_d|_|S)NrQfire_remove_event)rS_sa_instrument_afterrJrTr8r:rVs z&collection.replaces..decoratorr8rWr8rTr:replaceszs zcollection.replacescsfdd}|S)aMark the method as removing an entity in the collection. Adds "remove from collection" handling to the method. The decorator argument indicates which method argument holds the SQLAlchemy-relevant value to be removed. Arguments can be specified positionally (i.e. integer) or by name:: @collection.removes(1) def zap(self, item): ... For methods where the value to remove is not known at call-time, use collection.removes_return. csdf|_|SNrYrRrJrTr8r:rVs z%collection.removes..decoratorr8rWr8rTr:removess zcollection.removescCs dd}|S)aMark the method as removing an entity in the collection. Adds "remove from collection" handling to the method. The return value of the method, if any, is considered the value to remove. The method arguments are not inspected:: @collection.removes_return() def pop(self): ... For methods where the value to remove is known at call-time, use collection.remove. cSs d|_|Sr\)rZrJr8r8r:rVsz,collection.removes_return..decoratorr8)rVr8r8r:removes_returnszcollection.removes_returnN)r=r>r?__doc__ staticmethodrGrLrMrOr deprecatedrPrXr[r]r^r8r8r8r:r&s. +       r@r5cCsdS)z7Fetch the :class:`.CollectionAdapter` for a collection.Nr8)r&r8r8r:r'srAc@seZdZUdZdZded<ded<ded<d ed <d ed <d ed<d ed<dd ddddZddddZeddddZ ed dddZ ddZ dSdd dd!d"d#Z d$d%Z ddd&d'Zd(dd)d*Zddd+d,d-Zd.dd/d0d1Zd2d3ZdTdd dd!d4d5Zddd+d6d7ZdUd dd8d9d:Zddd;d<Zd=d>Zd?d@ZdAdBZdefdCdDZdefdEdFZdefdGdHZdefdIdJZdefdKdLZdefdMdNZ dOdPZ!dQdRZ"dS)Vr@aiBridges between the ORM and arbitrary Python collections. Proxies base-level collection operations (append, remove, iterate) to the underlying Python collection, and emits add/remove events for entities entering or leaving the collection. The ORM uses :class:`.CollectionAdapter` exclusively for interaction with entity collections. )attr_key_data owner_state _converter invalidatedemptyrrbstrrcz)Callable[..., _AdaptedCollectionProtocol]rdzInstanceState[Any]rer4rfboolrgrhr,)rbredatacCs>||_|j|_t||_||_||_|j|_ d|_ d|_ dSNF) rbkeyrcweakrefrefrdrerArErfrgrh)r9rbrerkr8r8r:__init__s zCollectionAdapter.__init__Noner6cCstddS)Nz%This collection has been invalidated.)rwarnr9r8r8r:_warn_invalidatedsz#CollectionAdapter._warn_invalidatedcCs|S)z$The entity collection being adapted.)rdrtr8r8r:rkszCollectionAdapter.datacCs|jj|j|kS)zreturn True if the owner state still refers to this collection. This will return False within a bulk replace operation, where this collection is the one being replaced. )redictrcrdrtr8r8r:_referenced_by_ownersz&CollectionAdapter._referenced_by_ownercCs |jSr7rdrBrtr8r8r: bulk_appenderszCollectionAdapter.bulk_appenderNrzOptional[AttributeEventToken])item initiatorr6cCs|j||ddS)z8Add an entity to the collection, firing mutation events. _sa_initiatorNrxr9rzr{r8r8r:append_with_eventsz#CollectionAdapter.append_with_eventcCs&|jrtdd|_||jj|j<dS)Nz7This collection adapter is already in the 'empty' stateT)rhAssertionErrorre_empty_collectionsrc)r9 user_datar8r8r: _set_emptys zCollectionAdapter._set_emptycCs2|jstdd|_|jj|j|jj|j<dS)Nz3This collection adapter is not in the 'empty' stateF)rhrrerpoprcrvrtr8r8r: _reset_empty#szCollectionAdapter._reset_emptyr cCstddS)NzZThis is a special 'empty' collection which cannot accommodate internal mutation operations)sa_excZInvalidRequestErrorrtr8r8r: _refuse_empty,szCollectionAdapter._refuse_empty)rzr6cCs$|jr||j|dddSz=Add or restore an entity to the collection, firing no events.Fr|NrhrrdrBr9rzr8r8r:append_without_event2sz&CollectionAdapter.append_without_eventz Iterable[Any])itemsr6cCs2|jr||j}|D]}||ddqdSrr)r9rrGrzr8r8r:append_multiple_without_event9s  z/CollectionAdapter.append_multiple_without_eventcCs |jSr7rdrCrtr8r8r: bulk_removerAszCollectionAdapter.bulk_removercCs|j||ddS)z=Remove an entity from the collection, firing mutation events.r|Nrr~r8r8r:remove_with_eventDsz#CollectionAdapter.remove_with_eventcCs$|jr||j|dddS)z7Remove an entity from the collection, firing no events.Fr|N)rhrrdrCrr8r8r:remove_without_eventJsz&CollectionAdapter.remove_without_event)r{r6cCs6|jr||j}t|D]}|||dq dS)z>Empty the collection, firing a mutation event for each entity.r|NrhrrdrClist)r9r{rLrzr8r8r:clear_with_eventPs   z"CollectionAdapter.clear_with_eventcCs6|jr||j}t|D]}||ddq dS)z'Empty the collection, firing no events.Fr|Nr)r9rLrzr8r8r:clear_without_event[s   z%CollectionAdapter.clear_without_eventcCst|S)z(Iterate over entities in the collection.)iterrdrDrtr8r8r:__iter__dszCollectionAdapter.__iter__cCstt|S)z!Count entities in the collection.)lenrrdrDrtr8r8r:__len__iszCollectionAdapter.__len__cCsdSNTr8rtr8r8r:__bool__mszCollectionAdapter.__bool__cCsT|sdS|dk rP|jr||jr,||D]}|j|j|jj|||q0dSrlrgrurhrrbfire_append_wo_mutation_eventrervr9rr{rmrzr8r8r:#_fire_append_wo_mutation_event_bulkpsz5CollectionAdapter._fire_append_wo_mutation_event_bulkcCsF|dk r>|jr||jr$||j|j|jj|||S|SdS)abNotify that a entity is entering the collection but is already present. Initiator is a token owned by the InstrumentedAttribute that initiated the membership mutation, and should be left as None unless you are passing along an initiator value from a chained operation. .. versionadded:: 1.4.15 FNrr9rzr{rmr8r8r:rs z/CollectionAdapter.fire_append_wo_mutation_eventcCsF|dk r>|jr||jr$||j|j|jj|||S|SdS)a Notify that a entity has entered the collection. Initiator is a token owned by the InstrumentedAttribute that initiated the membership mutation, and should be left as None unless you are passing along an initiator value from a chained operation. FN)rgrurhrrbrQrervrr8r8r:rQs z#CollectionAdapter.fire_append_eventcCsT|sdS|dk rP|jr||jr,||D]}|j|j|jj|||q0dSrlrgrurhrrbrYrervrr8r8r:_fire_remove_event_bulksz)CollectionAdapter._fire_remove_event_bulkcCsB|dk r>|jr||jr$||j|j|jj|||dS)aNotify that a entity has been removed from the collection. Initiator is the InstrumentedAttribute that initiated the membership mutation, and should be left as None unless you are passing along an initiator value from a chained operation. FNrrr8r8r:rYsz#CollectionAdapter.fire_remove_eventcCs,|jr||jj|j|jj||ddS)zNotify that an entity is about to be removed from the collection. Only called if the entity cannot be removed after calling fire_remove_event(). )r{rmN)rgrurbfire_pre_remove_eventrerv)r9r{rmr8r8r:rsz'CollectionAdapter.fire_pre_remove_eventcCs |j|j|jj|j|j|jdS)N)rmre owner_clsrkrgrh)rcreclass_rkrgrhrtr8r8r: __getstate__szCollectionAdapter.__getstate__cCsj|d|_|d|_t|d|_|dj|_||d_|d|_t |d|jj |_ | dd|_ dS)NrmrerkrgrrhF)rcrernrordrErfrArggetattrimplrbgetrh)r9dr8r8r: __setstate__s     zCollectionAdapter.__setstate__)N)N)N)#r=r>r?r_ __slots__rFrprupropertyrkrwryrrrrrrrrrrrrrrrrrrQrrYrrrr8r8r8r:r@sV         c Cst|tsttj}||pd}||p*d}||p6d|}||}|} |pXdD].} | |krt| | |dqZ| |krZ| | ddqZ|r|j||d|j ||ddS)aFLoad a new collection, firing events based on prior like membership. Appends instances in ``values`` onto the ``new_adapter``. Events will be fired for any instance not present in the ``existing_adapter``. Any instances in ``existing_adapter`` not present in ``values`` will have remove events fired upon them. :param values: An iterable of collection member instances :param existing_adapter: A :class:`.CollectionAdapter` of instances to be replaced :param new_adapter: An empty :class:`.CollectionAdapter` to load with ``values`` r8r|F)r{N) isinstancerrrZ IdentitySet intersection differenceryrr) valuesZexisting_adapterZ new_adapterr{ZidsetZexisting_idset constants additionsZremovalsrGmemberr8r8r: bulk_replaces$   rz4Union[Type[Collection[Any]], _CollectionFactoryType]_CollectionFactoryType)factoryr6cCsx|tkrt|}n tt|}t|}|tkr@t|}t|}trtz t|ddt|krft |W5tX|S)aoPrepare a callable for future use as a collection class factory. Given a collection class factory (either a type or no-arg callable), return another factory that will produce compatible instances when called. This function is responsible for converting collection_class=list into the run-time behavior of collection_class=InstrumentedList. rNN) __canned_instrumentationrrtype__instrumentation_mutexacquirereleaserid_instrument_class)rZ impl_factoryclsr8r8r:prepare_instrumentation/s      rcCsH|jdkrtdt|\}}t|||t|||t|||dS)z6Modify methods in a class and install instrumentation. __builtin__zGCan not instrument a built-in type. Use a subclass, even a trivial one.N)r>r ArgumentError_locate_roles_and_methods_setup_canned_roles_assert_required_roles_set_collection_attributesrrolesmethodsr8r8r:r_s    rc Csi}i}|jD]}t|D]\}}t|s0qt|drX|j}|dksLt|||d}d}t|dr|j\} } | dkst| | f}t|dr|j } | dkst| }|r||f||<q|rdd|f||<qq||fS)zgsearch for _sa_instrument_role-decorated methods in method resolution order, assign to roles. rI)rGrLrMrPNrS)rQrYrZ) __mro__varsrcallablehasattrrIr setdefaultrSrZ) rrrZsuperclsnamemethodrolebeforeafteropargumentr8r8r:rts2         rc Cst|}|tkr|dk stt|\}}|D]\}}|||q2|D]:\}} t||d} | rP||krPt| dsPt||| | qPdS)zsee if this class has "canned" roles based on a known collection type (dict, set, list). Apply those roles as needed to the "roles" dictionary, and also prepare "decorator" methods NrN) rduck_type_collection __interfacesrrrrrsetattr) rrrZcollection_typeZ canned_rolesZ decoratorsrrrrVrKr8r8r:rs    rcCsd|kst||ds(td|jn,|d|krTtt||ddsTd||d<d|ksjt||ds|td|jn,|d|krtt||ddsd||d<d|kst||dstd |jd S) zTensure all roles are present, and apply implicit instrumentation if needed rGz>Type %s must elect an appender method to be a collection classrN)rQrNrLzType %s must elect an iterator method to be a collection classN)rrrr=rrr8r8r:rs8    rc Cs|D]*\}\}}}t||tt|||||q|D]\}}t|d|t||qdn|krV|nd~fdd}d|_t drj |_ j |_ j |_ |S)zIRoute method args and/or return value through the collection adapter.rNcsrbdkr,|kr"td|}n6t|krB|}n |krT|}ntd|dd}|dkr|d}n |dj}r|rt|||r|s||S||}|dk rt||||SdS)NzMissing argument %sr}Fr)rrrrrAr)argskwvaluer{executorresrrrrZ named_argZpos_argr8r:wrappers4        z/_instrument_membership_mutator..wrapperTrI) rrZflatten_iteratorrrintrindexrNrrIr=r_)rrrrZfn_argsrr8rr:rs&  # rcCs&|dk r"|j}|r"|j||dddS)zERun set wo mutation events. The collection is not mutated. FNrm)rAr)r&rzr}rr8r8r:__set_wo_mutation0srcCs&|dk r"|j}|r"|j|||d}|S)z^Run set events. This event always occurs before the collection is actually mutated. Fr)rArQr&rzr}rmrr8r8r:__set>s rcCs&|dk r"|j}|r"|j|||ddS)aRun del events. This event occurs before the collection is actually mutated, *except* in the case of a pop operation, in which case it occurs afterwards. For pop operations, the __before_pop hook is called before the operation occurs. FrN)rArYrr8r8r:__delLs rcCs|j}|r||dS)z;An event which occurs on a before a pop() operation occurs.N)rAr)r&r}rr8r8r: __before_pop[srzDict[str, Callable[[_FN], _FN]]rrc sddfdd}fdd}fdd}fd d }fd d }fd d}fdd}fdd}fdd}t} | d| S)z:Tailored instrumentation wrappers for any list-like class.cSsd|_tt|jj|_dSr)rNrrr=r_rJr8r8r:_tidyesz_list_decorators.._tidycsdfdd }||S)Ncst|||t}||dSr7)rr)r9rzr}rJr8r:appendjsz0_list_decorators..append..append)Nr8)rKrrrJr:risz _list_decorators..appendcsdfdd }||S)Ncst|||t||dSr7rrr9rr}rJr8r:removersz0_list_decorators..remove..remove)Nr8rKrrrJr:rqsz _list_decorators..removecsfdd}||S)Ncst||d|}|||dSr7)r)r9rrrJr8r:insert{sz0_list_decorators..insert..insertr8)rKrrrJr:rzs z _list_decorators..insertcsfdd}||S)Nc sXt|tsF||}|dk r(t||d|t||d|}|||n|jpNd}|jpXd}|dkrn|t|7}|jdk r|j}nt|}|dkr|t|7}|dkr||krdSt|||D]}t||kr||=qt |D]\}}| |||qn\t t|||} t|t| kr2t dt|t| ft | |D]\}}|||q.__setitem__..__setitem__r8rKrrrJr:rs (z%_list_decorators..__setitem__csfdd}||S)NcsVt|ts,||}t||d|||n&||D]}t||d|q4||dSr7)rrrr9rrzrJr8r: __delitem__s   z:_list_decorators..__delitem__..__delitem__r8rKrrrJr:rs z%_list_decorators..__delitem__csdd}||S)NcSst|D]}||qdSr7rrr9iterablerr8r8r:extends z0_list_decorators..extend..extendr8)rKrrr8r:rsz _list_decorators..extendcsdd}||S)NcSst|D]}||q|Sr7rrr8r8r:__iadd__s  z4_list_decorators..__iadd__..__iadd__r8)rKrrr8r:rsz"_list_decorators..__iadd__csdfdd }||S)Ncs$t|||}t||d||Sr7rrrrJr8r:rs z*_list_decorators..pop..pop)rr8rKrrrJr:rsz_list_decorators..popcsdfdd }||S)Nrcs$|D]}t||d|q|dSr7rrrJr8r:clearsz._list_decorators..clear..clear)rr8rKr rrJr:r sz_list_decorators..clearrlocalscopyr) rrrrrrrrr lr8rr:_list_decoratorsbs   ,     rcstddfdd}fdd}fdd}fd d }fd d }fd d}fdd}t}|d|S)zBTailored instrumentation wrappers for any dict-like mapping class.cSsd|_tt|jj|_dSr)rNrrvr=r_rJr8r8r:rsz_dict_decorators.._tidycsdfdd }||S)Ncs8||krt|||||t||||}|||dSr7)rr)r9rmrr}rJr8r:rsz:_dict_decorators..__setitem__..__setitem__)Nr8rrrJr:rsz%_dict_decorators..__setitem__csdfdd }||S)Ncs(||krt|||||||dSr7r )r9rmr}rJr8r:rsz:_dict_decorators..__delitem__..__delitem__)Nr8rrrJr:rsz%_dict_decorators..__delitem__csfdd}||S)Ncs(|D]}t|||d|q|dSr7r )r9rmrJr8r:r  sz._dict_decorators..clear..clearr8r rrJr:r  s z_dict_decorators..clearcstffdd }||S)NcsFt|||k}|tkr$||}n |||}|rBt||d||Sr7)rrr)r9rmdefaultZ_to_delrzrJr8r:rs  z*_dict_decorators..pop..poprr rrJr:rs z_dict_decorators..popcsfdd}||S)Ncs&t||}t||ddd|S)NrrrrJr8r:popitem"sz2_dict_decorators..popitem..popitemr8)rKrrrJr:r!s z!_dict_decorators..popitemcsddd}||S)NcSs>||kr||||S||}||kr6t||d|SdSr7)r __getitem__r)r9rmrrr8r8r:r,s   z8_dict_decorators..setdefault..setdefault)Nr8)rKrrr8r:r+s z$_dict_decorators..setdefaultcstfdd}||S)Nc[s|tk rt|drXt|D]:}||ks6||||k rD||||<qt|||dqn8|D]2\}}||ksx|||k r|||<q\t||dq\|D]:}||ks||||k r||||<qt|||dqdS)Nkeys)rrrr)r9Z__otherrrmrr8r8r:update;s    z0_dict_decorators..update..updaterrKrrr8r:r:s z _dict_decorators..updaterr )rrr rrrrrr8rr:_dict_decoratorss     rrrj)r9objr6cCst|t|jfS)zKAllow only set, frozenset and self.__class__-derived objects in binops.)r_set_binop_bases __class__r9rr8r8r:_set_binops_check_strictZsrcCs t|t|jfpt|tkS)z5Allow anything set-like to participate in set binops.)rrrrrsetrr8r8r:_set_binops_check_loose`s rcsddfdd}fdd}fdd}fd d }fd d }fd d}fdd}fdd}fdd}fdd} fdd} fdd} fdd} t} | d| S)z9Tailored instrumentation wrappers for any set-like class.cSsd|_tt|jj|_dSr)rNrrr=r_rJr8r8r:rksz_set_decorators.._tidycsdfdd }||S)Ncs2||krt|||t}n t|||||dSr7)rrrrrJr8r:addps z)_set_decorators..add..add)Nr8)rKr rrJr:r osz_set_decorators..addcsdfdd }||S)Ncs$||krt|||t||dSr7rrrJr8r:discard|sz1_set_decorators..discard..discard)Nr8)rKr!rrJr:r!{sz _set_decorators..discardcsdfdd }||S)Ncs$||krt|||t||dSr7rrrJr8r:rsz/_set_decorators..remove..remove)Nr8rrrJr:rsz_set_decorators..removecsfdd}||S)Ncs"t||}t||dt|Sr7)rrrrrJr8r:rsz)_set_decorators..pop..popr8r rrJr:rs z_set_decorators..popcsdd}||S)NcSst|D]}||qdSr7)rrrr8r8r:r s z-_set_decorators..clear..clearr8r rr8r:r sz_set_decorators..clearcsdd}||S)NcSs|D]}||qdSr7)r r9rrzr8r8r:rsz/_set_decorators..update..updater8rrr8r:rsz_set_decorators..updatecsdd}||S)NcSs&t||stS|D]}||q|Sr7)rNotImplementedr r"r8r8r:__ior__s   z1_set_decorators..__ior__..__ior__r8)rKr$rr8r:r$sz _set_decorators..__ior__csdd}||S)NcSs|D]}||qdSr7)r!r"r8r8r:difference_updateszE_set_decorators..difference_update..difference_updater8)rKr%rr8r:r%sz*_set_decorators..difference_updatecsdd}||S)NcSs&t||stS|D]}||q|Sr7)rr#r!r"r8r8r:__isub__s   z3_set_decorators..__isub__..__isub__r8)rKr&rr8r:r&sz!_set_decorators..__isub__csdd}||S)NcSsR||t|}}||||}}|D]}||q*|D]}||q>dSr7)rrrr r9otherZwantZhaverr rzr8r8r:intersection_updates  zI_set_decorators..intersection_update..intersection_updater8)rKr)rr8r:r)s z,_set_decorators..intersection_updatecsdd}||S)NcSs`t||stS||t|}}||||}}|D]}||q8|D]}||qL|Sr7)rr#rrrr r'r8r8r:__iand__s   z3_set_decorators..__iand__..__iand__r8)rKr*rr8r:r*s z!_set_decorators..__iand__csdd}||S)NcSsR||t|}}||||}}|D]}||q*|D]}||q>dSr7)symmetric_differencerrr r'r8r8r:symmetric_difference_updates  zY_set_decorators..symmetric_difference_update..symmetric_difference_updater8)rKr,rr8r:r,s z4_set_decorators..symmetric_difference_updatecsdd}||S)NcSs`t||stS||t|}}||||}}|D]}||q8|D]}||qL|Sr7)rr#r+rrr r'r8r8r:__ixor__s   z3_set_decorators..__ixor__..__ixor__r8)rKr-rr8r:r-s z!_set_decorators..__ixor__rr )r r!rrr rr$r%r&r)r*r,r-rr8rr:_set_decoratorshs"       r.c@seZdZdZdS)InstrumentedListz-An instrumented version of the built-in list.Nr=r>r?r_r8r8r8r:r/ sr/c@seZdZdZdS)InstrumentedSetz,An instrumented version of the built-in set.Nr0r8r8r8r:r1sr1c@seZdZdZdS)InstrumentedDictz-An instrumented version of the built-in dict.Nr0r8r8r8r:r2sr2z/util.immutabledict[Any, _CollectionFactoryType]rrrr)rGrLrMr rMrzMutil.immutabledict[Any, Tuple[Dict[str, str], Dict[str, Callable[..., Any]]]]rcCs|ddlmaddlmaddlmaddlmaddlmaddlmaddlmadd lmatt tt ttdS) Nrr!rrr#)r()r))r*)r+) r(r"r rr$r)r*r+rr/r1)Zlclsr8r8r:__go:s        r3)N)N)N)Zr_ __future__roperator threadingtypingrrrrrrr r r r r rrrrrnbaserrrrZsql.baserZ util.compatrZ util.typingr attributesrrr(rr r"r$stater%__all__Lockrrr-r/r0r1r2r4r,r&r' attrgetterr@rrrrrrrrrrrrrrr frozensetrrrr.r/r1r2Z immutabledictrrvrrFrr3rr8r8r8r: sa                                     ? *0- >  h#