U
    Jc                     @   s   d dl mZmZ d dlmZ ddddddd	d
gZG dd deZdd Zdd Z	dd Z
dd Zdd Zefdd	Zdd
 ZdS )   )	_toposortgroupby)
isvariadicAmbiguityWarning
supercedes
consistent	ambiguousambiguitiessuper_signatureedgeorderingc                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   _/tmp/pip-unpacked-wheel-gikjz4vx/torch/fx/experimental/unification/multipledispatch/conflict.pyr      s   c                 C   sB  t | t |k r.|  o,t |dko,t|d S t | t |krNttt| |S d}d}|t | k r |t |k r | | }|| }t|st|st||sdS |d7 }|d7 }qVt|r|t | d kst|t |d kot||S t|rV|t |d kstt||sdS |d7 }qV|t |d ko<|t | kS dS )z3 A is consistent and strictly more specific than B r       FN)lenr   allmap
issubclassAssertionErrorabp1p2Zcur_aZcur_br   r   r   r      s.    


c                 C   s  | s| pt |d S |s,|  p*t | d S t| t|krTtdd t| |D S d}d}|t| k r|t|k r| | }|| }t||st||sdS t |st |s|d7 }|d7 }q\t |r|d7 }q\t |r\|d7 }q\t |r|t|kpt |o|t| kS dS )z= It is possible for an argument list to satisfy both A and B r   c                 s   s&   | ]\}}t ||pt ||V  qd S N)r   ).0ZaaZbbr   r   r   	<genexpr>4   s   zconsistent.<locals>.<genexpr>Fr   N)r   r   r   zipr   r   r   r   r   r   )   s2    


c                 C   s    t | |ot| |pt||  S )z> A is consistent with B but neither is strictly more specific )r   r   r   r   r   r   r   r   K   s    c                    s$   t tt  t fdd D S )z5 All signature pairs such that A is ambiguous with B c                 3   sR   | ]J D ]@t  t k r
t r
t fd dD s
 fV  q
qdS )c                 3   s"   | ]}t | ot |V  qd S r   r   )r   cr"   r   r   r    V   s   z(ambiguities.<locals>.<genexpr>.<genexpr>N)hashr   anyr   
signaturesr"   r   r    S   s      
zambiguities.<locals>.<genexpr>)listr   tuplesetr(   r   r(   r   r	   P   s    c                    s<   t d  t fddD s&tfddt D S )z* A signature that would break ambiguities r   c                 3   s   | ]}t | kV  qd S r   )r   )r   s)nr   r   r    ]   s     z"super_signature.<locals>.<genexpr>c                    s*   g | ]" t  fd dD tdd qS )c                    s   g | ]}t |  qS r   )typemro)r   sigir   r   
<listcomp>_   s     z.super_signature.<locals>.<listcomp>.<listcomp>)keyr   )maxr   r'   r(   r2   r   r4   _   s   z#super_signature.<locals>.<listcomp>)r   r   r   ranger(   r   )r.   r)   r   r
   Z   s
    
c                 C   s&   t | |o$t ||  p$|| ||kS )zV A should be checked before B
    Tie broken by tie_breaker, defaults to ``hash``
    r#   )r   r   Ztie_breakerr   r   r   r   c   s    c                    sf   t tt   fdd D }tdd |} D ]}||kr2g ||< q2tdd | D }t|S )z A sane ordering of signatures to check, first to last
    Topoological sort of edges as given by ``edge`` and ``supercedes``
    c                    s(   g | ] } D ]}t ||r||fqqS r   )r   r   r   r   r(   r   r   r4   q   s
       
 zordering.<locals>.<listcomp>c                 S   s   | d S )Nr   r   )xr   r   r   <lambda>r       zordering.<locals>.<lambda>c                 s   s$   | ]\}}|d d |D fV  qdS )c                 S   s   g | ]\}}|qS r   r   r8   r   r   r   r4   v   s     z&ordering.<locals>.<genexpr>.<listcomp>Nr   )r   kvr   r   r   r    v   s     zordering.<locals>.<genexpr>)r*   r   r+   r   dictitemsr   )r)   edgesr-   r   r(   r   r   l   s    
N)utilsr   r   Zvariadicr   __all__Warningr   r   r   r   r	   r
   r%   r   r   r   r   r   r   <module>   s    "
		