o
    §ƒðjÂ/  ã                   @  sÞ   U d dl mZ d dlmZ d dlZddlmZmZmZm	Z	 ddl
mZ d dlZG dd„ deƒZd	d
„ ZG dd„ dƒZdd„ Zeƒ Zded< dgZdd„ Zefdd„ZG dd„ dƒZdd„ ZG dd„ deƒZdd„ Zdd„ ZdS ) é    )Úannotations)ÚwarnNé   )ÚorderingÚambiguitiesÚsuper_signatureÚAmbiguityWarning)Úexpand_tuplesc                   @  s   e Zd ZdZdS )ÚMDNotImplementedErrorz- A NotImplementedError for multiple dispatch N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú£/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/multipledispatch/dispatcher.pyr
   
   s    r
   c                 C  s   t t| j|ƒtƒ dS )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_textÚnamer   )Ú
dispatcherr   r   r   r   Úambiguity_warn   s   r   c                   @  s    e Zd ZdZdd„ Zdd„ ZdS )ÚRaiseNotImplementedErrorz*Raise ``NotImplementedError`` when called.c                 C  s
   || _ d S ©N)r   )Úselfr   r   r   r   Ú__init__$   ó   
z!RaiseNotImplementedError.__init__c                 O  s*   t dd„ |D ƒƒ}td| jjt|ƒf ƒ‚)Nc                 s  s   � | ]}t |ƒV  qd S r   ©Útype)Ú.0Úar   r   r   Ú	<genexpr>(   s   € z4RaiseNotImplementedError.__call__.<locals>.<genexpr>z Ambiguous signature for %s: <%s>)ÚtupleÚNotImplementedErrorr   r   Ústr_signature)r   ÚargsÚkwargsÚtypesr   r   r   Ú__call__'   s   ÿÿz!RaiseNotImplementedError.__call__N)r   r   r   r   r   r%   r   r   r   r   r   !   s    r   c                 C  s@   |D ]}t t|ƒƒ}tt|ƒƒdkrq| j|t| ƒtd� qdS )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   r   ÚlenÚsetÚaddr   Ú#ambiguity_register_error_ignore_dup)r   r   ÚambÚ	signaturer   r   r   r+   .   s   þür+   zset[Dispatcher]Ú_unresolved_dispatchersTc                   C  s   dt d< d S )NFr   )Ú_resolver   r   r   r   Úhalt_orderingN   s   r0   c                 C  s,   dt d< trt ¡ }|j| d� tsd S d S )NTr   r&   )r/   r.   ÚpopÚreorder)r'   r   r   r   r   Úrestart_orderingR   s
   þr3   c                   @  s¾   e Zd ZdZdZd(dd„Zdd„ Zedd	„ ƒZed
d„ ƒZ	e
fdd„Ze
fdd„Zdd„ Zdd„ ZeZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZd d!„ Zd"d#„ Zd$d%„ Zd&d'„ ZdS ))Ú
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   r   Úfuncsr   Ú_cacheÚdocNc                 C  s(   | | _ | _i | _i | _g | _|| _d S r   )r   r   r5   r6   r   r7   )r   r   r7   r   r   r   r   q   s
   
zDispatcher.__init__c                   s   ‡ ‡‡fdd„}|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]
        c                   s   ˆj ˆ| fi ˆ ¤Ž | S r   )r*   )Úfunc©r#   r   r$   r   r   Ú_“   s   zDispatcher.register.<locals>._r   )r   r$   r#   r:   r   r9   r   Úregisterx   s   zDispatcher.registerc                 C  s"   t tdƒrt |¡}|j ¡ S d S )Nr-   )ÚhasattrÚinspectr-   Ú
parametersÚvalues©Úclsr8   Úsigr   r   r   Úget_func_params˜   s   


þzDispatcher.get_func_paramsc                   sZ   |   |¡}|r)tj‰ ‡ fdd„|D ƒ}tdd„ |D ƒƒ}t‡ fdd„|D ƒƒs+|S dS dS )z; Get annotations of function positional parameters
        c                 3  s&   � | ]}|j ˆ jˆ jfv r|V  qd S r   )ÚkindÚPOSITIONAL_ONLYÚPOSITIONAL_OR_KEYWORD©r   Úparam©Ú	Parameterr   r   r   ¦   s   € ÿÿÿÿz2Dispatcher.get_func_annotations.<locals>.<genexpr>c                 s  ó   � | ]}|j V  qd S r   )Ú
annotationrG   r   r   r   r   «   s
   € ÿ
ÿc                 3  s   � | ]}|ˆ j u V  qd S r   )Úempty)r   ÚannrI   r   r   r   ¯   ó   € N)rC   r=   rJ   r   Úany)rA   r8   Úparamsr   r   rI   r   Úget_func_annotationsž   s   
þózDispatcher.get_func_annotationsc                 C  s¨   |s|   |¡}|r|}tdd„ |D ƒƒr$t|ƒD ]	}|  |||¡ qdS |D ]}t|tƒsAd dd„ |D ƒ¡}td||| jf ƒ‚q&|| j	|< | j
|d� | j ¡  dS )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: <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                 s  s   � | ]}t |tƒV  qd S r   )Ú
isinstancer   )r   Útypr   r   r   r   Ì   rO   z!Dispatcher.add.<locals>.<genexpr>Nú, c                 s  s(   � | ]}t |tƒr|jnt|ƒV  qd S r   )rS   r   r   Ústr)r   Úcr   r   r   r   Ó   s   € ÿ
ÿzDTried to dispatch on non-type: %s
In signature: <%s>
In function: %sr&   )rR   rP   r	   r*   rS   r   ÚjoinÚ	TypeErrorr   r5   r2   r6   Úclear)r   r-   r8   r'   r   ÚtypsrT   Ústr_sigr   r   r   r*   ²   s(   


ÿ
ýý
zDispatcher.addc                 C  sB   t d rt| jƒ| _t| jƒ}|r|| |ƒ d S d S t | ¡ d S )Nr   )r/   r   r5   r   r.   r*   )r   r'   r,   r   r   r   r2   Þ   s   
ÿzDispatcher.reorderc                 O  sÞ   t dd„ |D ƒƒ}z| j| }W n  ty0   | j|Ž }|s)td| jt|ƒf ƒ‚|| j|< Y nw z||i |¤ŽW S  tyn   | j|Ž }t	|ƒ |D ]}z||i |¤ŽW    Y S  tyb   Y qKw td| jt|ƒf ƒ‚w )Nc                 S  ó   g | ]}t |ƒ‘qS r   r   ©r   Úargr   r   r   Ú
<listcomp>è   ó    z'Dispatcher.__call__.<locals>.<listcomp>ú%Could not find signature for %s: <%s>zFMatching functions for %s: <%s> found, but none completed successfully)
r   r6   ÚKeyErrorÚdispatchr    r   r!   r
   Údispatch_iterÚnext)r   r"   r#   r$   r8   r5   r   r   r   r%   ç   s8   
ÿÿú
ÿþøzDispatcher.__call__c                 C  s
   d| j  S )Nz<dispatched %s>©r   ©r   r   r   r   Ú__str__  r   zDispatcher.__str__c                 G  s:   || j v r
| j | S zt| j|Ž ƒW S  ty   Y dS w )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)r5   rf   re   ÚStopIteration©r   r$   r   r   r   rd     s   

ÿzDispatcher.dispatchc                 g  sF   � t |ƒ}| jD ]}t |ƒ|kr ttt||ƒƒr | j| }|V  qd S r   )r(   r   ÚallÚmapÚ
issubclassr5   )r   r$   Únr-   Úresultr   r   r   re   #  s   €

€ýzDispatcher.dispatch_iterc                 C  s   t dtƒ | 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))r   ÚDeprecationWarningrd   rk   r   r   r   Úresolve*  s   ÿ
zDispatcher.resolvec                 C  s   | j | jdœS )N©r   r5   rs   rh   r   r   r   Ú__getstate__5  s   ÿzDispatcher.__getstate__c                 C  s*   |d | _ |d | _t| jƒ| _i | _d S )Nr   r5   )r   r5   r   r6   )r   Údr   r   r   Ú__setstate__9  s   


zDispatcher.__setstate__c                 C  s²   d| j  g}| jr| | j¡ g }| jd d d… D ].}| j| }|jr@dt|ƒ }|dt|ƒ d 7 }||j ¡ 7 }| |¡ q| t|ƒ¡ q|rT| dd 	|¡ ¡ d 	|¡S )	NzMultiply dispatched method: %séÿÿÿÿzInputs: <%s>
ú-Ú
zOther signatures:
    z
    ú

)
r   r7   Úappendr   r5   r   r!   r(   ÚstriprX   )r   ÚdocsÚotherrB   r8   Úsr   r   r   r   ?  s   

zDispatcher.__doc__c                 G  s   | j tt|ƒŽ jS r   )rd   rm   r   r   )r   r"   r   r   r   Ú_helpV  s   zDispatcher._helpc                 O  ó   t | j|Ž ƒ dS )z: Print docstring for the function corresponding to inputs N)Úprintr€   ©r   r"   r#   r   r   r   ÚhelpY  ó   zDispatcher.helpc                 G  s$   | j tt|ƒŽ }|stdƒ‚t|ƒS )NzNo function found)rd   rm   r   rY   Úsource)r   r"   r8   r   r   r   Ú_source]  s   zDispatcher._sourcec                 O  r�   )z< Print source code for the function corresponding to inputs N)r‚   r‡   rƒ   r   r   r   r†   c  r…   zDispatcher.sourcer   )r   r   r   r   Ú	__slots__r   r;   ÚclassmethodrC   rR   r   r*   r2   r%   ri   Ú__repr__rd   re   rr   rt   rv   Úpropertyr€   r„   r‡   r†   r   r   r   r   r4   Y   s2    
 

,	
r4   c                 C  s    dt  | ¡ }|t  | ¡ }|S )Nz
File: %s

)r=   ÚgetsourcefileÚ	getsource)r8   r   r   r   r   r†   h  s   r†   c                   @  s,   e Zd ZdZedd„ ƒZdd„ Zdd„ ZdS )	ÚMethodDispatcherzP Dispatch methods based on type signature

    See Also:
        Dispatcher
    c                 C  s,   t tdƒrt |¡}t |j ¡ dd ¡S d S )Nr-   r   )r<   r=   r-   ÚitlÚislicer>   r?   r@   r   r   r   rC   u  s   

þz MethodDispatcher.get_func_paramsc                 C  s   || _ || _| S r   )ÚobjrA   )r   ÚinstanceÚownerr   r   r   Ú__get__{  s   zMethodDispatcher.__get__c                 O  sN   t dd„ |D ƒƒ}| j|Ž }|std| jt|ƒf ƒ‚|| jg|¢R i |¤ŽS )Nc                 S  r]   r   r   r^   r   r   r   r`   �  ra   z-MethodDispatcher.__call__.<locals>.<listcomp>rb   )r   rd   r    r   r!   r‘   )r   r"   r#   r$   r8   r   r   r   r%   €  s   
ÿzMethodDispatcher.__call__N)r   r   r   r   r‰   rC   r”   r%   r   r   r   r   rŽ   n  s    
rŽ   c                 C  s   d  dd„ | D ƒ¡S )z§ String representation of type signature

    >>> from sympy.multipledispatch.dispatcher import str_signature
    >>> str_signature((int, float))
    'int, float'
    rU   c                 s  rK   r   )r   )r   rA   r   r   r   r   �  s   € z str_signature.<locals>.<genexpr>©rX   )rB   r   r   r   r!   ‰  s   r!   c                   sb   dˆ  }|d7 }|D ]}|dd  dd„ |D ƒ¡ d 7 }q
|d7 }|d	  ‡ fd
d„|D ƒ¡7 }|S )z! The text for ambiguity warnings z.
Ambiguities exist in dispatched function %s

z;The following signatures may result in ambiguous behavior:
ú	rU   c                 s  s    � | ]}d t |ƒ d V  qdS )ú[ú]N)r!   ©r   r   r   r   r   r   ™  s   € zwarning_text.<locals>.<genexpr>ry   z,

Consider making the following additions:

rz   c                   s$   g | ]}d t t|ƒƒ dˆ   ‘qS )z
@dispatch(z)
def %s(...))r!   r   r™   rg   r   r   r`   ›  s
    ÿÿz warning_text.<locals>.<listcomp>r•   )r   r,   ÚtextÚpairr   rg   r   r   “  s   ÿÿ
ÿr   )Ú
__future__r   Úwarningsr   r=   Úconflictr   r   r   r   Úutilsr	   Ú	itertoolsr�   r    r
   r   r   r+   r)   r.   Ú__annotations__r/   r0   r3   r4   r†   rŽ   r!   r   r   r   r   r   Ú<module>   s*      
