o
    ¶2úhÔ9  ã                   @   s–   d Z ddlmZ ddlmZ ddlmZ 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 G d	d
„ d
ejƒZdd„ ZedkrIe ¡  dS dS )zTests for scope.é    )Úabsolute_import)Údivision)Úprint_functionN)Ú	ast_utils)Úscope)Ú
test_utilsc                   @   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dd„ Zdd„ Ze d¡dd„ ƒZe d¡dd„ ƒZdd„ Zdd„ Zd d!„ Zd"d#„ Zd$S )%Ú	ScopeTestc              
   C   sH  d | _ t d¡}t |¡}|j}|d jd }|d jd }|d jd }|d jd }|d }|d jd }	|d }
|d jd }|d jd }t |¡}|  	|j 
¡ h d£¡ |  	|j 
¡ h d£¡ |  	|jd	 t d	||jd	 ¡t d	||jd	 ¡g¡ |  	|jd
 t d
||jd
 ¡g¡ |  	|jd t d||jd ¡g¡ |  	|jd t d||jd jd ¡g¡ |  	|jd t d||jd	 jd
 ¡g¡ |  	|jd t d||jd	 jd
 jd ¡g¡ |  	|jd t d|d ¡g¡ |  	|jd t d|	|jd ¡g¡ |  	|jd t d|
d ¡g¡ |  	|jd t d|
d ¡g¡ |  	|jd t d||jd ¡g¡ |  	|jd t d||jd ¡g¡ |  |jd	 j|¡ |  |jd
 j|¡ |  |jd j|¡ |  |jd j|	¡ |  |jd j|¡ |  |jd j|¡ |  	|jd	 j|g¡ dD ]}|  |j| jg d| ¡ q‘d S )Nz“        import aaa
        import bbb, ccc.ddd
        import aaa.bbb.ccc
        from eee import fff
        from ggg.hhh import iii, jjj
        r   é   é   é   é   >   ÚaaaÚbbbÚcccÚfffÚiiiÚjjj>   r   r   r   ÚeeeÚgggúaaa.bbbúccc.dddúeee.fffúggg.hhhúaaa.bbb.cccúggg.hhh.iiiúggg.hhh.jjjr   r   r   r   Údddr   r   r   r   r   r   r   r   r   r   r   >   r   r   r   r   r   úExpected no reads for %s)ÚmaxDiffÚtextwrapÚdedentÚastÚparseÚbodyÚnamesr   ÚanalyzeÚassertItemsEqualÚkeysÚexternal_referencesÚExternalReferenceÚattrsÚassertIsÚ
definitionÚreadsÚassertEqual)ÚselfÚsourceÚtreeÚnodesÚ
node_1_aaaÚ
node_2_bbbÚnode_2_ccc_dddÚnode_3_aaa_bbb_cccÚ
node_4_eeeÚ
node_4_fffÚnode_5_ggg_hhhÚ
node_5_iiiÚ
node_5_jjjÚsÚref© r>   úP/var/www/html/chatgem/venv/lib/python3.10/site-packages/pasta/base/scope_test.pyÚtest_top_level_imports    s’   


ÿÿþÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿz ScopeTest.test_top_level_importsc           	      C   sì   t  d¡}t |¡}|j}t |tj¡\}}}t 	|¡}|  
|j ¡ h d£¡ |  
|j ¡ h d£¡ |  |jd j|¡ |  |jd j|¡ |  |jd j|¡ |  |jd j¡ |  |jd j¡ dD ]}|  |j| jg d	| ¡ qdd S )
Nzs        if a:
          import aaa
        elif b:
          import bbb
        else:
          import ccc
        >   ÚaÚbr   r   r   >   r   r   r   r   r   r   rA   rB   r   )r   r    r!   r"   r#   r   Úfind_nodes_by_typeÚaliasr   r%   r&   r$   r'   r(   r.   r,   ÚassertIsNoner-   ©	r/   r0   r1   r2   Únode_aaaÚnode_bbbÚnode_cccr<   r=   r>   r>   r?   Útest_if_nested_importsu   s"   


ÿÿz ScopeTest.test_if_nested_importsc           	      C   sÈ   t  d¡}t |¡}|j}t |tj¡\}}}t 	|¡}|  
|j ¡ h d£¡ |  
|j ¡ h d£¡ |  |jd j|¡ |  |jd j|¡ |  |jd j|¡ dD ]}|  |j| jg d| ¡ qRd S )Nzu        try:
          import aaa
        except:
          import bbb
        finally:
          import ccc
        >   r   r   r   r   r   r   r   )r   r    r!   r"   r#   r   rC   rD   r   r%   r&   r$   r'   r(   r.   r,   r-   rF   r>   r>   r?   Útest_try_nested_imports“   s   


ÿÿz!ScopeTest.test_try_nested_importsc                 C   sb   t  d¡}t |¡}|j}t |tj¡d }t 	|¡}|  
|j ¡ dh¡ |  
|j ¡ dh¡ d S )Nz3        def foo(bar):
          import aaa
        r   Úfoor   )r   r    r!   r"   r#   r   rC   rD   r   r%   r&   r$   r'   r(   ©r/   r0   r1   r2   rG   r<   r>   r>   r?   Útest_functiondef_nested_imports®   s   


z)ScopeTest.test_functiondef_nested_importsc                 C   sd   t  d¡}t |¡}|j}|d jd jd }t |¡}|  |j 	¡ dh¡ |  |j
 	¡ dh¡ d S )Nz2        class Foo():
          import aaa
        r   ÚFoor   )r   r    r!   r"   r#   r$   r   r%   r&   r'   r(   rM   r>   r>   r?   Útest_classdef_nested_imports½   s   


z&ScopeTest.test_classdef_nested_importsc                 C   sº   t  d¡}t |¡}|j}|d jjj}t |¡}|  	|j
 ¡ dh¡ |  	|j ¡ h d£¡ |  	|j
d j|jjg¡ |  	|j
d jd j|jg¡ |  	|j
d jd jd j|g¡ d S )Nz=        import aaa.bbb.ccc
        aaa.bbb.ccc.foo()
        r	   r   >   r   r   r   r   r   ©r   r    r!   r"   r#   ÚvalueÚfuncr   r%   r&   r$   r'   r(   r-   r*   )r/   r0   r1   r2   Únode_refr<   r>   r>   r?   Útest_multilevel_import_readsÌ   s   


ÿÿz&ScopeTest.test_multilevel_import_readsc                 C   sŠ   t  d¡}t |¡}|j}|d jd j}|d jd j}t |¡}|  	|j
 ¡ ddh¡ |  	|j ¡ dh¡ |  	|j
d j||g¡ d S )NzU        import aaa
        @aaa.x
        def foo(bar):
          return aaa
        r	   r   r   rL   )r   r    r!   r"   r#   rR   Údecorator_listr   r%   r&   r$   r'   r(   r-   )r/   r0   r1   r2   Úreturn_valueÚ	decoratorr<   r>   r>   r?   Ú test_import_reads_in_functiondefà   s   


z*ScopeTest.test_import_reads_in_functiondefc                 C   s˜   t  d¡}t |¡}|j}|d jd }|d jd j}|d jd j}t	 
|¡}|  |j ¡ ddh¡ |  |j ¡ dh¡ |  |jd j||g¡ d S )NzU        import aaa
        @aaa.x
        class Foo(aaa.Bar):
          pass
        r   r	   r   rO   )r   r    r!   r"   r#   r$   rV   rR   Úbasesr   r%   r&   r'   r(   r-   )r/   r0   r1   r2   rG   rX   Úbaser<   r>   r>   r?   Útest_import_reads_in_classdefó   s   


z'ScopeTest.test_import_reads_in_classdefc                 C   sx   t  d¡}t |¡}|j}|d jjd }t |¡}|  	|j
 ¡ ddh¡ |  	|j ¡ dh¡ |  	|j
d j|g¡ d S )NzJ        import aaa
        def foo(aaa=aaa):
          return aaa
        r	   r   r   rL   )r   r    r!   r"   r#   ÚargsÚdefaultsr   r%   r&   r$   r'   r(   r-   )r/   r0   r1   r2   Úargvalr<   r>   r>   r?   Ú"test_import_masked_by_function_arg  s   


z,ScopeTest.test_import_masked_by_function_argc                 C   sr   t  d¡}t |¡}|j}|d j}t |¡}|  |j	 
¡ ddh¡ |  |j 
¡ dh¡ |  |j	d j|g¡ d S )Nzc        import aaa
        def foo():
          aaa = 123
          return aaa
        aaa
        r
   r   rL   )r   r    r!   r"   r#   rR   r   r%   r&   r$   r'   r(   r-   rM   r>   r>   r?   Útest_import_masked_by_assign  s   



z&ScopeTest.test_import_masked_by_assignc                 C   sx   t  d¡}t |¡}|j}|d jd j}t |¡}|  	|j
 ¡ ddh¡ |  	|j ¡ dh¡ |  	|j
d j|g¡ d S )NzW        import aaa
        @aaa.wrapper
        def foo(aaa=1):
          pass
        r	   r   r   rL   )r   r    r!   r"   r#   rV   rR   r   r%   r&   r$   r'   r(   r-   )r/   r0   r1   r2   rX   r<   r>   r>   r?   Útest_import_in_decortator+  s   


z#ScopeTest.test_import_in_decortatorÚtype_annotationsc                 C   st   t  d¡}t |¡}|j}|d }t |¡}|  |j 	¡ ddh¡ |  |j
 	¡ dh¡ |  |jd j|jjg¡ d S )NzH        import aaa
        def foo() -> aaa.Foo:
          pass
        r	   r   rL   )r   r    r!   r"   r#   r   r%   r&   r$   r'   r(   r-   ÚreturnsrR   ©r/   r0   r1   r2   rS   r<   r>   r>   r?   Útest_import_in_return_type=  s   


z$ScopeTest.test_import_in_return_typec                 C   s|   t  d¡}t |¡}|j}|d }t |¡}|  |j 	¡ ddh¡ |  |j
 	¡ dh¡ |  |jd j|jjd jjg¡ d S )NzI        import aaa
        def foo(bar: aaa.Bar):
          pass
        r	   r   rL   r   )r   r    r!   r"   r#   r   r%   r&   r$   r'   r(   r-   r]   Ú
annotationrR   re   r>   r>   r?   Útest_import_in_argument_typeO  s   


ÿz&ScopeTest.test_import_in_argument_typec                 C   sà   t  d¡}t |¡}|j}|d jjj}|d jjj}|d jjj}t |¡}|  	|j
 ¡ ddh¡ |  	|j ¡ h d£¡ |  	|j
d j||j|jjg¡ |  	|j
d jd j||jg¡ |  	|j
d jd jd	 j|g¡ d S )
Nzh        import aaa.bbb.ccc, ddd.eee
        aaa.x()
        aaa.bbb.y()
        aaa.bbb.ccc.z()
        r	   r
   r   r   r   >   úddd.eeer   r   r   r   r   r   rQ   )r/   r0   r1   r2   Úcall1Úcall2Úcall3r<   r>   r>   r?   Ú test_import_attribute_referencesb  s(   


ÿÿÿÿz*ScopeTest.test_import_attribute_referencesc                 C   s:  t  d¡}t |¡}|j\}}|j\}}t |¡}| |¡}|  |j	|¡ |  ||¡ |  
|jddg¡ | |¡}	|  |	j	|¡ |  |	j|¡ |  
|	jg d¢¡ | |¡}
|  |
j	|¡ |  |
j|	¡ |  
|
jdg¡ | |¡}|  |j	|¡ |  ||	¡ |  
|jg d¢¡ |  |
 |¡|	¡ |  | tjdd¡¡ d S )Nzn        import a
        def b(c, d, e=1):
          class F(d):
            g = 1
          return c
        rA   rB   )ÚcÚdÚeÚFÚgrL   )Úid)r   r    r!   r"   r#   r   r%   Úlookup_scoper+   Únoder&   r$   Úparent_scoperE   ÚName)r/   ÚsrcÚtÚimport_nodeÚ	func_nodeÚ
class_nodeÚreturn_nodeÚscÚimport_node_scopeÚfunc_node_scopeÚclass_node_scopeÚreturn_node_scoper>   r>   r?   Útest_lookup_scope|  s2   








ÿzScopeTest.test_lookup_scopec                 C   sˆ   t  d¡}t |¡}|j\}}|j\}}t |¡}|  |j 	¡ ddh¡ |  |j
 	¡ dh¡ |  |jd j|jd j|jd jg¡ d S )Nz        import aaa
        class C:
          def aaa(self):
            return aaa

          def bbb(self):
            return aaa
        r   ÚCr   )r   r    r!   r"   r#   r   r%   r&   r$   r'   r(   r-   rR   )r/   r0   r1   Ú
importstmtÚclassdefÚ
method_aaaÚ
method_bbbr<   r>   r>   r?   Útest_class_methods¢  s   

	


ÿzScopeTest.test_class_methodsc           	      C   sŒ   t  d¡}t |¡}|j\}}|j\}}t |¡}| |¡}|  d|j	¡ |  
|j	d j|jg¡ |  d|j	¡ |  
|j	d j|jg¡ d S )Nz]        def aaa(bbb, *ccc, **ddd):
          ccc
          ddd
        eee(ccc, ddd)
        r   r   )r   r    r!   r"   r#   r   r%   rt   ÚassertInr$   r&   r-   rR   )	r/   r0   r1   ÚfuncdefÚcallÚccc_exprÚddd_exprr~   Ú
func_scoper>   r>   r?   Ú-test_vararg_kwarg_references_in_function_body¸  s   





z7ScopeTest.test_vararg_kwarg_references_in_function_bodyN)Ú__name__Ú
__module__Ú__qualname__r@   rJ   rK   rN   rP   rU   rY   r\   r`   ra   rb   r   Úrequires_featuresrf   rh   rm   rƒ   r‰   r   r>   r>   r>   r?   r      s(    U

&r   c                  C   s   t  ¡ } |  t  t¡¡ | S )N)ÚunittestÚ	TestSuiteÚaddTestsÚ	makeSuiter   )Úresultr>   r>   r?   ÚsuiteÌ  s   rš   Ú__main__)Ú__doc__Ú
__future__r   r   r   r!   r   r•   Ú
pasta.baser   r   r   ÚTestCaser   rš   r‘   Úmainr>   r>   r>   r?   Ú<module>   s$      1ÿ