U
    9%e-                     @   s   d dl Zd dlZd dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZmZmZmZmZ d dlmZ G d	d
 d
eZG dd deZedkre  dS )    N)TestCasenjit)typeserrorscgutils	signature)models)overloadSentryLiteralArgsoverload_methodregister_model	intrinsic	literallyc                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zejdd Zejdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%S )&TestLiteralDispatchc                 C   sR   t dd }|D ]}| ||| qt||jD ]\}}| |d j| q2d S )Nc                 S   s   t | S Nr   x r   `/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/numba/tests/test_literal_dispatch.pyfoo   s    z4TestLiteralDispatch.check_literal_basic.<locals>.foor   )r   assertEqualzip
signaturesliteral_value)selfZliteral_argsr   litsigr   r   r   check_literal_basic   s    
z'TestLiteralDispatch.check_literal_basicc                 C   s    |  ddg |  ddg d S )N{   A  abcZcb123)r    r   r   r   r   test_literal_basic   s    z&TestLiteralDispatch.test_literal_basicc                    s   t dd  t  fdd}d\}}| ||||d |   j\}| |d t| |j\}| |d t| | |d tj d S )	Nc                 S   s   t | d S )N   r   r   r   r   r   r   "   s    z4TestLiteralDispatch.test_literal_nested.<locals>.fooc                    s    | | S r   r   )yr   r   r   r   bar&   s    z4TestLiteralDispatch.test_literal_nested.<locals>.bar)      r&   r      )r   r   r   r   literalassertNotIsInstanceLiteral)r   r)   r'   r   foo_sigZbar_sigr   r(   r   test_literal_nested!   s    
z'TestLiteralDispatch.test_literal_nestedc                    sH   dd l  t fdd}| |dd | |jd d td d S )Nr   c                    s
     | S r   r   r   numbar   r   r   6   s    z7TestLiteralDispatch.test_literally_freevar.<locals>.foor!   )r3   r   r   r   r   r-   r   r   r   r2   r   test_literally_freevar2   s
    z*TestLiteralDispatch.test_literally_freevarc                 C   s   dd }|dd \}}|t \}}| |dd|dd | |jd d jd | |jd d jd | |d	d
|d	d
 | |jd d jd
 | |jd d jd
 d S )Nc                    s(   |  fdd| fdd  fS )Nc                    s   | dk r|S |  | d | S Nr,   r   nvalue)	inner_facr   r   	outer_fac?   s    z[TestLiteralDispatch.test_mutual_recursion_literal.<locals>.get_functions.<locals>.outer_facc                    s"   | dk rt |S |  | d | S r6   r   r7   )r;   r   r   r:   E   s    z[TestLiteralDispatch.test_mutual_recursion_literal.<locals>.get_functions.<locals>.inner_facr   )Zdecorr   )r:   r;   r   get_functions>   s
    zHTestLiteralDispatch.test_mutual_recursion_literal.<locals>.get_functionsc                 S   s   | S r   r   r   r   r   r   <lambda>M       zCTestLiteralDispatch.test_mutual_recursion_literal.<locals>.<lambda>
      r   r,         )r   r   r   r   )r   r<   Zref_outer_facZref_inner_facr;   r:   r   r   r   test_mutual_recursion_literal=   s    z1TestLiteralDispatch.test_mutual_recursion_literalc                    sp   t  fdd}t dd  tdddd}|f |}d	d
 f |}| || |j\}| |d td d S )Nc                    s
    | |S r   r   abcinnerr   r   r   Y   s    z>TestLiteralDispatch.test_literal_nested_multi_arg.<locals>.fooc                 S   s   | t | S r   r   r   r'   r   r   r   rI   ]   s    z@TestLiteralDispatch.test_literal_nested_multi_arg.<locals>.innerr,   r&   r*   rD   c                 S   s   | | S r   r   rD   r   r   r   r=   c   r>   zCTestLiteralDispatch.test_literal_nested_multi_arg.<locals>.<lambda>)r   dictr   r   r   r-   )r   r   kwargsgotexpectr0   r   rH   r   test_literal_nested_multi_argX   s    

z1TestLiteralDispatch.test_literal_nested_multi_argc              	      sb   t  fdd}t dd  td}| tj}|dd|d W 5 Q R X | d	t|j d S )
Nc                    s
    | |S r   r   rD   rH   r   r   r   i   s    z>TestLiteralDispatch.test_unsupported_literal_type.<locals>.fooc                 S   s   | t | S r   r   rJ   r   r   r   rI   m   s    z@TestLiteralDispatch.test_unsupported_literal_type.<locals>.innerr?   r,   r&   rD   znumpy.ndarray)	r   npZarangeassertRaisesr   LiteralTypingErrorassertInstr	exception)r   r   Zarrraisesr   rH   r   test_unsupported_literal_typeh   s    

z1TestLiteralDispatch.test_unsupported_literal_typec                    s   t  fdd}t dd  tdddd}|f |}d	d
 f |}| || |j\\}}}| |tj | |tj | |jd | |tj | |jd d S )Nc                    s    | | || S r   r   rD   rH   r   r   r   x   s    z/TestLiteralDispatch.test_biliteral.<locals>.fooc                 S   s   | t | S r   r   rJ   r   r   r   rI   |   s    z1TestLiteralDispatch.test_biliteral.<locals>.innerr,   r&   r*   rD   c                 S   s   | | | | S r   r   rD   r   r   r   r=      r>   z4TestLiteralDispatch.test_biliteral.<locals>.<lambda>	r   rK   r   r   r.   r   r/   assertIsInstancer   r   r   rL   rM   rN   type_atype_bZtype_cr   rH   r   test_biliteralv   s    

z"TestLiteralDispatch.test_biliteralc              	      sx   t dd  | tj  ddd W 5 Q R X t  fdd}| tj}|dd W 5 Q R X | dt|j d S )	Nc                 W   s   t |S r   r   )rE   argsr   r   r   r      s    z7TestLiteralDispatch.test_literally_varargs.<locals>.foor,   r&   r*   c                    s    | | d S r   r   rE   rF   r(   r   r   r)      s    z7TestLiteralDispatch.test_literally_varargs.<locals>.barzCannot request literal type)r   rQ   r   rR   TypingErrorrS   rT   rU   )r   r)   rV   r   r(   r   test_literally_varargs   s    
z*TestLiteralDispatch.test_literally_varargsc                 C   s   t ddd}|dd d S )Nr,   c                 S   s   | t |fS r   r   r_   r   r   r   r      s    z8TestLiteralDispatch.test_literally_defaults.<locals>.foorE   )r,   r   r4   r   r   r   test_literally_defaults   s    z+TestLiteralDispatch.test_literally_defaultsc                    s*   t ddd t  fdd}|d d S )Nr,   c                 S   s   | t |fS r   r   r_   r   r   r   r      s    z>TestLiteralDispatch.test_literally_defaults_inner.<locals>.fooc                    s    | d S r6   r   rb   r(   r   r   r)      s    z>TestLiteralDispatch.test_literally_defaults_inner.<locals>.bar)r,   r   )r   r)   r   r(   r   test_literally_defaults_inner   s
    z1TestLiteralDispatch.test_literally_defaults_innerc                 C   sB   t dd }|d}| ||d | |jd d tj d S )Nc                 S   s
   t | S r   )r3   r   r   r   r   r   r      s    z;TestLiteralDispatch.test_literally_from_module.<locals>.foor!   r   )r   r   py_funcrY   r   r   r/   )r   r   rM   r   r   r   test_literally_from_module   s
    
z.TestLiteralDispatch.test_literally_from_modulec              	   C   sD   t dd }| tj}|dd W 5 Q R X | dt|j d S )Nc                 S   s   t d|  S r6   r   r_   r   r   r   r      s    z1TestLiteralDispatch.test_non_literal.<locals>.foor,   r&   zInvalid use of non-Literal type)r   rQ   r   r`   rS   rT   rU   )r   r   rV   r   r   r   test_non_literal   s    
z$TestLiteralDispatch.test_non_literalc                    sT   t dd   dd}| | dd t  fdd}| }| ||  d S )Nc                 S   s   d}| t | t | S )Ni  r   )rE   rF   vr   r   r   r      s    z5TestLiteralDispatch.test_inlined_literal.<locals>.foor,   r&   c                     s   d} d} || dS )Nd   	   r_   r   r_   r(   r   r   r)      s    z5TestLiteralDispatch.test_inlined_literal.<locals>.bar)r   r   re   )r   rM   r)   r   r(   r   test_inlined_literal   s    

z(TestLiteralDispatch.test_inlined_literalc                    s   t  fdd}t dd  tdddd}|f |}d	d
 f |}| || |j\\}}}| |tj | |tj | |jd | |tj | |jd d S )Nc                    s    fdd}||S )Nc                    s   t | d   S Nr?   r   )d)rE   rF   rI   r   r   closure   s    zGTestLiteralDispatch.test_aliased_variable.<locals>.foo.<locals>.closurer   )rE   rF   rG   rn   rH   r_   r   r      s    z6TestLiteralDispatch.test_aliased_variable.<locals>.fooc                 S   s   | t | S r   r   rJ   r   r   r   rI      s    z8TestLiteralDispatch.test_aliased_variable.<locals>.innerr,   r&   r*   rD   c                 S   s   |d| |   S rl   r   rD   r   r   r   r=      r>   z;TestLiteralDispatch.test_aliased_variable.<locals>.<lambda>rX   rZ   r   rH   r   test_aliased_variable   s    

z)TestLiteralDispatch.test_aliased_variablec                    s   dd  t  fddt fdd}d}d}|||}| |||  |jd	 \}}| |tj | |j| | |tj d S )
Nc                 S   s   | | S r   r   rJ   r   r   r   do_this   s    z;TestLiteralDispatch.test_overload_explicit.<locals>.do_thisc                    s    t dg | | dd S )Nr   c                 S   s   | | S r   r   rJ   r   r   r   r=      r>   zPTestLiteralDispatch.test_overload_explicit.<locals>.ov_do_this.<locals>.<lambda>)r   Zfor_functionbindrJ   )
ov_do_thisr   r   rr      s    z>TestLiteralDispatch.test_overload_explicit.<locals>.ov_do_thisc                    s
    | |S r   r   r_   rp   r   r   r      s    z7TestLiteralDispatch.test_overload_explicit.<locals>.foor!   r"   r   )	r   r   r   r   rY   r   r/   r   r.   )r   r   rE   rF   rr[   r\   r   )rp   rr   r   test_overload_explicit   s    
z*TestLiteralDispatch.test_overload_explicitc                    s   dd  t dd t fdd}t  fdd}d	}d
}|||}| |||  |jd \}}| |tj | |j| | |tj d S )Nc                 S   s   | | S r   r   rJ   r   r   r   rp     s    z;TestLiteralDispatch.test_overload_implicit.<locals>.do_thisc                 S   s   t | | S r   r   rJ   r   r   r   hidden  s    z:TestLiteralDispatch.test_overload_implicit.<locals>.hiddenc                    s   t | tjr fddS d S )Nc                    s
    | |S r   r   rJ   rv   r   r   r=     r>   zPTestLiteralDispatch.test_overload_implicit.<locals>.ov_do_this.<locals>.<lambda>)
isinstancer   IntegerrJ   rw   r   r   rr     s    z>TestLiteralDispatch.test_overload_implicit.<locals>.ov_do_thisc                    s
    | |S r   r   r_   rs   r   r   r     s    z7TestLiteralDispatch.test_overload_implicit.<locals>.foor!   r"   r   )	r   r   r   r   rY   r   r/   r   r.   )r   rr   r   rE   rF   rt   r[   r\   r   rp   rv   r   test_overload_implicit  s    

z*TestLiteralDispatch.test_overload_implicitc              	      sr   dd  t dd t fdd}t  fdd}| tj}|d	d
d W 5 Q R X | dt|j d S )Nc                 S   s   | | S r   r   rJ   r   r   r   rp   .  s    z=TestLiteralDispatch.test_overload_error_loop.<locals>.do_thisc                 S   s   t | | S r   r   rJ   r   r   r   rv   1  s    z<TestLiteralDispatch.test_overload_error_loop.<locals>.hiddenc                    s,   t |tjrtdn fdd}|S d S )NZoopsc                    s
    | |S r   r   rJ   rw   r   r   impl<  s    zNTestLiteralDispatch.test_overload_error_loop.<locals>.ov_do_this.<locals>.impl)rx   r   ZIntegerLiteralr   ZNumbaValueError)r   r'   r|   rw   r   r   rr   5  s    z@TestLiteralDispatch.test_overload_error_loop.<locals>.ov_do_thisc                    s    | t |S r   r   r_   rs   r   r   r   @  s    z9TestLiteralDispatch.test_overload_error_loop.<locals>.foor!   r"   r_   zRepeated literal typing request)r   r   rQ   r   ZCompilerErrorrS   rT   rU   )r   rr   r   rV   r   rz   r   test_overload_error_loop)  s    

z,TestLiteralDispatch.test_overload_error_loopN)__name__
__module____qualname__r    r%   r1   r5   rC   rO   rW   r]   ra   unittestZexpectedFailurerc   rd   rf   rg   rk   ro   ru   r{   r}   r   r   r   r   r      s(   


r   c                   @   s   e Zd Zdd Zdd ZdS )!TestLiteralDispatchWithCustomTypec                    sp   G dd dt }G  fdddtj t G fdddtjt fddt|fd	d
} |fS )Nc                   @   s   e Zd Zdd ZdS )z@TestLiteralDispatchWithCustomType.make_dummy_type.<locals>.Dummyc                 S   s   |S r   r   r   rE   r   r   r   r   O  s    zDTestLiteralDispatchWithCustomType.make_dummy_type.<locals>.Dummy.litN)r~   r   r   r   r   r   r   r   DummyN  s   r   c                       s   e Zd Z fddZ  ZS )zDTestLiteralDispatchWithCustomType.make_dummy_type.<locals>.DummyTypec                    s   t  | jdd d S )Ndummy)namesuper__init__r$   )	DummyType	__class__r   r   r   S  s    zMTestLiteralDispatchWithCustomType.make_dummy_type.<locals>.DummyType.__init__r~   r   r   r   __classcell__r   r   r   r   r   R  s   r   c                       s   e Zd Z fddZ  ZS )zITestLiteralDispatchWithCustomType.make_dummy_type.<locals>.DummyTypeModelc                    s   g }t  | ||| d S r   r   )r   ZdmmZfe_typemembers)DummyTypeModelr   r   r   r   X  s    zRTestLiteralDispatchWithCustomType.make_dummy_type.<locals>.DummyTypeModel.__init__r   r   )r   r   r   r   V  s   r   c                    s   dd }t   }||fS )Nc                 S   s   t |j| |}| S r   )r   Zcreate_struct_proxyreturn_typeZ	_getvalue)contextZbuilderr	   r^   r   r   r   r   codegen^  s     zVTestLiteralDispatchWithCustomType.make_dummy_type.<locals>.init_dummy.<locals>.codegenr   )Z	typingctxr   r   r   r   r   
init_dummy\  s    
zETestLiteralDispatchWithCustomType.make_dummy_type.<locals>.init_dummyc                     s    fdd} | S )Nc                      s     S r   r   r   r   r   r   ctori  s    zWTestLiteralDispatchWithCustomType.make_dummy_type.<locals>.dummy_overload.<locals>.ctorr   )r   r   r   r   dummy_overloadg  s    zITestLiteralDispatchWithCustomType.make_dummy_type.<locals>.dummy_overload)objectr   Typer   r
   ZStructModelr   r   )r   r   r   r   )r   r   r   r   make_dummy_typeM  s    
z1TestLiteralDispatchWithCustomType.make_dummy_typec              	      s   |   \} t|ddd }t fdd| dd tfdd}| tj}|d	 W 5 Q R X | d
t|j	 d S )Nr   c                 S   s   dd }|S )Nc                 S   s   t |S r   r   r   r   r   r   r|   v  s    zZTestLiteralDispatchWithCustomType.test_overload_method.<locals>.lit_overload.<locals>.implr   )r   rE   r|   r   r   r   lit_overloadt  s    zLTestLiteralDispatchWithCustomType.test_overload_method.<locals>.lit_overloadc                    s     }| | S r   )r   )rE   rm   )r   r   r   	test_impl{  s    zITestLiteralDispatchWithCustomType.test_overload_method.<locals>.test_impl   c                    s    | d S r6   r   rb   )r   r   r   inside  s    zFTestLiteralDispatchWithCustomType.test_overload_method.<locals>.inside   zCannot request literal type.)
r   r   r   r   rQ   r   r`   rS   rT   rU   )r   r   r   r   rV   r   )r   r   r   test_overload_methodp  s    
z6TestLiteralDispatchWithCustomType.test_overload_methodN)r~   r   r   r   r   r   r   r   r   r   L  s   #r   __main__)numpyrP   r3   r   Znumba.tests.supportr   r   Z
numba.corer   r   r   Znumba.core.typingr	   Znumba.core.datamodelr
   Znumba.core.extendingr   r   r   r   r   Znumba.misc.specialr   r   r   r~   mainr   r   r   r   <module>   s     >C