
    1Vh9                         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
ej                        Zd Zedk(  r ej                           yy)zTests for scope.    )absolute_import)division)print_functionN)	ast_utils)scope)
test_utilsc                       e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Z ej                  d      d        Z ej                  d      d        Zd Zd Zd Zd Zy)	ScopeTestc           
         d | _         t        j                  d      }t        j                  |      }|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                  |      }| j                  |j                  j                         h d       | j                  |j                  j                         h d       | j                  |j                  d	   t        j                  d	||j                  d	         t        j                  d	||j                  d	         g       | j                  |j                  d
   t        j                  d
||j                  d
         g       | j                  |j                  d   t        j                  d||j                  d         g       | j                  |j                  d   t        j                  d||j                  d   j                  d         g       | j                  |j                  d   t        j                  d||j                  d	   j                  d
         g       | j                  |j                  d   t        j                  d||j                  d	   j                  d
   j                  d         g       | j                  |j                  d   t        j                  d|d       g       | j                  |j                  d   t        j                  d|	|j                  d         g       | j                  |j                  d   t        j                  d|
d       g       | j                  |j                  d   t        j                  d|
d       g       | j                  |j                  d   t        j                  d||j                  d         g       | j                  |j                  d   t        j                  d||j                  d         g       | j                  |j                  d	   j                  |       | j                  |j                  d
   j                  |       | j                  |j                  d   j                  |       | j                  |j                  d   j                  |	       | j                  |j                  d   j                  |       | j                  |j                  d   j                  |       | j                  |j                  d	   j                   |g       dD ]/  }| j#                  |j                  |   j                   g d|z         1 y )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refs                  E/home/dcms/DCMS/lib/python3.12/site-packages/pasta/base/scope_test.pytest_top_level_importsz ScopeTest.test_top_level_imports    s,   DL__  F 99VDIIEq"Jq"J1X^^A&Nq*qJq"J1XNq"Jq"JdA	 
 		""$ '

 	!//6z1775>B'91775>J9  	!//6z1775>B9  	!//6~qwwu~F9  	!//	:	> ! 4 4U ;	==  	!//	:	+= ! 4 4U ;	==  	!//>/A ! 4 4U ; A A% H	JA  	!//6z489  	!//	:	:qwwu~F=  	!//6~t<9  	!//	:	>4@=  	!//>z1775>JA  	!//>z1775>JA  	MM!''%.++Z8MM!''%.++Z8MM!''%.++^<MM!''%.++Z8MM!''%.++Z8MM!''%.++Z8!''%...1C0DE2 Q
qwws|))2/IC/OPQ    c                    t        j                  d      }t        j                  |      }|j                  }t        j                  |t        j                        \  }}}t        j                  |      }| j                  |j                  j                         h d       | j                  |j                  j                         h d       | j                  |j                  d   j                  |       | j                  |j                  d   j                  |       | j                  |j                  d   j                  |       | j!                  |j                  d   j                         | j!                  |j                  d   j                         dD ]/  }| j                  |j                  |   j"                  g d	|z         1 y )
Nzs        if a:
          import aaa
        elif b:
          import bbb
        else:
          import ccc
        >   abr   r   r   >   r   r   r   r   r   r   rE   rF   r    )r"   r#   r$   r%   r&   r   find_nodes_by_typealiasr   r(   r)   r'   r*   r+   r1   r/   assertIsNoner0   	r2   r3   r4   r5   node_aaanode_bbbnode_cccr?   r@   s	            rA   test_if_nested_importsz ScopeTest.test_if_nested_importsu   s]   __  F 99VDIIE#,#?#?cii#P HhdA!'',,.*IJ!//4468MNQWWU^..9QWWU^..9QWWU^..9aggcl--.aggcl--.$ 9
qwws|))21C799rC   c                    t        j                  d      }t        j                  |      }|j                  }t        j                  |t        j                        \  }}}t        j                  |      }| j                  |j                  j                         h d       | j                  |j                  j                         h d       | j                  |j                  d   j                  |       | j                  |j                  d   j                  |       | j                  |j                  d   j                  |       dD ]/  }| j                  |j                  |   j                   g d|z         1 y )Nzu        try:
          import aaa
        except:
          import bbb
        finally:
          import ccc
        >   r   r   r   r   r   r   r    )r"   r#   r$   r%   r&   r   rG   rH   r   r(   r)   r'   r*   r+   r1   r/   r0   rJ   s	            rA   test_try_nested_importsz!ScopeTest.test_try_nested_imports   s%   __  F 99VDIIE#,#?#?cii#P HhdA!'',,.*?@!//4468MNQWWU^..9QWWU^..9QWWU^..9$ 9
qwws|))21C799rC   c                    t        j                  d      }t        j                  |      }|j                  }t        j                  |t        j                        d   }t        j                  |      }| j                  |j                  j                         dh       | j                  |j                  j                         dh       y )Nz3        def foo(bar):
          import aaa
        r   foor   )r"   r#   r$   r%   r&   r   rG   rH   r   r(   r)   r'   r*   r+   r2   r3   r4   r5   rK   r?   s         rA   test_functiondef_nested_importsz)ScopeTest.test_functiondef_nested_imports   s    __  F 99VDIIE++D#))<Q?HdA!'',,.5'2!//446@rC   c                    t        j                  d      }t        j                  |      }|j                  }|d   j                  d   j
                  d   }t        j                  |      }| j                  |j
                  j                         dh       | j                  |j                  j                         dh       y )Nz2        class Foo():
          import aaa
        r   Foor   )r"   r#   r$   r%   r&   r'   r   r(   r)   r*   r+   rS   s         rA   test_classdef_nested_importsz&ScopeTest.test_classdef_nested_imports   s    __  F 99VDIIEQx}}Q%%a(HdA!'',,.5'2!//446@rC   c                    t        j                  d      }t        j                  |      }|j                  }|d   j
                  j                  j
                  }t        j                  |      }| j                  |j                  j                         dh       | j                  |j                  j                         h d       | j                  |j                  d   j                  |j
                  j
                  g       | j                  |j                  d   j                  d   j                  |j
                  g       | j                  |j                  d   j                  d   j                  d   j                  |g       y )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+   r0   r-   )r2   r3   r4   r5   node_refr?   s         rA   test_multilevel_import_readsz&ScopeTest.test_multilevel_import_reads   s'   __  F 99VDIIEQx~~""((HdA!'',,.5'2!//446;=!''%...1E1E0FG!''%...u5;;hnn=MN!''%...u5;;EBHH#*&rC   c                    t        j                  d      }t        j                  |      }|j                  }|d   j                  d   j
                  }|d   j                  d   j
                  }t        j                  |      }| j                  |j                  j                         ddh       | j                  |j                  j                         dh       | j                  |j                  d   j                  ||g       y )NzU        import aaa
        @aaa.x
        def foo(bar):
          return aaa
        r   r   r   rR   )r"   r#   r$   r%   r&   rZ   decorator_listr   r(   r)   r'   r*   r+   r0   )r2   r3   r4   r5   return_value	decoratorr?   s          rA    test_import_reads_in_functiondefz*ScopeTest.test_import_reads_in_functiondef   s    __  F 99VDIIE8==#))La''*00IdA!'',,.5%.9!//446@!''%...L0IJrC   c                 2   t        j                  d      }t        j                  |      }|j                  }|d   j
                  d   }|d   j                  d   j                  }|d   j                  d   j                  }t        j                  |      }| j                  |j
                  j                         ddh       | j                  |j                  j                         dh       | j                  |j
                  d   j                  ||g       y )NzU        import aaa
        @aaa.x
        class Foo(aaa.Bar):
          pass
        r   r   r   rV   )r"   r#   r$   r%   r&   r'   r_   rZ   basesr   r(   r)   r*   r+   r0   )r2   r3   r4   r5   rK   ra   baser?   s           rA   test_import_reads_in_classdefz'ScopeTest.test_import_reads_in_classdef   s    __  F 99VDIIEQx~~a Ha''*00I8>>!""DdA!'',,.5%.9!//446@!''%...D0ABrC   c                    t        j                  d      }t        j                  |      }|j                  }|d   j
                  j                  d   }t        j                  |      }| j                  |j                  j                         ddh       | j                  |j                  j                         dh       | j                  |j                  d   j                  |g       y )NzJ        import aaa
        def foo(aaa=aaa):
          return aaa
        r   r   r   rR   )r"   r#   r$   r%   r&   argsdefaultsr   r(   r)   r'   r*   r+   r0   )r2   r3   r4   r5   argvalr?   s         rA   "test_import_masked_by_function_argz,ScopeTest.test_import_masked_by_function_arg  s    __  F
 99VDIIE1X]]##A&FdA!'',,.5%.9!//446@!''%...9rC   c                    t        j                  d      }t        j                  |      }|j                  }|d   j
                  }t        j                  |      }| j                  |j                  j                         ddh       | j                  |j                  j                         dh       | j                  |j                  d   j                  |g       y )Nzc        import aaa
        def foo():
          aaa = 123
          return aaa
        aaa
        r   r   rR   )r"   r#   r$   r%   r&   rZ   r   r(   r)   r'   r*   r+   r0   rS   s         rA   test_import_masked_by_assignz&ScopeTest.test_import_masked_by_assign  s    __  F 99VDIIEQx~~HdA!'',,.5%.9!//446@!''%...
;rC   c                    t        j                  d      }t        j                  |      }|j                  }|d   j
                  d   j                  }t        j                  |      }| j                  |j                  j                         ddh       | j                  |j                  j                         dh       | j                  |j                  d   j                  |g       y )NzW        import aaa
        @aaa.wrapper
        def foo(aaa=1):
          pass
        r   r   r   rR   )r"   r#   r$   r%   r&   r_   rZ   r   r(   r)   r'   r*   r+   r0   )r2   r3   r4   r5   ra   r?   s         rA   test_import_in_decortatorz#ScopeTest.test_import_in_decortator+  s    __  F 99VDIIEa''*00IdA!'',,.5%.9!//446@!''%...<rC   type_annotationsc                    t        j                  d      }t        j                  |      }|j                  }|d   }t        j                  |      }| j                  |j                  j                         ddh       | j                  |j                  j                         dh       | j                  |j                  d   j                  |j                  j                  g       y )NzH        import aaa
        def foo() -> aaa.Foo:
          pass
        r   r   rR   )r"   r#   r$   r%   r&   r   r(   r)   r'   r*   r+   r0   returnsrZ   r2   r3   r4   r5   r[   r?   s         rA   test_import_in_return_typez$ScopeTest.test_import_in_return_type=  s    __  F
 99VDIIE8DdA!'',,.5%.9!//446@!''%...1C1C0DErC   c                    t        j                  d      }t        j                  |      }|j                  }|d   }t        j                  |      }| j                  |j                  j                         ddh       | j                  |j                  j                         dh       | j                  |j                  d   j                  |j                  j                  d   j                  j                  g       y )NzI        import aaa
        def foo(bar: aaa.Bar):
          pass
        r   r   rR   r   )r"   r#   r$   r%   r&   r   r(   r)   r'   r*   r+   r0   rh   
annotationrZ   rs   s         rA   test_import_in_argument_typez&ScopeTest.test_import_in_argument_typeO  s    __  F
 99VDIIE8DdA!'',,.5%.9!//446@!''%...99>>!,77==>@rC   c                    t        j                  d      }t        j                  |      }|j                  }|d   j
                  j                  j
                  }|d   j
                  j                  j
                  }|d   j
                  j                  j
                  }t        j                  |      }| j                  |j                  j                         ddh       | j                  |j                  j                         h d       | j                  |j                  d   j                  ||j
                  |j
                  j
                  g       | j                  |j                  d   j                  d   j                  ||j
                  g       | j                  |j                  d   j                  d   j                  d	   j                  |g       y )
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   rY   )r2   r3   r4   r5   call1call2call3r?   s           rA    test_import_attribute_referencesz*ScopeTest.test_import_attribute_referencesb  sm   __  F 99VDIIE!HNN%%E!HNN%%E!HNN%%EdA!'',,.5%.9!//446MO!''%... %++u{{/@/@AC!''%...u5;; %++.0!''%...u5;;EBHH '#rC   c                 v   t        j                  d      }t        j                  |      }|j                  \  }}|j                  \  }}t        j                  |      }|j                  |      }| j                  |j                  |       | j                  ||       | j                  |j                  ddg       |j                  |      }	| j                  |	j                  |       | j                  |	j                  |       | j                  |	j                  g d       |j                  |      }
| j                  |
j                  |       | j                  |
j                  |	       | j                  |
j                  dg       |j                  |      }| j                  |j                  |       | j                  ||	       | j                  |j                  g d       | j                  |
j                  |      |	       | j                  |j                  t        j                  d                   y )Nzn        import a
        def b(c, d, e=1):
          class F(d):
            g = 1
          return c
        rE   rF   )cdeFgrR   )id)r"   r#   r$   r%   r&   r   r(   lookup_scoper.   noder)   r'   parent_scoperI   Name)r2   srctimport_node	func_node
class_nodereturn_nodescimport_node_scopefunc_node_scopeclass_node_scopereturn_node_scopes               rA   test_lookup_scopezScopeTest.test_lookup_scope|  s   
//  C 			#AVVK'nnJ	q	B4MM#((!,MM#R(+11C:>ooi0OMM/&&	2MM/..3///1EFz2MM"''4MM"//A*003%84MM#(()4MM#_5+113GHMM"//	:!# 	boochh%&89:rC   c                    t        j                  d      }t        j                  |      }|j                  \  }}|j                  \  }}t        j                  |      }| j                  |j                  j                         ddh       | j                  |j                  j                         dh       | j                  |j                  d   j                  |j                  d   j                  |j                  d   j                  g       y )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+   r0   rZ   )r2   r3   r4   
importstmtclassdef
method_aaa
method_bbbr?   s           rA   test_class_methodszScopeTest.test_class_methods  s    __  F 99VD99J%]]J
dA!'',,.5#,7!//446@!''%...%??1-33Z__Q5G5M5MNPrC   c                     t        j                  d      }t        j                  |      }|j                  \  }}|j                  \  }}t        j                  |      }|j                  |      }| j                  d|j                         | j                  |j                  d   j                  |j                  g       | j                  d|j                         | j                  |j                  d   j                  |j                  g       y )Nz]        def aaa(bbb, *ccc, **ddd):
          ccc
          ddd
        eee(ccc, ddd)
        r   r   )r"   r#   r$   r%   r&   r   r(   r   assertInr'   r)   r0   rZ   )	r2   r3   r4   funcdefcallccc_exprddd_exprr   
func_scopes	            rA   -test_vararg_kwarg_references_in_function_bodyz7ScopeTest.test_vararg_kwarg_references_in_function_body  s    __  F 99VDIIMGT Hh	t	B)JMM%))****5177(..9IJMM%))****5177(..9IJrC   N)__name__
__module____qualname__rB   rN   rP   rT   rW   r]   rb   rf   rk   rm   ro   r   requires_featuresrt   rw   r}   r   r   r    rC   rA   r
   r
      s    SQj9<96AA&(K&C(:"<&=$  : 23F 4F"  : 23@ 4@$#4$;LP,KrC   r
   c                  ~    t        j                         } | j                  t        j                  t                     | S )N)unittest	TestSuiteaddTests	makeSuiter
   )results    rA   suiter     s,    &//($$Y/0	-rC   __main__)__doc__
__future__r   r   r   r$   r"   r   
pasta.baser   r   r   TestCaser
   r   r   mainr   rC   rA   <module>r      s[     '  % 
      !kK
## kK\ z(--/ rC   