
    sh"6                        S SK r S SKrS SKJr  S SKJr  SSKJrJ	r	J
r
Jr  SSKJr  SSKJrJr  / SQr " S	 S
\5      rS r\" S\S9S 5       r\" S\S9\4S j5       rS rS r " S S5      rS r " S S\5      rS rS rg)    N)
deprecated)warn   )ambiguitiesAmbiguityWarningorderingsuper_signature)expand_tuples)
isvariadicVariadic)MDNotImplementedErrorambiguity_warnhalt_orderingrestart_orderingvariadic_signature_matches_itervariadic_signature_matches
DispatchersourceMethodDispatcherstr_signaturewarning_textc                       \ rS rSrSrSrg)r      z+A NotImplementedError for multiple dispatch N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       ڑ/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/fx/experimental/unification/multipledispatch/dispatcher.pyr   r      s    5r!   r   c                 L    [        [        U R                  U5      [        5        g)a  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   r   namer   )
dispatcherr   s     r"   r   r      s     	joo{	35EFr!   z?`halt_ordering` is deprecated, you can safely remove this call.categoryc                      g)z5Deprecated interface to temporarily disable ordering.Nr   r   r!   r"   r   r   .       r!   z`restart_ordering` is deprecated, if you would like to eagerly order the dispatchers, you should call the `reorder()` method on each dispatcher.c                     g)z4Deprecated interface to temporarily resume ordering.Nr   )on_ambiguitys    r"   r   r   6   r)   r!   c              #     #    [        U5      n[        U5      nU  H/  n[        XC5      nUv   [        U5      (       a  M$  [        U5      nM1      [        U5      nSv   g! [         a    [        U5      (       d   eSv    gf = f7f)a'  Check if a set of input types matches a variadic signature.
Notes
-----
The algorithm is as follows:
Initialize the current signature to the first in the sequence
For each type in `types`:
    If the current signature is variadic
        If the type matches the signature
            yield True
        Else
            Try to get the next signature
            If no signatures are left we can't possibly have a match
                so yield False
    Else
        yield True if the type matches the current signature
        Get the next signature
FTN)iternext
issubclassr   StopIteration)typesfull_signaturesigitersigtypmatchess         r"   r   r   ?   s     $ >"G
w-CS&# w-C 	w-C K  	c??"?J	s.   9BBA B B?BBBc                 <    U(       d   e[        [        X5      5      $ N)allr   )r1   r2   s     r"   r   r   f   s    >.uEFFr!   c                       \ rS rSrSrSrSS jrS r\S 5       r	\S 5       r
S	 r\S
 5       r\4S jrS rS r\rS rS r\" S\S9S 5       rS rS r\S 5       rS rS rS rS rSrg)r   l   a>  Dispatch methods based on type signature
Use ``dispatch`` to add implementations
Examples
--------
>>> # xdoctest: +SKIP("bad import name")
>>> from multipledispatch import dispatch
>>> @dispatch(int)
... def f(x):
...     return x + 1
>>> @dispatch(float)
... def f(x):
...     return x - 1
>>> f(3)
4
>>> f(3.0)
2.0
)r   r$   funcs	_ordering_cachedocNc                 H    U=U l         U l        0 U l        X l        0 U l        g r8   )r$   r   r<   r?   r>   )selfr$   r?   s      r"   __init__Dispatcher.__init__   s#    $((	DM
r!   c                    ^ ^^ UU U4S jnU$ )a]  register dispatcher with new implementation
>>> # xdoctest: +SKIP
>>> 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]
c                 0   > TR                   " TU 40 TD6  U $ r8   )add)funckwargsrA   r1   s    r"   _df Dispatcher.register.<locals>._df   s    HHUD+F+Kr!   r   )rA   r1   rH   rI   s   ``` r"   registerDispatcher.register   s    ,	 
r!   c                     [        [        S5      (       a0  [        R                  " U5      nUR                  R	                  5       $ g )N	signature)hasattrinspectrN   
parametersvaluesclsrG   r4   s      r"   get_func_paramsDispatcher.get_func_params   s7    7K((##D)C>>((** )r!   c                    ^ U R                  U5      nU(       aK  [        R                  mU4S jU 5       n[        S U 5       5      n[	        U4S jU 5       5      (       a  U$ gg)z1get annotations of function positional parametersc              3   x   >#    U  H/  nUR                   TR                  TR                  4;   d  M+  Uv   M1     g 7fr8   )kindPOSITIONAL_ONLYPOSITIONAL_OR_KEYWORD).0param	Parameters     r"   	<genexpr>2Dispatcher.get_func_annotations.<locals>.<genexpr>   s;      #E::--y/N/NOP #s   *:	:c              3   8   #    U  H  oR                   v   M     g 7fr8   )
annotation)r\   r]   s     r"   r_   r`      s     EfU 0 0f   c              3   >   >#    U  H  oTR                   Lv   M     g 7fr8   )empty)r\   annr^   s     r"   r_   r`      s     E#ioo-s   N)rU   rP   r^   tupler9   )rT   rG   paramsannotationsr^   s       @r"   get_func_annotationsDispatcher.get_func_annotations   sf     $$T*))I#F  EfEEKEEEE"" F r!   c           	         U(       d  U R                  U5      nU(       a  Un[        S U 5       5      (       a$  [        U5       H  nU R                  XB5        M     g/ n[	        USS9 H  u  pg[        U[        [        45      (       d6  SR                  S U 5       5      n[        SU SU S	U R                   35      e[        U[        5      (       aQ  U[        U5      :w  a  [        S
5      e[        U5      S:w  a  [        S5      eUR                  [        US      5        M  UR                  U5        M     X R                  [        U5      '   U R                   R#                  5          U ?g! [&         a     gf = f)a  Add new types/method pair to dispatcher
>>> # xdoctest: +SKIP
>>> 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: <int, float>
>>> # 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.
c              3   B   #    U  H  n[        U[        5      v   M     g 7fr8   )
isinstancerg   )r\   r5   s     r"   r_   !Dispatcher.add.<locals>.<genexpr>   s     ;#z#u%%s   Nr   )start, c              3   z   #    U  H1  n[        U[        5      (       a  UR                  O
[        U5      v   M3     g 7fr8   )rn   typer   str)r\   cs     r"   r_   ro      s,      $KTa*Q"5"5AJJ3q6A9s   9;zTried to dispatch on non-type: z
In signature: <z>
In function: z+Variadic signature must be the last elementzVariadic signature must contain exactly one element. To use a variadic union type place the desired types inside of a tuple, e.g., [(int, str)]r   )rj   anyr
   rF   	enumeratern   rs   listjoin	TypeErrorr$   lenappendr   r<   rg   r>   clearr=   AttributeError)	rA   rN   rG   ri   typsnew_signatureindexr5   str_sigs	            r"   rF   Dispatcher.add   sr   " 33D9K'	 ;;;;%i0$ 1#IQ7JEcD$<00)) $KT$   5cU ;&&-Y /$$(II;0  #t$$C	N*#$QRRs8q=#@ 
 $$Xc!f%56$$S)1 84 ,0

5'(	 		s   ,E/ /
E<;E<c                 \     U R                   $ ! [         a    U R                  5       s $ f = fr8   )r=   r~   reorderrA   s    r"   r   Dispatcher.ordering   s,    	">>! 	"<<>!	"s    ++c                     [        U R                  5      =U l        n[        U R                  5      nU(       a  U" X5        U$ r8   )r   r<   r=   r   )rA   r+   odambs       r"   r   Dispatcher.reorder  s5    &tzz22$**%#	r!   c           	      >   [        U Vs/ s H  n[        U5      PM     sn5      n U R                  U   n U" U0 UD6$ s  snf ! [         aT  nU R                  " U6 nU(       d&  [        SU R                   S[        U5       S35      UeXPR                  U'    S nANgS nAff = f! [         am  nU R                  " U6 n[        U5        U H"  n U" U0 UD6s  s S nA$ ! [         a     M   f = f   [        SU R                   S[        U5       S35      UeS nAff = f)NCould not find signature for : <>zMatching functions for z(> found, but none completed successfully)rg   rs   r>   KeyErrordispatchNotImplementedErrorr$   r   r   dispatch_iterr.   )rA   argsrH   argr1   rG   er<   s           r"   __call__Dispatcher.__call__  s9   D1DStCyD12	&;;u%D	((( 2  	&==%(D)3DII;c-PUBVAWWXY "&KK	& % 	&&.EK000,   &)99+Su!5 66^` 	s]   ?A B% 
B"A
BB"%
D/DCDD
C,(D+C,,+DDc                 "    SU R                    S3$ )Nz<dispatched r   )r$   r   s    r"   __str__Dispatcher.__str__'  s    dii[**r!   c                     XR                   ;   a  U R                   U   $  [        U R                  " U6 5      $ ! [         a     gf = f)a  Determine 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.
>>> # xdoctest: +SKIP
>>> from 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:
  ``multipledispatch.conflict`` - module to determine resolution order
N)r<   r.   r   r0   rA   r1   s     r"   r   Dispatcher.dispatch,  sJ    $ JJ::e$$	**E233 		s   8 
AAc              '   d  #    [        U5      nU R                   H  n[        U5      U:X  a3  [        [        [        X5      5      (       a  U R
                  U   nUv   ME  [        U5      (       d  MW  [        US   5      (       d  Ml  [        X5      (       d  M~  U R
                  U   nUv   M     g 7f)N)r{   r   r9   mapr/   r<   r   r   )rA   r1   nrN   results        r"   r   Dispatcher.dispatch_iterF  s     JI9~"s3z5+L'M'MI.YJy}$=$=-e??!ZZ	2F L 's   A+B01B0B0B0z1`resolve()` is deprecated, use `dispatch(*types)`r&   c                      U R                   " U6 $ )zxDetermine appropriate implementation for this type signature
.. deprecated:: 0.4.4
    Use ``dispatch(*types)`` instead
)r   r   s     r"   resolveDispatcher.resolveQ  s     }}e$$r!   c                 4    U R                   U R                  S.$ )Nr$   r<   r   r   s    r"   __getstate__Dispatcher.__getstate__[  s    		DJJ77r!   c                 n    US   U l         US   U l        [        U R                  5      U l        0 U l        g )Nr$   r<   )r$   r<   r   r=   r>   )rA   ds     r"   __setstate__Dispatcher.__setstate__^  s/    fI	wZ
!$**-r!   c                 >   SU R                    3/nU R                  (       a  UR                  U R                  5        / nU R                  S S S2    H  nU R                  U   nUR
                  (       aR  S[        U5       S3nUS[        U5      -  S-   -  nXTR
                  R                  5       -  nUR                  U5        Mu  UR                  [        U5      5        M     U(       a#  UR                  SSR                  U5      -   5        S	R                  U5      $ )
NzMultiply dispatched method: r   z	Inputs: <z>
-
zOther signatures:
    z
    

)
r$   r?   r|   r   r<   r   r   r{   stripry   )rA   docsotherr4   rG   ss         r"   r   Dispatcher.__doc__d  s    .tyyk:;88KK!==2&C::c?D||c 2337S3q6\D((\\''))A]3/0 ' KK1HMM%4HHI{{4  r!   c                 P    U R                   " [        [        U5      6 R                  $ r8   )r   r   rs   r   )rA   r   s     r"   _helpDispatcher._help{  s    }}c$o.666r!   c                 4    [        U R                  " U6 5        g)z8Print docstring for the function corresponding to inputsN)printr   rA   r   rH   s      r"   helpDispatcher.help~  s    djj$ r!   c                 v    U R                   " [        [        U5      6 nU(       d  [        S5      e[	        U5      $ )NzNo function found)r   r   rs   rz   r   )rA   r   rG   s      r"   _sourceDispatcher._source  s/    }}c$o./00d|r!   c                 4    [        U R                  " U6 5        g)z:Print source code for the function corresponding to inputsN)r   r   r   s      r"   r   Dispatcher.source  s    dllD!"r!   )r   r>   r=   r?   r<   r$   r8   )r   r   r   r   r   	__slots__rB   rK   classmethodrU   rj   rF   propertyr   r   r   r   r   __repr__r   r   r   FutureWarningr   r   r   r   r   r   r   r    r   r!   r"   r   r   l   s    $ JI8 + +
 # #$>@ " " $2 8+ H4	! ;m%%8 ! !,7!#r!   r   c                 l    S[         R                  " U 5       S3nU[         R                  " U 5      -   nU$ )NzFile: r   )rP   getsourcefile	getsource)rG   r   s     r"   r   r     s6    
&&t,-T2A	Gd##AHr!   c                   8    \ rS rSrSrSr\S 5       rS rS r	Sr
g)	r   i  zBDispatch methods based on type signature
See Also:
    Dispatcher
objrT   c                     [        [        S5      (       aF  [        R                  " U5      n[        R                  " UR
                  R                  5       SS 5      $ g )NrN   r   )rO   rP   rN   itlislicerQ   rR   rS   s      r"   rU    MethodDispatcher.get_func_params  sD    7K((##D)C::cnn335q$?? )r!   c                     Xl         X l        U $ r8   r   )rA   instanceowners      r"   __get__MethodDispatcher.__get__  s    r!   c                     [        U Vs/ s H  n[        U5      PM     sn5      nU R                  " U6 nU(       d%  [        SU R                   S[        U5       S35      eU" U R                  /UQ70 UD6$ s  snf )Nr   r   r   )rg   rs   r   r   r$   r   r   )rA   r   rH   r   r1   rG   s         r"   r   MethodDispatcher.__call__  sz    D1DStCyD12}}e$%/		{#mE>R=SSTU  DHH.t.v.. 2s   A7)rT   r   N)r   r   r   r   r   r   r   rU   r   r   r    r   r!   r"   r   r     s,    
 I@ @

/r!   r   c                 2    SR                  S U  5       5      $ )zUString representation of type signature
>>> str_signature((int, float))
'int, float'
rq   c              3   8   #    U  H  oR                   v   M     g 7fr8   )r   )r\   rT   s     r"   r_    str_signature.<locals>.<genexpr>  s     1Sc\\Src   )ry   )r4   s    r"   r   r     s    
 991S111r!   c                    SU  S3nUS-  nU H$  nUSSR                  S U 5       5      -   S-   -  nM&     US-  nUSR                  U Vs/ s H!  nS	[        [        U5      5      -   S
U  S3-   PM#     sn5      -  nU$ s  snf )zThe text for ambiguity warningsz*
Ambiguities exist in dispatched function r   z;The following signatures may result in ambiguous behavior:
	rq   c              3   D   #    U  H  nS [        U5      -   S-   v   M     g7f)[]N)r   )r\   r   s     r"   r_   warning_text.<locals>.<genexpr>  s      Lt!}Q'7!7#!=ts    r   z,

Consider making the following additions:

z
@dispatch(z)
def z(...))ry   r   r	   )r$   r   textpairr   s        r"   r   r     s    8dCDJJDtyy Lt LLLtSS >>DFKK 	
 =);<<e?TT	
 D K	
s   (A>
)rP   	itertoolsr   typing_extensionsr   warningsr   conflictr   r   r   r	   utilsr
   variadicr   r   __all__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r"   <module>r      s      (  N N   *6/ 6G E@	@ A
 #1 ?
?$NG^# ^#B	/z /:2r!   