K i/UddlmZddlmZddlZddlmZmZmZm Z ddl m Z ddl Z GddeZd ZGd d Zd ZeZd ed<dgZdZefdZGddZdZGddeZdZdZy)) annotations)warnN)ordering ambiguitiessuper_signatureAmbiguityWarning) expand_tuplesceZdZdZy)MDNotImplementedErrorz- A NotImplementedError for multiple dispatch N)__name__ __module__ __qualname____doc__g/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/multipledispatch/dispatcher.pyr r s7rr cLtt|j|ty)aC Raise warning when ambiguity is detected Parameters ---------- dispatcher : Dispatcher The dispatcher on which the ambiguity was detected ambiguities : set Set of type signature pairs that are ambiguous within this dispatcher See Also: Dispatcher.add warning_text N)r warning_textnamer ) dispatcherrs rambiguity_warnrs joo{ 35EFrceZdZdZdZdZy)RaiseNotImplementedErrorz*Raise ``NotImplementedError`` when called.c||_yN)r)selfrs r__init__z!RaiseNotImplementedError.__init__$s $rctd|D}td|jjdt |d)Nc32K|]}t|ywr)type).0as r z4RaiseNotImplementedError.__call__..(s,!d1g,szAmbiguous signature for : <>)tupleNotImplementedErrorrr str_signature)rargskwargstypess r__call__z!RaiseNotImplementedError.__call__'s8,t,,! OO -"6   rN)r rrrrr-rrrrr!s4% rrc|D]O}tt|}tt|dk(r/|j |t |t Qy)a If super signature for ambiguous types is duplicate types, ignore it. Else, register instance of ``RaiseNotImplementedError`` for ambiguous types. Parameters ---------- dispatcher : Dispatcher The dispatcher on which the ambiguity was detected ambiguities : set Set of type signature pairs that are ambiguous within this dispatcher See Also: Dispatcher.add ambiguity_warn r on_ambiguityN)r'rlensetaddr#ambiguity_register_error_ignore_dup)rramb signatures rr4r4.sU  /#./ s9~ ! #  / ;<   rzset[Dispatcher]_unresolved_dispatchersTcdtd<y)NFr)_resolverrr halt_orderingr:Ns HQKrc~dtd<tr.tj}|j|tr-yy)NTrr/)r9r7popreorder)r0rs rrestart_orderingr>Rs5HQK !,002  5 " !rceZdZdZdZddZdZedZedZ e fdZ e fd Z d Z d ZeZd Zd ZdZdZdZedZdZdZdZdZy) Dispatcheraj Dispatch methods based on type signature Use ``dispatch`` to add implementations Examples -------- >>> from sympy.multipledispatch import dispatch >>> @dispatch(int) ... def f(x): ... return x + 1 >>> @dispatch(float) ... def f(x): # noqa: F811 ... return x - 1 >>> f(3) 4 >>> f(3.0) 2.0 )r rfuncsr_cachedocNcX|x|_|_i|_i|_g|_||_yr)rr rArBrrC)rrrCs rrzDispatcher.__init__qs,$(( DM   rcfd}|S)a# Register dispatcher with new implementation >>> from sympy.multipledispatch.dispatcher import Dispatcher >>> f = Dispatcher('f') >>> @f.register(int) ... def inc(x): ... return x + 1 >>> @f.register(float) ... def dec(x): ... return x - 1 >>> @f.register(list) ... @f.register(tuple) ... def reverse(x): ... return x[::-1] >>> f(1) 2 >>> f(1.0) 0.0 >>> f([1, 2, 3]) [3, 2, 1] c0j|fi|Sr)r3)funcr+rr,s r_zDispatcher.register.._s DHHUD +F +Krr)rr,r+rHs``` rregisterzDispatcher.registerxs6 rcttdr/tj|}|jj Sy)Nr6)hasattrinspectr6 parametersvaluesclsrGsigs rget_func_paramszDispatcher.get_func_paramss4 7K (##D)C>>((* * )rc|j|}|rDtjfd|D}td|D}t fd|Ds|Syy)z; Get annotations of function positional parameters c3jK|]*}|jjjfvr|,ywr)kindPOSITIONAL_ONLYPOSITIONAL_OR_KEYWORD)r"param Parameters rr$z2Dispatcher.get_func_annotations..s;9 00 66889s03c34K|]}|jywr) annotation)r"rXs rr$z2Dispatcher.get_func_annotations..s %   %c3:K|]}|juywr)empty)r"annrYs rr$z2Dispatcher.get_func_annotations..sE#sioo-EsN)rRrLrYr'any)rPrGparamsrrYs @rget_func_annotationszDispatcher.get_func_annotationssk$$T* ))I99F   %# %%KEEE""F rc |s|j|}|r|}td|Dr$t|D]}|j|||y|D]H}t |t rdj d|D}td|d|d|j||j|<|j||jjy) a Add new types/method pair to dispatcher >>> from sympy.multipledispatch import Dispatcher >>> D = Dispatcher('add') >>> D.add((int, int), lambda x, y: x + y) >>> D.add((float, float), lambda x, y: x + y) >>> D(1, 2) 3 >>> D(1, 2.0) Traceback (most recent call last): ... NotImplementedError: Could not find signature for add: When ``add`` detects a warning it calls the ``on_ambiguity`` callback with a dispatcher/itself, and a set of ambiguous type signature pairs as inputs. See ``ambiguity_warn`` for an example. c3<K|]}t|tywr) isinstancer')r"typs rr$z!Dispatcher.add..s;#z#u%;sN, c3jK|]+}t|tr |jn t|-ywr)rer!r str)r"cs rr$z!Dispatcher.add..s3$D453=Q2EAJJ),Q%0$Ds13zTried to dispatch on non-type: z In signature: In function: r/) rbr`r r3rer!join TypeErrorrrAr=rBclear)rr6rGr0rtypsrfstr_sigs rr3zDispatcher.adds(33D9K'  ;; ;%i0 3t\2 3  ;Cc4())$D9B$DD"%gtyy!:;;  ;!% 9 , / rctdr found, but none completed successfully) r'r!rBKeyErrordispatchr(rr)r dispatch_iternext)rr*r+argr,rGrAs rr-zDispatcher.__call__s#D1StCy12 &;;u%D K(( (2 & 4==%(D)YY e 4677"&DKK   &% K&D&&.E K 000,  &)-M%4H'JK K KsE<ABABB(D>C D CDC*Dc d|jzS)Nz)rrs r__str__zDispatcher.__str__s 499,,rc||jvr|j|S t|j|S#t$rYywxYw)a` Deterimine appropriate implementation for this type signature This method is internal. Users should call this object as a function. Implementation resolution occurs within the ``__call__`` method. >>> from sympy.multipledispatch import dispatch >>> @dispatch(int) ... def inc(x): ... return x + 1 >>> implementation = inc.dispatch(int) >>> implementation(3) 4 >>> print(inc.dispatch(float)) None See Also: ``sympy.multipledispatch.conflict`` - module to determine resolution order N)rArvru StopIterationrr,s rrtzDispatcher.dispatchsO, DJJ ::e$ $ ***E23 3  s7 AAc'Kt|}|jD]?}t||k(sttt||s-|j |}|Aywr)r1rallmap issubclassrA)rr,nr6results rruzDispatcher.dispatch_iter#sQ J I9~"s3z5)+L'MI.  s)AAAc@tdt|j|S)z Deterimine appropriate implementation for this type signature .. deprecated:: 0.4.4 Use ``dispatch(*types)`` instead z-resolve() is deprecated, use dispatch(*types))rDeprecationWarningrtr}s rresolvezDispatcher.resolve*s% <  !t}}e$$rc4|j|jdS)NrrArrys r __getstate__zDispatcher.__getstate__5s % %rcn|d|_|d|_t|j|_i|_y)NrrA)rrArrB)rds r __setstate__zDispatcher.__setstate__9s/fI wZ  ,  rcd|jzg}|jr|j|jg}|jdddD]}|j|}|j rQdt |z}|dt|zdzz }||j jz }|j|o|jt ||r#|jddj|zdj|S) NzMultiply dispatched method: %sz Inputs: <%s> - zOther signatures: z  ) rrCappendrrArr)r1striprk)rdocsotherrQrGss rrzDispatcher.__doc__?s0499<= 88 KK !==2& 1C::c?D||$}S'99S3q6\D((T\\'')) A ]3/0 1  KK1HMM%4HH I{{4  rcP|jtt|jSr)rtrr!r)rr*s r_helpzDispatcher._helpVst}}c$o.666rc4t|j|y)z: Print docstring for the function corresponding to inputs N)printrrr*r+s rhelpzDispatcher.helpYs jdjj$ rcl|jtt|}|s tdt |S)NzNo function found)rtrr!rlsource)rr*rGs r_sourcezDispatcher._source]s1t}}c$o./0 0d|rc4t|j|y)z< Print source code for the function corresponding to inputs N)rrrs rrzDispatcher.sourcecs ldllD!"rr)r rrr __slots__rrI classmethodrRrbrr3r=r-rz__repr__rtrurrrpropertyrrrrrrrr@r@Ys*II@++ ##&1?*X$2.K4-H< %% !!,7! #rr@cfdtj|z}|tj|z}|S)Nz File: %s )rL getsourcefile getsource)rGrs rrrhs1..t44A G  d ##A Hrc,eZdZdZedZdZdZy)MethodDispatcherzP Dispatch methods based on type signature See Also: Dispatcher cttdrDtj|}tj|j j ddSy)Nr6r)rKrLr6itlislicerMrNrOs rrRz MethodDispatcher.get_func_paramsusA 7K (##D)C::cnn335q$? ? )rc"||_||_|Sr)objrP)rinstanceowners r__get__zMethodDispatcher.__get__{s rct|Dcgc] }t|c}}|j|}|s%td|jdt |d||j g|i|Scc}w)Nrrr%r&)r'r!rtr(rr)r)rr*r+rwr,rGs rr-zMethodDispatcher.__call__soD1StCy12t}}e$%'+yy-2F'HI IDHH.t.v.. 2sA/N)r rrrrrRrr-rrrrrns' @@  /rrc2djd|DS)z String representation of type signature >>> from sympy.multipledispatch.dispatcher import str_signature >>> str_signature((int, float)) 'int, float' rgc34K|]}|jywr)r )r"rPs rr$z str_signature..s1cS\\1r\)rk)rQs rr)r)s 991S1 11rc d|z}|dz }|D]#}|ddjd|Dzdzz }%|dz }|dj|Dcgc]}d tt|zd |zz!c}z }|Scc}w) z! The text for ambiguity warnings z. Ambiguities exist in dispatched function %s z;The following signatures may result in ambiguous behavior:  rgc3>K|]}dt|zdzyw)[]N)r))r"rs rr$zwarning_text..sAqcM!,,s2Asrz, Consider making the following additions: rz @dispatch(z ) def %s(...))rkr)r)rr5textpairrs rrrs >$ GD JJDI  IIADA ABDHI II >>DFKKr@rrr)rrrrrs"NN 8/8 G"    8,/50 6#16L#L#^ /z/62 r