L i9 rUdZddlmZddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z dd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlZddlmZddlmZddlmZddlmZddlmZddl m!Z!ej$r*ddl"m#Z#ddl"m$Z$ddl%m&a&ddl%m'a'ddl%m(a(dd l%m)a)dd!l*m+Z+gd"Z,ejZZ.egd#fZ/ed$e%Z0ed&e%Z1ed'e%Z2ed(d)%Z3ed*d+%Z4Gd,d-e!Z5Gd.d#e!Z6Gd/d0Z7erdXd2Z8nejrd3Z8Gd4d1Z:dYd5Z; dZd6Zd9Z?d:Z@d;ZAd<ZBdYd=ZCd>ZDd?ZEdYd@ZFd[dAZGd[dBZHeIeJfZKd\dCZLd\dDZMd[dEZNGdFdGe e0ZOGdHdIee0ZPGdJdKe e1e2fZQejeSeOeIePeTeQiZUdLeVdM<ejeSdNdOdPdQeGfeIdRdOdPdQeNfeTdSdTieHfiZWdUeVdV<dWZXeXeYy)]aLSupport 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_COLCollection[Any]_FNCallable[..., Any]ceZdZddZy)_CollectionConverterProtocolcyN)selfr's `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sqlalchemy/orm/collections.py__call__z%_CollectionConverterProtocol.__call__N)r'r2returnr2)__name__ __module__ __qualname__r=r:r?r<r7r7s5r?r7c@eZdZUded<ded<ded<ded<ded <y ) r-CollectionAdapter _sa_adapterr5 _sa_appender _sa_removerzCallable[..., Iterable[Any]] _sa_iteratorr7 _sa_converterN)rArBrC__annotations__r:r?r<r-r-s ""$$##..//r?ceZdZdZedZedZedZedZee jdddZ edd Z ed Z ed Zed Zy )r'avDecorators 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): ... cd|_|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_rolefns r<rNzcollection.appendersV", r?cd|_|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. removerrOrQs r<rTzcollection.removers2"+ r?cd|_|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): ... iteratorrOrQs r<rVzcollection.iterators", r?cd|_|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_instrumentedrQs r<internally_instrumentedz"collection.internally_instrumented,s$# r?z1.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.cd|_|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. converterrOrQs r<r[zcollection.converterAsJ"- r?cfd}|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): ... cdf|_|S)Nfire_append_event_sa_instrument_beforerRargs r< decoratorz"collection.adds..decorator{(;S'AB $Ir?r:rbrcs` r<addszcollection.addsis$ r?cfd}|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): ... c(df|_d|_|S)Nr^fire_remove_event)r`_sa_instrument_afterras r<rcz&collection.replaces..decorators(;S'AB $&9B #Ir?r:res` r<replaceszcollection.replacess   r?cfd}|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. cdf|_|SNrir_ras r<rcz%collection.removes..decoratorrdr?r:res` r<removeszcollection.removess" r?c d}|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. cd|_|Srn)rjrQs r<rcz,collection.removes_return..decorators&9B #Ir?r:)rcs r<removes_returnzcollection.removes_returns  r?N)rbintr@zCallable[[_FN], _FN])rArBrC__doc__ staticmethodrNrTrVrYr deprecatedr[rfrkrorrr:r?r<r'r's4++Z6  (T__  ?@.,,r?r'rEcy)z7Fetch the :class:`.CollectionAdapter` for a collection.Nr:)r's r<r(r(r>r?rFceZdZUdZdZded<ded<ded<d ed <d ed <d ed<d ed< d,dZd-dZed.dZ ed/dZ dZ d0 d1dZ dZ d-dZd2dZd3dZd4dZdZ d0 d1dZd3dZ d0 d5dZd-d Zd!Zd"Zd#Zdefd$Zdefd%Zdefd&Zdefd'Zdefd(Zdefd)Z d*Z!d+Z"y)6rEaiBridges 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 invalidatedemptyrrystrrzz)Callable[..., _AdaptedCollectionProtocol]r{InstanceState[Any]r|r7r}boolr~rc||_|j|_tj||_||_||_|j|_ d|_ d|_ yNF) rykeyrzweakrefrefr{r|rFrJr}r~r)r;ryr|datas r<__init__zCollectionAdapter.__init__sT  HH [[& &,,  r?c.tjdy)Nz%This collection has been invalidated.)rwarnr;s r<_warn_invalidatedz#CollectionAdapter._warn_invalidateds 9:r?c"|jS)z$The entity collection being adapted.)r{rs r<rzCollectionAdapter.data szz|r?ch|jj|j|juS)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. )r|dictrzr{rs r<_referenced_by_ownerz&CollectionAdapter._referenced_by_owners*$$TYY/4::<??r?c6|jjSr9r{rGrs r< bulk_appenderzCollectionAdapter.bulk_appenderszz|(((r?NcF|jj||y)z8Add an entity to the collection, firing mutation events. _sa_initiatorNrr;item initiators r<append_with_eventz#CollectionAdapter.append_with_events !!$i!@r?c~|jrJdd|_||jj|j<y)Nz7This collection adapter is already in the 'empty' stateT)rr|_empty_collectionsrz)r; user_datas r< _set_emptyzCollectionAdapter._set_empty#s<  E D E  9B++DII6r?c|jsJdd|_|jjj|j|jj |j<y)Nz3This collection adapter is not in the 'empty' stateF)rr|rpoprzrrs r< _reset_emptyzCollectionAdapter._reset_empty*sZ JJ A @ A     / / 3 3DII > dii(r?c,tjd)NzZThis is a special 'empty' collection which cannot accommodate internal mutation operations)sa_excInvalidRequestErrorrs r< _refuse_emptyzCollectionAdapter._refuse_empty3s(( +  r?c~|jr|j|jj|dyz=Add or restore an entity to the collection, firing no events.FrNrrr{rGr;rs r<append_without_eventz&CollectionAdapter.append_without_event9s0 ::     !!$e!Empty the collection, firing a mutation event for each entity.rNrrr{rHlist)r;rrTrs r<clear_with_eventz"CollectionAdapter.clear_with_eventWsE ::    **,**J 3D D 2 3r?c|jr|j|jj}t |D] }||dy)z'Empty the collection, firing no events.FrNr)r;rTrs r<clear_without_eventz%CollectionAdapter.clear_without_eventbsE ::    **,**J /D D . /r?cPt|jjS)z(Iterate over entities in the collection.)iterr{rIrs r<__iter__zCollectionAdapter.__iter__ksDJJL--/00r?cbtt|jjS)z!Count entities in the collection.)lenrr{rIrs r<__len__zCollectionAdapter.__len__ps!4 113455r?cyNTr:rs r<__bool__zCollectionAdapter.__bool__tsr?c |sy|dur}|jr|j|jr|j|D]?}|jj |j |j j|||Ayyrr~rrrryfire_append_wo_mutation_eventr|rr;rrrrs r<#_fire_append_wo_mutation_event_bulkz5CollectionAdapter._fire_append_wo_mutation_event_bulkws  E !&&(zz!!#  77$$$$))   "r?c|duru|jr|j|jr|j|jj |j |j j|||S|S)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 Frr;rrrs r<rz/CollectionAdapter.fire_append_wo_mutation_eventso E !&&(zz!!#99::  $"2"2"7"7y# Kr?c|duru|jr|j|jr|j|jj |j |j j|||S|S)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. F)r~rrrryr^r|rrs r<r^z#CollectionAdapter.fire_append_eventso E !&&(zz!!#99..  $"2"2"7"7y# Kr?c |sy|dur}|jr|j|jr|j|D]?}|jj |j |j j|||Ayyrr~rrrryrir|rrs r<_fire_remove_event_bulkz)CollectionAdapter._fire_remove_event_bulks  E !&&(zz!!#  ++$$$$))   "r?c|durv|jr|j|jr|j|jj |j |j j|||yy)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. FNrrs r<riz#CollectionAdapter.fire_remove_eventsh E !&&(zz!!# II ' '  $"2"2"7"7y#  "r?c|jr|j|jj|j|jj ||y)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(). )rrN)r~rryfire_pre_remove_eventr|r)r;rrs r<rz'CollectionAdapter.fire_pre_remove_eventsN     " " $ ''       ! ! ( r?c|j|j|jj|j|j|j dS)N)rr| owner_clsrr~r)rzr|class_rr~rrs r< __getstate__zCollectionAdapter.__getstate__sB99++))00II++ZZ   r?c4|d|_|d|_tj|d|_|dj |_||d_|d|_t|d|jj|_ |jdd|_ y)Nrr|rr~rrF)rzr|rrr{rJr}rFr~getattrimplrygetr)r;ds r< __setstate__zCollectionAdapter.__setstate__seH ]+[[6+ F)11 $& ]+AkNDII6;; UU7E* r?)ryrr|rrr-)r@None)r@r-)r@rr9)rrrOptional[AttributeEventToken]r@r)r@r )rrr@r)rz Iterable[Any]r@r)rrr@r)#rArBrCrt __slots__rKrrpropertyrrrrrrrrrrrrrrrrrrrrr^rrirrrr:r?r<rErEs I "! I 54##,, K%() ,;@@)EIAA$AA AC  =0(EI@@$A@ @ <:> 36 3  3/1 6 $,=Af415&,8<(15&&/3   +r?ct|tsJtj}||xsd}|j |xsd}||xsdj |}|j |}|j } |xsdD] } | |vr | | || |vs| | d"|r'|j|||j||yy)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`` r:rF)rN) isinstancerr IdentitySet intersection differencerrr) valuesexisting_adapter new_adapterridsetexisting_idset constants additionsremovalsrNmembers r< bulk_replacer s& fd ## #   E+1r2N++FLb9Ifl#..y9I((3H((*H,B2 Y  V9 5 y V5 1 2 <<  =  00Y0O r?c||tvr t|}ntt|}t|}|tvrt|}t|}tj r; t |ddt|k7r t|tj|S|S#tjwxYw)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. rXN) __canned_instrumentationr_CollectionFactoryTypetype__instrumentation_mutexacquirerid_instrument_classrelease)factory impl_factoryclss r<prepare_instrumentationr6s"**/8 2G< |~ C && 04 <>"&&( .s.5C@!#& # + + - < $ + + -s )$B%%B;c|jdk(rtjdt|\}}t |||t |||t |||y)z6Modify methods in a class and install instrumentation. __builtin__zGCan not instrument a built-in type. Use a subclass, even a trivial one.N)rBr ArgumentError_locate_roles_and_methods_setup_canned_roles_assert_required_roles_set_collection_attributesrrolesmethodss r<rrfs\  ~~&"" ,  /s3NE7UG,3w/sE73r?ci}i}|jD]}t|jD]\}}t|st |dr$|j }|dvsJ|j ||d}d}t |dr|j\} } | dvsJ| | f}t |dr|j} | dvsJ| }|r ||fz||<|sdd|f||<||fS)zgsearch for _sa_instrument_role-decorated methods in method resolution order, assign to roles. rP)rNrTrVr[Nr`)r^rirj) __mro__varsrcallablehasattrrP setdefaultr`rj) rrrsuperclsnamemethodrolebeforeafteroparguments r<rr{s( EMOGKK 2 N002 2LD&F#v4511    t,15F#'Ev67%;; HGGGGXv5600GGGG &% 1  $dE 1 ? 2 2B '>r?cNtj|}|tvr|Jt|\}}|jD]\}}|j |||jD]:\}} t ||d} | s||vst | dr(t||| | <yy)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 NrX)rduck_type_collection __interfacesrr rr setattr) rrrcollection_type canned_roles decoratorsrrrrcrRs r<rrs//4O,&***#/#@ j&,,. )JD$   T4 ( )",!1!1!3 4 FIfd+B')$67VYr]3 4'r?cd|vst||ds"tjd|jz|d|vr!tt ||ddsd||d<d|vst||ds"tjd|jz|d|vr!tt ||ddsd||d<d|vst||ds"tjd |jzy ) zTensure all roles are present, and apply implicit instrumentation if needed rNz>Type %s must elect an appender method to be a collection classrX)r^rNrTzType %s must elect an iterator method to be a collection classN)r rrrArrs r<rrs, gc53D&E"" !#&<< 0   z ' )'U:&');3&Dj!"WS% 2B%C"" !#&<< 0   y  (U9%&(:2%Ci !gc53D&E"" !#&<< 0  'Fr?c :|jD],\}\}}}t||tt|||||.|jD]\}}t|d|zt||!d|_t |dsd|_t||_y)zkapply ad-hoc instrumentation from decorators, class-level defaults and implicit role declarations z_sa_%sNrJ) rr_instrument_membership_mutatorrrFr rJrrX)rrr method_namerrrrs r<rrs 3:--/ . .fh   *[)68U   #[[]AkX_gc;&?@ACO 3 ( c7Cr?crqttjtd}t t rt |kDxr|xsdn|vr|jnd~fd}d|_tdrj|_ j|_ j|_ |S)zIRoute method args and/or return value through the collection adapter.rNcrZ " |vrtjdz| }n6t| kDr| }n" |vr| }ntjdz|jdd}|durd}n|dj}r|rt ||r|s |i|S |i|}|t ||||S)NzMissing argument %srFr)rrrrrFr) argskwvaluerexecutorresrrrr named_argpos_args r<wrapperz/_instrument_membership_mutator..wrapper s B& ..-89 t9w& ME"_yME ..-8FF?D1  HAw**H h %GHf %eY 7H4&2& &$%"%C(%(i8Jr?TrP) rrflatten_iteratorrrrsrindexrXr rPrArt)rrrrfn_argsr)r'r(s```` @@r<rrs  ! !"8"@"C D  h $GG x/EGH4EMI7"!--1 I !!F $Gv,-&,&@&@#GnnGO Nr?cT|dur$|j}|r|j||dyyy)zERun set wo mutation events. The collection is not mutated. FNr)rFr)r'rrr%s r<__set_wo_mutationr/7s> E!))   2 2m 3  "r?cR|dur"|j}|r|j|||}|S)z^Run set events. This event always occurs before the collection is actually mutated. Fr.)rFr^r'rrrr%s r<__setr2Es6E!)) --dMs-KD Kr?cT|dur$|j}|r|j|||yyy)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. Fr.N)rFrir1s r<__delr4Ss9E!))   & &t] & D "r?cD|j}|r|j|yy)z;An event which occurs on a before a pop() operation occurs.N)rFr)r'rr%s r< __before_popr6bs#%%H&&}5r?c d fd} fd} fd} fd} fd} fd} fd} fd } fd }tj} | jd | S) z:Tailored instrumentation wrappers for any list-like class.cdd|_tt|jj|_yr)rXrrrArtrQs r<_tidyz_list_decorators.._tidyl""T2;;/77 r?c&dfd }||S)Nc<t|||t}||yr9)r2r)r;rrrRs r<appendz0_list_decorators..append..appendqst]F;D tTNr?r9r:)rRr=r9s` r<r=z _list_decorators..appendps  f  r?c&dfd }||S)Nc<t|||t||yr9r4rr;r$rrRs r<removez0_list_decorators..remove..removeys $}f 5 tUOr?r9r:rRrBr9s` r<rBz _list_decorators..removexs  f  r?c$fd}||S)Nc6t||d|}|||yr9)r2)r;r+r$rRs r<insertz0_list_decorators..insert..inserts$tU3E tUE "r?r:)rRrFr9s` r<rFz _list_decorators..inserts # f  r?c$fd}||S)Nct|ts.||}|t||d|t||d|} |||y|jxsd}|j xsd}|dkr|t |z }|j |j}n t |}|dkr|t |z }|dk(rR||uryt|||D]}t ||kDs||=t|D]\}}|j||z|ytt|||} t |t | k7r#tdt |dt | t| |D]\}}|j||y)Nrrz#attempt to assign sequence of size z to extended slice of size )rslicer4r2stepstartrstoprange enumeraterFr ValueErrorzip __setitem__) r;r+r$existingrJrKrLirrngrRs r<rQz:_list_decorators..__setitem__..__setitem__seU+;'$$6dE474&zzQ (q19SY&E::) ::Dt9D!8CI%D19}"5$5,t9u, $U ,$-U#354 AIt45uUD$78C5zSX-( #5z3s85 $'sE?24((D12r?r:rRrQr9s` r<rQz%_list_decorators..__setitem__s& 2P kr?c$fd}||S)Nct|ts||}t||d|||y||D]}t||d|||yr9)rrIr4r;r+rrRs r< __delitem__z:_list_decorators..__delitem__..__delitem__sYeU+E{dD$.4 !K3D$dE234r?r:rRrYr9s` r<rYz%_list_decorators..__delitem__s  kr?cd}||S)NcFt|D]}|j|yr9rr=r;iterabler$s r<extendz0_list_decorators..extend..extends!h # E" #r?r:)rRr`r9s r<r`z _list_decorators..extends # f  r?cd}||S)NcHt|D]}|j||Sr9r]r^s r<__iadd__z4_list_decorators..__iadd__..__iadd__s(h # E" #Kr?r:)rRrcr9s r<rcz"_list_decorators..__iadd__  hr?c&dfd }||S)NcLt|||}t||d||Sr9r6r4rXs r<rz*_list_decorators..pop..pops)  dE?D $dE *Kr?r:rRrr9s` r<rz_list_decorators..pops  c  r?c&dfd }||S)Nc@|D]}t||d||yr9r4rXs r<clearz._list_decorators..clear..clears' /dD$. / tHr?rhr:rRrnr9s` r<rnz_list_decorators..clears  e  r?r9localscopyr) r=rBrFrQrYr`rcrrnlr9s @r<_list_decoratorsrtisU8*X"   AEE'N Hr?cdfd}fd}fd}fd}fd}fd}fd}tj}|jd |S) zBTailored instrumentation wrappers for any dict-like mapping class.cdd|_tt|jj|_yr)rXrrrArtrQs r<r9z_dict_decorators.._tidyr:r?c&dfd }||S)Nc`||vrt|||||t||||}|||yr9)r4r2)r;rr$rrRs r<rQz:_dict_decorators..__setitem__..__setitem__s8d{dDI}c:$}c:E tS% r?r9r:rUs` r<rQz%_dict_decorators..__setitem__s ! kr?c&dfd }||S)NcB||vrt|||||||yr9rm)r;rrrRs r<rYz:_dict_decorators..__delitem__..__delitem__s%d{dDI}c: tSMr?r9r:rZs` r<rYz%_dict_decorators..__delitem__s  kr?c$fd}||S)NcF|D]}t|||d||yr9rm)r;rrRs r<rnz._dict_decorators..clear..clears+ 2dDItS1 2 tHr?r:ros` r<rnz_dict_decorators..clears  e  r?c0tffd }||S)Nc~t|||v}|tur ||}n |||}|rt||d||Sr9)r6rr4)r;rdefault_to_delrrRs r<rz*_dict_decorators..pop..popsK  TkG& $}$W-dD$,Kr?rrjs` r<rz_dict_decorators..pops#)  c  r?c$fd}||S)NcPt||}t||ddd|S)Nrrgr;rrRs r<popitemz2_dict_decorators..popitem..popitem)s+  d8D $Qq )Kr?r:)rRrr9s` r<rz!_dict_decorators..popitem(s  gr?c dd}||S)Ncz||vr|j|||S|j|}||ur t||d|Sr9)rQ __getitem__r/)r;rrr$s r<r z8_dict_decorators..setdefault..setdefault3sG$  g.((-G#%dE48 r?r9r:)rRr r9s r<r z$_dict_decorators..setdefault2s  jr?c*tfd}||S)NcH|turlt|dr8t|D])}||vs ||||ur ||||<t|||d+n(|D]#\}}||vs|||ur|||<t||d%|D])}||vs ||||ur ||||<t|||d+y)Nkeys)rr rr/)r;__otherr#rr$s r<updatez0_dict_decorators..update..updateBsf$7F+#G}Hd?d3iws|.K(/ DI-dGCL$G H '.A Ud?d3iu.D(-DI-dE4@ A  ;d?d3ir#w&> "3DI%dBsGT:  ;r?rrRrr9s r<rz _dict_decorators..updateAs!' ;( f  r?r9rp) rQrYrnrrr rrsr9s @r<_dict_decoratorsrsJ8  0  AEE'N Hr?c>t|t|jfzS)zKAllow only set, frozenset and self.__class__-derived objects in binops.)r_set_binop_bases __class__r;objs r<_set_binops_check_strictras c+t~~.?? @@r?czt|t|jfzxstj|t k(S)z5Allow anything set-like to participate in set binops.)rrrrrsetrs r<_set_binops_check_loosergs8 3(DNN+<<= 1  $ $S )S 0r?cdfd}fd}fd}fd}fd}fd}fd}fd }fd }fd } fd } fd } fd} tj} | jd| S)z9Tailored instrumentation wrappers for any set-like class.cdd|_tt|jj|_yr)rXrrrArtrQs r<r9z_set_decorators.._tidyrs""S"++.66 r?c&dfd }||S)Nc`||vrt|||t}n t|||||yr9)r2rr/rAs r<addz)_set_decorators..add..addws0D dE=&A!$}= tUOr?r9r:)rRrr9s` r<rz_set_decorators..addvs  c  r?c&dfd }||S)NcD||vrt|||t||yr9r@rAs r<discardz1_set_decorators..discard..discard!}dE=&9 tUOr?r9r:)rRrr9s` r<rz _set_decorators..discards  gr?c&dfd }||S)NcD||vrt|||t||yr9r@rAs r<rBz/_set_decorators..remove..removerr?r9r:rCs` r<rBz_set_decorators..removes  f  r?c$fd}||S)NcRt||}t||dt|Sr9)r6r4rrs r<rz)_set_decorators..pop..pops)  d8D $dF +Kr?r:rjs` r<rz_set_decorators..pops  c  r?cd}||S)NcFt|D]}|j|yr9)rrBrs r<rnz-_set_decorators..clear..clears!T  " D! "r?r:ros r<rnz_set_decorators..clears " e  r?cd}||S)Nc4|D]}|j|yr9)rr;r$rs r<rz/_set_decorators..update..updates  r?r:rs r<rz_set_decorators..updates  f  r?cd}||S)NcZt||stS|D]}|j||Sr9)rNotImplementedrrs r<__ior__z1_set_decorators..__ior__..__ior__s1+D%8%%  Kr?r:)rRrr9s r<rz _set_decorators..__ior__s  gr?cd}||S)Nc4|D]}|j|yr9)rrs r<difference_updatezE_set_decorators..difference_update..difference_updates # T" #r?r:)rRrr9s r<rz*_set_decorators..difference_updates #    r?cd}||S)NcZt||stS|D]}|j||Sr9)rrrrs r<__isub__z3_set_decorators..__isub__..__isub__s2+D%8%% # T" #Kr?r:)rRrr9s r<rz!_set_decorators..__isub__rdr?cd}||S)Nc|j|t|}}||z ||z }}|D]}|j||D]}|j|yr9)rrrBrr;otherwanthaverBrrs r<intersection_updatezI_set_decorators..intersection_update..intersection_updates`**513t9$D+td{CF " D! "  r?r:)rRrr9s r<rz,_set_decorators..intersection_updates  !"""r?cd}||S)Nct||stS|j|t|}}||z ||z }}|D]}|j ||D]}|j ||Sr9)rrrrrBrrs r<__iand__z3_set_decorators..__iand__..__iand__su+D%8%%**513t9$D+td{CF " D! "  Kr?r:)rRrr9s r<rz!_set_decorators..__iand__  hr?cd}||S)Nc|j|t|}}||z ||z }}|D]}|j||D]}|j|yr9)symmetric_differencerrBrrs r<symmetric_difference_updatezY_set_decorators..symmetric_difference_update..symmetric_difference_updates`22593t9$D+td{CF " D! "  r?r:)rRrr9s r<rz4_set_decorators..symmetric_difference_updates  )***r?cd}||S)Nct||stS|j|t|}}||z ||z }}|D]}|j ||D]}|j ||Sr9)rrrrrBrrs r<__ixor__z3_set_decorators..__ixor__..__ixor__su+D%8%%22593t9$D+td{CF " D! "  Kr?r:)rRrr9s r<rz!_set_decorators..__ixor__rr?r9rp)rrrBrrnrrrrrrrrrsr9s @r<_set_decoratorsrosh7     !  # +  AEE'N Hr?ceZdZdZy)InstrumentedListz-An instrumented version of the built-in list.NrArBrCrtr:r?r<rr7r?rceZdZdZy)InstrumentedSetz,An instrumented version of the built-in set.Nrr:r?r<rrs6r?rceZdZdZy)InstrumentedDictz-An instrumented version of the built-in dict.Nrr:r?r<rrrr?rz/util.immutabledict[Any, _CollectionFactoryType]rr=rBr)rNrTrVrrVrzMutil.immutabledict[Any, Tuple[Dict[str, str], Dict[str, Callable[..., Any]]]]rcddlmaddlmaddlmaddlmaddlmaddlmaddlmadd lmatttttty) Nrr"r rr$)r))r*)r+)r,) r)r#r!rr%r*r+r,rrr)lclss r<__gorAs7 347.4;>3&'o&k"r?)r'r3r@rEr9)rz4Union[Type[Collection[Any]], _CollectionFactoryType]r@r)r@zDict[str, Callable[[_FN], _FN]])r;rrrr@r)Zrt __future__roperator threadingtypingrrrrrr r r r r rrrrrrbaserrrrsql.baser util.compatr util.typingr attributesrrr)rr!r#r%stater&__all__Lockrrr.r0r1r2r4r7r-r'r( attrgetterrErrrrrrrrr/r2r4r6rtrr frozensetrrrrrrr immutabledictrrrrKrrrqr:r?r<rsbF#  0" /3742.$ ))..*""&B"BC T e3 e3v./ e/068600JJZF-,,];z+z+z 'PT- A--`4**Z42 @#0;|   E6J Ze P#A _ D8tBx87c"g78tCH~8 D "  " I D $#&      8 L   H%'7'9:  4#4VXr?