o
    DDiN                  	   @   sH  d dl 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mZ d dl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 d d
lmZ d dlmZmZ G dd dejZ de!deej"e	ejee f f fddZ#de!dee fddZ$de!de!dee fddZ%G dd deZ&G dd deZ'G dd deZ(dS )    )Path)TemporaryDirectory)dedent)
CollectionDictMappingOptionalSetTupleN)ensure_type)CSTNode)FullyQualifiedNameProviderMetadataWrapperQualifiedNameQualifiedNameProviderQualifiedNameSource)FullRepoManager)FullyQualifiedNameVisitor)data_providerUnitTestc                   @   s0   e Zd ZefZdddZdejdefddZ	dS )	QNameVisitorreturnNc                 C   s
   i | _ d S N)qnames)self r   a/var/www/Datamplify/venv/lib/python3.10/site-packages/libcst/metadata/tests/test_name_provider.py__init__      
zQNameVisitor.__init__nodec                 C   s   |  t|}|| j|< dS )NT)get_metadatar   r   )r   r   qnamer   r   r   on_visit    s   
zQNameVisitor.on_visitr   N)
__name__
__module____qualname__r   METADATA_DEPENDENCIESr   cstr   boolr"   r   r   r   r   r      s    
r   
module_strr   c                 C   s.   t tt| }t }|| |j|jfS r   )r   r(   parse_moduler   r   visitmoduler   )r*   wrappervisitorr   r   r   $get_qualified_name_metadata_provider&   s   
r0   c                 C   s   t | \}}dd | D S )Nc                 S      h | ]	}|D ]}|qqS r   r   .0r   r!   r   r   r   	<setcomp>1   s    z&get_qualified_names.<locals>.<setcomp>)r0   values)r*   _
qnames_mapr   r   r   get_qualified_names/   s   r8   	file_pathc                 C   sN   t jt t|ttjtd| gd d| did}dd |t	 D S )N )timeout)cachec                 S   r1   r   r   r2   r   r   r   r4   =   s    z,get_fully_qualified_names.<locals>.<setcomp>)
r(   r   r+   r   r   	gen_cacher   getresolver5   )r9   r*   r.   r   r   r   get_fully_qualified_names4   s   r@   c                   @   s   e Zd ZdddZdddZdddZdd	d
ZdddZdddZdddZ	dddZ
dddZdddZdddZdddZdS )QualifiedNameProviderTestr   Nc                 C   sD   t d}| dhdd |D  |D ]}| j|jtj| d qd S )Nz>
            from a.b import c as d
            d
            a.b.cc                 S      h | ]}|j qS r   namer3   r!   r   r   r   r4   L       z9QualifiedNameProviderTest.test_imports.<locals>.<setcomp>msg)r8   assertEqualsourcer   IMPORTr   r   r!   r   r   r   test_importsE   s   z&QualifiedNameProviderTest.test_importsc                 C   F   t d}| ddhdd |D  |D ]}| j|jtj| d qd S )Nz#
            int(None)
            builtins.intbuiltins.Nonec                 S   rC   r   rD   rF   r   r   r   r4   W   rG   z:QualifiedNameProviderTest.test_builtins.<locals>.<setcomp>rH   )r8   rJ   rK   r   BUILTINrM   r   r   r   test_builtinsP   s   z'QualifiedNameProviderTest.test_builtinsc                 C   rO   )Nz9
            class X:
                a: "X"
            XzX.ac                 S   rC   r   rD   rF   r   r   r   r4   c   rG   z8QualifiedNameProviderTest.test_locals.<locals>.<setcomp>rH   )r8   rJ   rK   r   LOCALrM   r   r   r   test_locals\   s   z%QualifiedNameProviderTest.test_localsc           	      C   sx  t d\}}t|jd tj}t|jjd tj}| |t|jtjj	 t
dtjh tt|jjd tjjd tjj}| || t
dtjh | || t
dtjh tt|jd tjjd tjj}| || t
dtjh tt|jjd tjjd tjjd j}| || t
dtjh tt|jjd tjjd tjjd j}| || t
dtjh d S )	Nz
            from a.b import c
            class Cls:
                def f(self) -> "c":
                    c()
                    d = {}
                    d['key'] = 0
            def g():
                pass
            g()
               r   rB      gzCls.f.<locals>.d   )r0   r   bodyr(   ClassDefFunctionDefrJ   returns
Annotation
annotationr   r   rL   SimpleStatementLineExprvaluerU   Assigntargetstarget)	r   mnamesclsfc_callg_calld_named_subscriptr   r   r   test_simple_qualified_namesg   s`   z5QualifiedNameProviderTest.test_simple_qualified_namesc                 C   s  t d\}}t|jd tj}| || tdtjh t|jjd tj	}| || tdtjh tt|jjd tj
jd tjj}| || tdtjh t|jjd tj	}| || tdtjh tt|jjd tj
jd tjj}| || tdtjh t|jd tj	}| || td	tjh t|jjd tj	}	| ||	 td
tjh |	jjd }
| ||
 tdtjh d S )Na  
            class A:
                def f1(self):
                    def f2():
                        pass
                    f2()

                def f3(self):
                    class B():
                        ...
                    B()
            def f4():
                def f5():
                    class C:
                        pass
                    C()
                f5()
            r   AzA.f1rW   zA.f1.<locals>.f2zA.f3zA.f3.<locals>.Bf4zf4.<locals>.f5zf4.<locals>.f5.<locals>.C)r0   r   r[   r(   r\   rJ   r   r   rU   r]   ra   rb   rc   )r   rg   rh   cls_afunc_f1func_f2_callfunc_f3call_bfunc_f4func_f5cls_cr   r   r   test_nested_qualified_names   sV   z5QualifiedNameProviderTest.test_nested_qualified_namesc                 C   sZ   t d\}}tt|jd tjjd tjj}| || tdt	j
dtdt	j
dh d S )Nz
            if 1:
                from a import b as c
            elif 2:
                from d import e as c
            c()
            rW   r   a.brE   rK   zd.e)r0   r   r[   r(   ra   rb   rc   rJ   r   r   rL   )r   rg   rh   callr   r   r   test_multiple_assignments   s   	z3QualifiedNameProviderTest.test_multiple_assignmentsc                 C   s   t d\}}t|jd tj}t|jjd tj}ttt|jjd tjjd tjjtj	}|j
j}| || tdtjdh t|j
jtj	}|j
j}| || tdtjdh t|jtj	}	|	j
j}
| ||
 tdtjdh d S )NaO  
            class C:
                def fn(self) -> None:
                    [[k for k in i] for i in [j for j in range(10)]]
                    # Note:
                    # The qualified name of i is straightforward to be "C.fn.<locals>.<comprehension>.i".
                    # ListComp j is evaluated outside of the ListComp i.
                    # so j has qualified name "C.fn.<locals>.<comprehension>.j".
                    # ListComp k is evaluated inside ListComp i.
                    # so k has qualified name "C.fn.<locals>.<comprehension>.<comprehension>.k".
            r   zC.fn.<locals>.<comprehension>.ir|   zC.fn.<locals>.<comprehension>.jz/C.fn.<locals>.<comprehension>.<comprehension>.k)r0   r   r[   r(   r\   r]   ra   rb   rc   ListCompfor_inrf   rJ   r   r   rU   iterelt)r   rg   rh   cls_deffn_def
outer_compiinner_comp_jjinner_comp_kkr   r   r   test_comprehension   sT   		z,QualifiedNameProviderTest.test_comprehensionc                 C   s.   G dd dt j}tt d||  d S )Nc                   @   s<   e Zd ZefZdeddfddZdejde	e
 fddZdS )	zCQualifiedNameProviderTest.test_has_name_helper.<locals>.TestVisitortestr   Nc                 S   s
   || _ d S r   )r   )r   r   r   r   r   r   -  r   zLQualifiedNameProviderTest.test_has_name_helper.<locals>.TestVisitor.__init__r   c              	   S   sl   | j t| |d | j t| |d | j t| |tdtj | j t| |tdtj d S )NrB   r{   )	r   
assertTruer   has_nameassertFalser   r   rL   rU   )r   r   r   r   r   
visit_Call0  s   zNQualifiedNameProviderTest.test_has_name_helper.<locals>.TestVisitor.visit_Call)r$   r%   r&   r   r'   r   r   r(   Callr   r)   r   r   r   r   r   TestVisitor*  s    r   zimport a;a.b.c())r(   
CSTVisitorr   r+   r,   )r   r   r   r   r   test_has_name_helper)  s   z.QualifiedNameProviderTest.test_has_name_helperc                 C   sn   t d\}}ttt|jd tjjd tjjtj}| || t	dt
jdh |j}| || t  d S )Nz=
            obj = object()
            obj.eval
            rW   r   zobj.evalr|   )r0   r   r[   r(   ra   rb   rc   	AttributerJ   r   r   rU   attrset)r   rg   rh   r   evalr   r   r   test_name_in_attributeB  s    z0QualifiedNameProviderTest.test_name_in_attributec                 C   s   t d\}}t|jd tj}ttt|jtjjd tjjd tj}t|jtj	}t|jtj
}| || tdtjh d S )NzW
            import a
            class Foo:
                bar: a.aa.aaa
            rW   r   za.aa.aaa)r0   r   r[   r(   r\   IndentedBlockra   	AnnAssignr`   r_   r   rJ   r   r   rL   )r   rg   rh   foobarr`   	attributer   r   r   &test_repeated_values_in_qualified_nameV  s$   z@QualifiedNameProviderTest.test_repeated_values_in_qualified_namec              	   C   s  t d\}}t|jd tj}t|jjd tj}ttt|jtjjjd tjjd tjj	}| 
t|tj |d j}tttt|jtjjtjjjd tjjd tjj	}| 
t|tj |d j}ttt|jd tjjd tjj	d jtjj}	ttt|jd tjjd tjjtj}
| || tdtjh | || t  | || t  | ||	 t  | ||
 tdtjtdtjtdtjtdtjh d S )Nz
            if False:
                def f(): pass
            elif False:
                from b import f
            else:
                import f
            import a.b as f

            f()
            r   rW   rZ   rj   zb.fr{   )r0   r   r[   r(   Ifr]   orelsera   
ImportFromrh   r   
isinstance
ImportStarrE   ElseImportasnameAsNamerb   rc   r   rJ   r   r   rU   r   rL   )r   rg   rh   if_first_fsecond_f_aliassecond_fthird_f_aliasthird_ffourth_fr}   r   r   r   test_multiple_qualified_namesn  s   
	




z7QualifiedNameProviderTest.test_multiple_qualified_namesc                    s   t d\} dtjdtt dd f fdd}||jd tdtjh t|jd	 tj	}||jjd
 tdtjh t|jd tj
}||jjd
 tdtjh d S )Nz
                from lib import a,b,c
                a = a
                class Test:
                    b = b
                def func():
                    c = c
            r   r   r   c                    s0   t t | tjjd tjj} | | d S )Nr   )r   r(   ra   r[   rd   rc   rJ   )r   r   rE   rh   r   r   r   	test_name  s   zFQualifiedNameProviderTest.test_shadowed_assignments.<locals>.test_namerW   zlib.arZ   r   zlib.brX   zlib.c)r0   r(   r   r	   r   r[   r   rL   r   r\   r]   )r   rg   r   ri   funcr   r   r   test_shadowed_assignments  s   "z3QualifiedNameProviderTest.test_shadowed_assignmentsr#   )r$   r%   r&   rN   rS   rV   ro   rz   r~   r   r   r   r   r   r   r   r   r   r   rA   D   s    




:
;

8


CrA   c                   @   s   e Zd Zedddejifdddejifdddejifdddejifd	ddejifd
dejejejdfddejejejejejdfddejejejdfddejejejdff	dedede	eef ddfddZ
dddZdS )FullyQualifiedNameProviderTestza/b/c.pyr:   rB   za/b.pyr{   za.pyaza/b/__init__.pyza/b/__main__.pyztest/module.pyz	int(None))ztest.modulerP   rQ   zsome/test/module.pyz
                from a.b import c as d
                from . import rel
                from .lol import rel2
                from .. import thing as rel3
                d, rel, rel2, rel3
                )some.test.modulerB   zsome.test.relzsome.test.lol.rel2z
some.thingzsome/test/module/__init__.pyzs
                from . import rel
                from .lol import rel2
                rel, rel2
                )r   zsome.test.module.relzsome.test.module.lol.rel2zC
                class X:
                    a: X
                )r   zsome.test.module.Xzsome.test.module.X.afilecoderh   r   Nc                 C   sP   t ||}| t| dd |D  |D ]}| j|j||j | d qd S )Nc                 S   rC   r   rD   rF   r   r   r   r4     rG   z=FullyQualifiedNameProviderTest.test_qnames.<locals>.<setcomp>rH   )r@   assertSetEqualr   keysrJ   rK   rE   )r   r   r   rh   r   r!   r   r   r   test_qnames  s   
D
z*FullyQualifiedNameProviderTest.test_qnamesc              	   C   s\   d}d}dD ]%\}}| j |d | t|||| W d    n1 s&w   Y  qd S )Nr   z	some.test))z.foozsome.test.foo)z..barzsome.bar)r   zsome.test.module.foorD   )subTestrJ   r   _fully_qualify_local)r   module_namepackage_namerE   expectedr   r   r   test_local_qualification  s   z7FullyQualifiedNameProviderTest.test_local_qualificationr#   )r$   r%   r&   r   r   rU   rR   rL   strr   r   r   r   r   r   r   r     sb    A
r   c                   @   s   e Zd ZdddZdS )!FullyQualifiedNameIntegrationTestr   Nc           
      C   s   t  M}t|}|d }|j  |  | }t||gtg}||}|	t}t
t| \}}	| |tj | |	tdtjdh W d    d S 1 sSw   Y  d S )Nz
pkg/mod.pyzpkg.modr|   )r   r   parentmkdirtouchas_posixr   r   get_metadata_wrapper_for_pathr?   nextr   itemsassertIsInstancer(   ModulerJ   r   r   rU   )
r   dirrootr9   file_path_strmgrr.   fqnamesmodrh   r   r   r   test_with_full_repo_manager0  s   


"z=FullyQualifiedNameIntegrationTest.test_with_full_repo_managerr#   )r$   r%   r&   r   r   r   r   r   r   /  s    r   ))pathlibr   tempfiler   textwrapr   typingr   r   r   r   r	   r
   libcstr(   r   libcst._nodes.baser   libcst.metadatar   r   r   r   r   !libcst.metadata.full_repo_managerr   libcst.metadata.name_providerr   libcst.testing.utilsr   r   r   r   r   r   r0   r8   r@   rA   r   r   r   r   r   r   <module>   s2    
	   ^