B
    '(b                 @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZmZmZmZ d dlmZmZmZ d dlmZmZ d dlmZ d dlmZmZmZmZmZmZmZ d dl m!Z!m"Z"m#Z# d d	l$m%Z%m&Z&m'Z'm(Z(m)Z) d d
l*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 d dl5m6Z6m7Z7 G dd dej8Z9G dd dej8Z:G dd dej8Z;G dd dej8Z<G dd deZ=e2ddZ>G dd dZ?G dd de?Z@G dd dej8ZAG dd dej8ZBG d d! d!ej8ZCG d"d# d#ej8ZDeEeEeEd$d%d&ZFdS )'    N)
ModuleType)AnyListTupleOptional)assert_equalassert_string_arrays_equallocal_sys_path_set)	DataSuiteDataDrivenTestCase)CompileError)generate_stubsparse_optionsOptionscollect_build_targetsmypy_optionsis_blacklisted_pathis_non_library_module)walk_packagesremove_misplaced_type_commentscommon_dir_prefix)generate_c_type_stubinfer_method_siggenerate_c_function_stubgenerate_c_property_stubis_c_property_readonly)
parse_signatureparse_all_signaturesbuild_signaturefind_unique_signaturesinfer_sig_from_docstringinfer_prop_type_from_docstringFunctionSigArgSig!infer_arg_sig_from_anon_docstringis_valid_type)ModuleInspectInspectErrorc                   s   e Zd ZdZeejdkdddddZeejdkddddd	Z	eejdkdddd
dZ
ddeeddddZdee ee d fddZ  ZS )StubgenCmdLineSuitezATest cases for processing command-line options and finding files.win32zclean up fails on WindowsN)returnc          
   C   s   t  }t }zt | t d | dd | dd t t jdd | ddd t	dg}t
|t|\}}t|g  dd |D }t|t jdddt jddt jddh W d t | X W d Q R X d S )Nsubdirza.pyzb.pypackz__init__.pyc             S   s   h | ]
}|j qS  )path).0modr-   r-   D/home/dcms/DCMS/lib/python3.7/site-packages/mypy/test/teststubgen.py	<setcomp>3   s    z7StubgenCmdLineSuite.test_files_found.<locals>.<setcomp>)osgetcwdtempfileTemporaryDirectorychdirmkdir	make_filer.   joinr   r   r   r   )selfcurrenttmpoptspy_modsc_modsfilesr-   r-   r1   test_files_found%   s"    




z$StubgenCmdLineSuite.test_files_foundc          
   C   s   t  }t }zt | t d | jdddd | dd | dd tddg}t|t	|\}}t
|g  dd	 |D }t
|t jddt jddt jddh W d t | X W d Q R X d S )
Nr,   z__init__.pyzfrom . import a, b)contentza.pyzb.pyz-pc             S   s   h | ]}t j|jpd qS )FAIL)r3   r.   relpath)r/   r0   r-   r-   r1   r2   G   s    z:StubgenCmdLineSuite.test_packages_found.<locals>.<setcomp>)r3   r4   r5   r6   r7   r8   r9   r   r   r   r   r.   r:   )r;   r<   r=   r>   r?   r@   rA   r-   r-   r1   test_packages_found:   s     



z'StubgenCmdLineSuite.test_packages_foundc          
   C   s   t  }t }|t_t h}zLt | | j	|ddd t
ddg}t|t|\}}| dksjtW d tjt_t | X W d Q R X d S )Nzmymodule.pyzimport a)rC   z-mZmymodule )r3   r4   ioStringIOsysstdoutr5   r6   r7   r9   r   r   r   getvalueAssertionError
__stdout__)r;   r<   captured_outputr=   r>   r?   r@   r-   r-   r1   test_module_not_foundN   s    

z)StubgenCmdLineSuite.test_module_not_foundrG   )rC   )r.   rC   r*   c         	   G   s0   t jj| }t|d}|| W d Q R X d S )Nw)r3   r.   r:   openwrite)r;   rC   r.   filefr-   r-   r1   r9   ^   s    zStubgenCmdLineSuite.make_file)resultr*   c          	      s   t   t |S Q R X d S )N)r	   superrun)r;   rV   )	__class__r-   r1   rX   c   s    zStubgenCmdLineSuite.run)N)__name__
__module____qualname____doc__unittestskipIfrJ   platformrB   rF   rP   strr9   r   r   rX   __classcell__r-   r-   )rY   r1   r(   "   s   r(   c               @   s   e Zd ZddddZdS )StubgenCliParseSuiteN)r*   c          
   C   st   t  d}ttt|dgdh ttt|ddgddh tt|dg}| |dddddh W d Q R X d S )Nzmypy.errorszmypy.stubgenZmypyz	mypy.testzmypy.test.helpers)r&   r   setr   
assertTrue
issuperset)r;   mZall_mypy_packagesr-   r-   r1   test_walk_packagesi   s    
z'StubgenCliParseSuite.test_walk_packages)rZ   r[   r\   rh   r-   r-   r-   r1   rc   h   s   rc   c               @   s  e Zd ZdZddddZddddZdddd	Zddd
dZddddZddddZ	ddddZ
ddddZeeeee ee f ddddZddddZddddZddddZddddZdddd Zddd!d"Zddd#d$Zddd%d&Zddd'd(Zddd)d*Zddd+d,Zddd-d.Zddd/d0Zddd1d2Zddd3d4Ze e!j"d5kd6ddd7d8Z#e e!j"d5kd9ddd:d;Z$dS )<StubgenUtilSuitez)Unit tests for stubgen utility functions.N)r*   c             C   s   |  ddg g f d S )Nzfunc()func)assert_parse_signature)r;   r-   r-   r1   test_parse_signature   s    z%StubgenUtilSuite.test_parse_signaturec             C   s.   |  dddgg f |  ddddgg f d S )Nz	func(arg)rj   argzdo(arg, arg2)Zdoarg2)rk   )r;   r-   r-   r1   test_parse_signature_with_args   s    z/StubgenUtilSuite.test_parse_signature_with_argsc             C   sD   |  ddg dgf |  dddgdgf |  ddg ddgf d S )Nzfunc([arg])rj   rm   zfunc(arg[, arg2])rn   zfunc([arg[, arg2]]))rk   )r;   r-   r-   r1   'test_parse_signature_with_optional_args   s    z8StubgenUtilSuite.test_parse_signature_with_optional_argsc             C   sD   |  ddg dgf |  dddgdgf |  ddg ddgf d S )Nzfunc(arg=None)rj   rm   zfunc(arg, arg2=None)rn   zfunc(arg=1, arg2=""))rk   )r;   r-   r-   r1   %test_parse_signature_with_default_arg   s    z6StubgenUtilSuite.test_parse_signature_with_default_argc             C   s   |  dddgg f d S )NzClassName.func(arg)rj   rm   )rk   )r;   r-   r-   r1   ,test_parse_signature_with_qualified_function   s    z=StubgenUtilSuite.test_parse_signature_with_qualified_functionc             C   s   |  ddddgdgf d S )NzClassName.func(arg, *, arg2=1)rj   rm   *rn   )rk   )r;   r-   r-   r1   %test_parse_signature_with_kw_only_arg   s    z6StubgenUtilSuite.test_parse_signature_with_kw_only_argc             C   s   |  ddddgg f d S )NzClassName.func(arg, *args)rj   rm   z*args)rk   )r;   r-   r-   r1   "test_parse_signature_with_star_arg   s    z3StubgenUtilSuite.test_parse_signature_with_star_argc             C   s   |  ddddgg f d S )NzClassName.func(arg, **args)rj   rm   z**args)rk   )r;   r-   r-   r1   'test_parse_signature_with_star_star_arg   s    z8StubgenUtilSuite.test_parse_signature_with_star_star_arg)sigrV   r*   c             C   s   t t|| d S )N)r   r   )r;   rw   rV   r-   r-   r1   rk      s    z'StubgenUtilSuite.assert_parse_signaturec             C   sd   t tg g d t tdgg d t tddgg d t tdgdgd t tdgddgd d S )	Nz()rm   z(arg)rn   z(arg, arg2)z(arg, arg2=...)z**xz(arg, arg2=..., **x))r   r   )r;   r-   r-   r1   test_build_signature   s
    z%StubgenUtilSuite.test_build_signaturec             C   s"   t tddddgddgg f d S )Nzrandom textz.. function:: fn(argz.. function:: fn()z  .. method:: fn2(arg))fnz())Zfn2z(arg))r   r   )r;   r-   r-   r1   test_parse_all_signatures   s    z*StubgenUtilSuite.test_parse_all_signaturesc             C   s    t tdddddgddg d S )N)rj   z())func2z())r{   z(arg))Zfunc3z(arg, arg2))r   r   )r;   r-   r-   r1   test_find_unique_signatures   s    z,StubgenUtilSuite.test_find_unique_signaturesc          	   C   s8  t tddtdtddgddg t tddtdtddgddg t tddtdtddtd	d
dgddg t tddtdtddtd	d
dgddg t tddtdtddtd	d
dgddg t tddg  t tddg  t tddtdtdd
dgddg t tddg  t tddtdtdddgddg t tddtdtddd
dgddg t tddtdtdd d
dgddg t tddtdg ddg t tddtdtddd
dgddg t tddtdtddd
dgddg t tddtdtdddgddg t tddtdtdddtd ddgddg t td!dtdtdd"dgddg t td#dtdtdd$d
dgddg t td%dtdtddd
dgddg t td&dg  t td'dtdtdd dgddg t td(dtdtddd
dgddg d S ))Nz
func(x) - yrj   x)namer   )r~   argsret_typez
func(x)z
func(x, Y_a=None)ZY_aT)r~   defaultz
func(x, Y_a=3)z
func(x, Y_a=[1, 2, 3])z
afunc(x) - yz

func(x, yz
func(x=z(y))z
func xz
func(x: int)int)r~   typez
func(x: int=3))r~   r   r   z

func(x=3)z
func() -> intz
func(x: int=3) -> intz
func(x: int=3) -> int   
z 
func(x: Tuple[int, str]) -> strzTuple[int,str]ra   z9
func(x: Tuple[int, Tuple[str, int], str], y: int) -> strzTuple[int,Tuple[str,int],str]yz
func(x: foo.bar)zfoo.barz
func(x: list=[1,2,[3,4]])listz
func(x: str="nasty[")z
func[(x: foo.bar, invalid]z&
func(x: invalid::type<with_template>)z
func(x: str=""))r   r    r"   r#   )r;   r-   r-   r1   test_infer_sig_from_docstring   s    


















z.StubgenUtilSuite.test_infer_sig_from_docstringc             C   s0   t tddtdtddtddgddg d S )Nz$
func(x, x) -> str
func(x, y) -> intrj   r}   )r~   r   r   )r~   r   r   )r   r    r"   r#   )r;   r-   r-   r1   ,test_infer_sig_from_docstring_duplicate_args  s    
z=StubgenUtilSuite.test_infer_sig_from_docstring_duplicate_argsc             C   s   t tddd  d S )Nz:
            x
              x
             x
            rj   )r   r    )r;   r-   r-   r1   -test_infer_sig_from_docstring_bad_indentation  s    z>StubgenUtilSuite.test_infer_sig_from_docstring_bad_indentationc             C   sH   t tdtddtddg t tdtdddd	td
ddd	g d S )Nz(*args, **kwargs)z*args)r~   z**kwargszB(x: Tuple[int, Tuple[str, int], str]=(1, ('a', 2), 'y'), y: int=4)r}   zTuple[int,Tuple[str,int],str]T)r~   r   r   r   r   )r   r$   r#   )r;   r-   r-   r1   &test_infer_arg_sig_from_anon_docstring  s    z7StubgenUtilSuite.test_infer_arg_sig_from_anon_docstringc             C   s<   t tdd t tdd t tdd t tdd  d S )Nzstr: A string.ra   zOptional[int]: An int.zOptional[int]zTuple[int, int]: A tuple.zTuple[int, int]z
str: A string.)r   r!   )r;   r-   r-   r1   #test_infer_prop_type_from_docstring'  s
    z4StubgenUtilSuite.test_infer_prop_type_from_docstringc             C   s   t ddg kstd S )Nz*fetch_row([maxrows, how]) -- Fetches stuffZ	fetch_row)r    rM   )r;   r-   r-   r1   -test_infer_sig_from_docstring_square_brackets.  s    z>StubgenUtilSuite.test_infer_sig_from_docstring_square_bracketsc             C   s   d}t t|| d S )Nu   
        ሴ
        def f(x):  # type: (int) -> int

        def g(x):
            # type: (int) -> int

        def h():

            # type: () int

        x = 1  # type: int
        )r   r   )r;   Zgoodr-   r-   r1   %test_remove_misplaced_type_comments_14  s    z6StubgenUtilSuite.test_remove_misplaced_type_comments_1c             C   s   d}d}t t|| d S )Nz
        def f(x):
            # type: Callable[[int], int]
            pass

        #  type:  "foo"
        #  type:  'bar'
        x = 1
        # type: int
        z?
        def f(x):

            pass



        x = 1

        )r   r   )r;   bad	bad_fixedr-   r-   r1   %test_remove_misplaced_type_comments_2E  s    

z6StubgenUtilSuite.test_remove_misplaced_type_comments_2c             C   s   d}d}t t|| d S )Nz
        def f(x):
            """docstring"""
            # type: (int) -> int
            pass

        def g(x):
            """docstring
            """
            # type: (int) -> int
            pass
        z
        def f(x):
            """docstring"""

            pass

        def g(x):
            """docstring
            """

            pass
        )r   r   )r;   r   r   r-   r-   r1   %test_remove_misplaced_type_comments_3\  s    z6StubgenUtilSuite.test_remove_misplaced_type_comments_3c             C   s   d}d}t t|| d S )Nz
        def f(x):
            '''docstring'''
            # type: (int) -> int
            pass

        def g(x):
            '''docstring
            '''
            # type: (int) -> int
            pass
        z
        def f(x):
            '''docstring'''

            pass

        def g(x):
            '''docstring
            '''

            pass
        )r   r   )r;   r   r   r-   r-   r1   %test_remove_misplaced_type_comments_4w  s    z6StubgenUtilSuite.test_remove_misplaced_type_comments_4c             C   s   d}d}t t|| d S )Nz
        def f(x):
            # type: (int, List[Any],
            #        float, bool) -> int
            pass

        def g(x):
            # type: (int, List[Any])
            pass
        z{
        def f(x):

            #        float, bool) -> int
            pass

        def g(x):

            pass
        )r   r   )r;   r   r   r-   r-   r1   %test_remove_misplaced_type_comments_5  s    

z6StubgenUtilSuite.test_remove_misplaced_type_comments_5c             C   s   d}d}t t|| d S )Ns   
        
        def f(x):  # type: (int) -> int

        def g(x):
            # type: (int) -> int
            pass

        def h():
            # type: int
            pass

        x = 1  # type: int
        s   
        
        def f(x):  # type: (int) -> int

        def g(x):
            # type: (int) -> int
            pass

        def h():

            pass

        x = 1  # type: int
        )r   r   )r;   originaldestr-   r-   r1   )test_remove_misplaced_type_comments_bytes  s    z:StubgenUtilSuite.test_remove_misplaced_type_comments_bytesr)   z0Tests building the paths common ancestor on *nixc             C   s   t g dkstt dgdks"tt dgdks4tt dgdksFtt ddgdksZtt ddgdksntt d	dgdkstt d
dgdkstt d	dgdkstt d
dgdkstt ddgdkstt dgdkstt dgdkstd S )N.zx.pyiz./x.pyizfoo/bar/x.pyizfoo/barzfoo/bar/y.pyiz	foo/y.pyifooz	foo/x.pyizfoo/bar/zar/x.pyizfoo/bar/zar/y.pyizfoo/bar\x.pyizfoo\bar/x.pyizfoo\bar)r   rM   )r;   r-   r-   r1   test_common_dir_prefix_unix  s    z,StubgenUtilSuite.test_common_dir_prefix_unixz3Tests building the paths common ancestor on Windowsc             C   s   t dgdkstt dgdks$tt dgdks6tt ddgdksJtt ddgdks^tt d	dgdksrtt d
dgdkstt d	dgdkstt d
dgdkstt ddgdkstt dgdkstt dgdkstt dgdkstd S )Nzx.pyir   z.\x.pyizfoo\bar\x.pyizfoo\barzfoo\bar\y.pyiz	foo\y.pyir   z	foo\x.pyizfoo\bar\zar\x.pyizfoo\bar\zar\y.pyizfoo/bar\x.pyizfoo\bar/x.pyizfoo/bar/x.pyi)r   rM   )r;   r-   r-   r1   test_common_dir_prefix_win  s    z+StubgenUtilSuite.test_common_dir_prefix_win)%rZ   r[   r\   r]   rl   ro   rp   rq   rr   rt   ru   rv   ra   r   r   rk   rx   rz   r|   r   r   r   r   r   r   r   r   r   r   r   r   r^   r_   rJ   r`   r   r   r-   r-   r-   r1   ri   }   s>   $
U
!ri   c               @   s(   e Zd ZddddZddddZdS )StubgenHelpersSuiteN)r*   c             C   sd   t drtt drtt dr$tt dr0tt ds<tt dsHtt dsTtt ds`td S )	Nz
foo/bar.pyzfoo.pyzfoo/xvendor/bar.pyzfoo/vendorx/bar.pyzfoo/vendor/bar.pyzfoo/vendored/bar.pyzfoo/vendored/bar/thing.pyz
foo/six.py)r   rM   )r;   r-   r-   r1   test_is_blacklisted_path  s    z,StubgenHelpersSuite.test_is_blacklisted_pathc             C   s   t drtt drtt dr$tt dr0tt dr<tt dsHtt dsTtt ds`tt d	sltt d
sxtt dstt dstt dstt dstt dstt dstt dstt dstt dstt dstd S )Nr   zfoo.barzfoo.bartestzfoo.bartestszfoo.testbarzfoo.testzfoo.test.fooz	foo.testszfoo.tests.foozfoo.testing.foozfoo.SelfTest.foozfoo.test_barzfoo.bar_testszfoo.testingzfoo.conftestzfoo.bar_test_utilzfoo.bar_test_utilszfoo.bar_test_basez	foo.setupzfoo.__main__)r   rM   )r;   r-   r-   r1   test_is_non_library_module  s(    z.StubgenHelpersSuite.test_is_non_library_module)rZ   r[   r\   r   r   r-   r-   r-   r1   r     s   
r   c               @   s   e Zd ZdZdZdZdgZeddddZeddd	d
Z	e
ee
 edddZe
ee
 dddZe
ee
 eddddZdS )StubgenPythonSuitea'  Data-driven end-to-end test cases that generate stub files.

    You can use these magic test case name suffixes:

    *_semanal
        Run semantic analysis (slow as this uses real stubs -- only use
        when necessary)
    *_import
        Import module and perform runtime introspection (in the current
        process!)

    You can use these magic comments:

    # flags: --some-stubgen-option ...
        Specify custom stubgen options

    # modules: module1 module2 ...
        Specify which modules to output (by default only 'main')
    Tr   zstubgen.testN)testcaser*   c          	   C   s    t   | | W d Q R X d S )N)r	   run_case_inner)r;   r   r-   r-   r1   run_case1  s    zStubgenPythonSuite.run_casec          
   C   s  g }g }d |j}x|jd|fg D ]v\}}|dd dd}|dr\|d\}}}|| d|kr||d	|g t|d
}	|		| W d Q R X q&W | 
||}
| |}d}zyd|jdsd|
_|jdsd|
_t|
 g }x.|D ]&}t||}| j||t|dkd qW W n* tk
rJ } z
|j}W d d }~X Y nX t|j|d|j|j W d x"|D ]}|tjkrptj|= qpW t| X d S )N
z	./main.py   /r   z	.__init__z-p z-mrQ   outZ_importTZ_semanal   )headerzInvalid output ({}, line {}))r:   inputrA   replaceendswith
rpartitionappendextendrR   rS   parse_flagsparse_modulesr~   Z	no_importZ
parse_onlyr   module_to_pathadd_filelenr   messagesr   outputformatrT   linerJ   modulesshutilrmtree)r;   r   extraZmodssourcerT   rC   r0   _rU   optionsr   out_diramodulefnamer-   r-   r1   r   5  sF    




 
z!StubgenPythonSuite.run_case_inner)program_textr   r*   c             C   sP   t jd|t jd}|r&|d }ng }t|| }d|krFd|_nd|_|S )Nz# flags: (.*)$)flagsr   z	--verboseT)research	MULTILINEgroupsplitr   quietverbose)r;   r   r   r   Z	flag_listr   r-   r-   r1   r   ]  s    zStubgenPythonSuite.parse_flags)r   r*   c             C   s.   t jd|t jd}|r$|d S dgS d S )Nz# modules: (.*)$)r   r   main)r   r   r   r   r   )r;   r   r   r-   r-   r1   r   j  s    z StubgenPythonSuite.parse_modules)r.   rV   r   r*   c          	   C   sp   t j|s&|d|dd  d S |rB|d|dd    t|dd}||   W d Q R X d S )Nz<%s was not generated>\r   z#    utf8)encoding)	r3   r.   existsr   r   rR   r   read
splitlines)r;   r.   rV   r   rT   r-   r-   r1   r   q  s    zStubgenPythonSuite.add_file)rZ   r[   r\   r]   Zrequired_out_section	base_pathrA   r   r   r   ra   r   r   r   r   boolr   r-   r-   r-   r1   r     s   (r   r;   )r~   c               @   s   e Zd ZdS )TestBaseClassN)rZ   r[   r\   r-   r-   r-   r1   r   ~  s   r   c               @   s   e Zd ZdS )	TestClassN)rZ   r[   r\   r-   r-   r-   r1   r     s   r   c               @   s  e Zd ZdZddddZddddZdddd	Zddd
dZddddZddddZ	ddddZ
ddddZddddZddddZddddZddddZddddZddddZddd d!Zddd"d#Zddd$d%Zddd&d'Zddd(d)Zddd*d+Zddd,d-Zddd.d/Zddd0d1Zddd2d3Zddd4d5Zddd6d7Zddd8d9ZdS ):StubgencSuitezoUnit tests for stub generation from C modules using introspection.

    Note that these don't cover a lot!
    N)r*   c             C   s   t tdtg d S )N__hash__)r   r   self_arg)r;   r-   r-   r1   test_infer_hash_sig  s    z!StubgencSuite.test_infer_hash_sigc             C   s   t tdttddg d S )N__getitem__index)r~   )r   r   r   r#   )r;   r-   r-   r1   test_infer_getitem_sig  s    z$StubgencSuite.test_infer_getitem_sigc             C   s$   t tdttddtddg d S )N__setitem__r   )r~   object)r   r   r   r#   )r;   r-   r-   r1   test_infer_setitem_sig  s    z$StubgencSuite.test_infer_setitem_sigc             C   s2   x,dD ]$}t td| dttddg qW d S )N)eqneltlegtgeaddZraddsubZrsubmulZrmul__other)r~   )r   r   r   r#   )r;   opr-   r-   r1   test_infer_binary_op_sig  s    
z&StubgencSuite.test_infer_binary_op_sigc             C   s*   x$dD ]}t td| dtg qW d S )N)negposr   )r   r   r   )r;   r   r-   r-   r1   test_infer_unary_op_sig  s    
z%StubgencSuite.test_infer_unary_op_sigc             C   s>   g }t dd}g }t|dt|| t|g  t|d d d S )Nr   rG   aliasr   zclass alias:)r   r   r   r   )r;   r   r0   importsr-   r-   r1   -test_generate_c_type_stub_no_crash_for_object  s    

z;StubgencSuite.test_generate_c_type_stub_no_crash_for_objectc             C   sL   G dd d}g }g }t dd}t|d||| t|g  t|ddg d S )Nc               @   s   e Zd ZdZdS )z^StubgencSuite.test_generate_c_type_stub_variable_type_annotation.<locals>.TestClassVariableClsr   N)rZ   r[   r\   r}   r-   r-   r-   r1   TestClassVariableCls  s   r   r   rG   Czclass C:z    x: ClassVar[int] = ...)r   r   r   )r;   r   r   r   r0   r-   r-   r1   2test_generate_c_type_stub_variable_type_annotation  s    

z@StubgencSuite.test_generate_c_type_stub_variable_type_annotationc             C   sJ   G dd dt }g }g }td}t|d||| t|dg t|g  d S )Nc               @   s   e Zd ZdS )zAStubgencSuite.test_generate_c_type_inheritance.<locals>.TestClassN)rZ   r[   r\   r-   r-   r-   r1   r     s   r   zmodule, r   zclass C(KeyError): ...)KeyErrorr   r   r   )r;   r   r   r   r0   r-   r-   r1    test_generate_c_type_inheritance  s    z.StubgencSuite.test_generate_c_type_inheritancec             C   s>   g }g }t tjd}t|dt|| t|dg t|g  d S )NrG   r   zclass C(TestBaseClass): ...)r   r   r[   r   r   r   )r;   r   r   r0   r-   r-   r1   ,test_generate_c_type_inheritance_same_module  s    z:StubgencSuite.test_generate_c_type_inheritance_same_modulec             C   sX   dd l }G dd d|j}g }g }tdd}t|d||| t|dg t|dg d S )	Nr   c               @   s   e Zd ZdS )zNStubgencSuite.test_generate_c_type_inheritance_other_module.<locals>.TestClassN)rZ   r[   r\   r-   r-   r-   r1   r     s   r   r   rG   r   zclass C(argparse.Action): ...zimport argparse)argparseActionr   r   r   )r;   r   r   r   r   r0   r-   r-   r1   -test_generate_c_type_inheritance_other_module  s    
z;StubgencSuite.test_generate_c_type_inheritance_other_modulec             C   sL   G dd dt }g }g }tdd}t|d||| t|dg t|g  d S )Nc               @   s   e Zd ZdS )zNStubgencSuite.test_generate_c_type_inheritance_builtin_type.<locals>.TestClassN)rZ   r[   r\   r-   r-   r-   r1   r     s   r   r   rG   r   zclass C(type): ...)r   r   r   r   )r;   r   r   r   r0   r-   r-   r1   -test_generate_c_type_inheritance_builtin_type  s    
z;StubgencSuite.test_generate_c_type_inheritance_builtin_typec          	   C   sT   G dd d}g }g }t |jd}t|d|j||ddd t|dg t|g  d S )Nc               @   s   e Zd ZeddddZdS )zDStubgencSuite.test_generate_c_type_with_docstring.<locals>.TestClassN)arg0r*   c             S   s   dS )zB
                test(self: TestClass, arg0: int)
                Nr-   )r;   r   r-   r-   r1   test  s    zIStubgencSuite.test_generate_c_type_with_docstring.<locals>.TestClass.test)rZ   r[   r\   ra   r   r-   r-   r-   r1   r     s   r   rG   r   r;   )self_var
class_namez%def test(self, arg0: int) -> Any: ...)r   r[   r   r   r   )r;   r   r   r   r0   r-   r-   r1   #test_generate_c_type_with_docstring  s    
z1StubgencSuite.test_generate_c_type_with_docstringc          	   C   sT   G dd d}g }g }t |jd}t|d|j||ddd t|dg t|g  d S )Nc               @   s   e Zd ZeddddZdS )zPStubgencSuite.test_generate_c_type_with_docstring_no_self_arg.<locals>.TestClassN)r   r*   c             S   s   dS )z1
                test(arg0: int)
                Nr-   )r;   r   r-   r-   r1   r     s    zUStubgencSuite.test_generate_c_type_with_docstring_no_self_arg.<locals>.TestClass.test)rZ   r[   r\   ra   r   r-   r-   r-   r1   r     s   r   rG   r   r;   )r  r  z%def test(self, arg0: int) -> Any: ...)r   r[   r   r   r   )r;   r   r   r   r0   r-   r-   r1   /test_generate_c_type_with_docstring_no_self_arg  s    
z=StubgencSuite.test_generate_c_type_with_docstring_no_self_argc          	   C   sT   G dd d}g }g }t |jd}t|d|j||ddd t|dg t|g  d S )Nc               @   s    e Zd ZeeddddZdS )zAStubgencSuite.test_generate_c_type_classmethod.<locals>.TestClassN)r   r*   c             S   s   d S )Nr-   )clsr   r-   r-   r1   r     s    zFStubgencSuite.test_generate_c_type_classmethod.<locals>.TestClass.test)rZ   r[   r\   classmethodra   r   r-   r-   r-   r1   r     s   r   rG   r   r  )r  r  z*def test(cls, *args, **kwargs) -> Any: ...)r   r[   r   r   r   )r;   r   r   r   r0   r-   r-   r1    test_generate_c_type_classmethod  s    
z.StubgencSuite.test_generate_c_type_classmethodc          	   C   sT   G dd d}g }g }t |jd}t|d|j||ddd t|dg t|g  d S )Nc               @   s   e Zd ZdeddddZdS )zRStubgencSuite.test_generate_c_type_with_docstring_empty_default.<locals>.TestClassrG   N)r   r*   c             S   s   dS )zG
                test(self: TestClass, arg0: str = "")
                Nr-   )r;   r   r-   r-   r1   r     s    zWStubgencSuite.test_generate_c_type_with_docstring_empty_default.<locals>.TestClass.test)rG   )rZ   r[   r\   ra   r   r-   r-   r-   r1   r     s   r   rG   r   r;   )r  r  z+def test(self, arg0: str = ...) -> Any: ...)r   r[   r   r   r   )r;   r   r   r   r0   r-   r-   r1   1test_generate_c_type_with_docstring_empty_default
  s    
z?StubgencSuite.test_generate_c_type_with_docstring_empty_defaultc             C   sP   t dddd}g }g }t| jd}t|d||| t|dg t|dg dS )	zQTest that if argument references type from other module, module will be imported.N)r   r*   c             S   s   dS )z5
            test(arg0: argparse.Action)
            Nr-   )r   r-   r-   r1   r     s    zEStubgencSuite.test_generate_c_function_other_module_arg.<locals>.testrG   r   z+def test(arg0: argparse.Action) -> Any: ...zimport argparse)ra   r   r[   r   r   )r;   r   r   r   r0   r-   r-   r1   )test_generate_c_function_other_module_arg  s    z7StubgencSuite.test_generate_c_function_other_module_argc             C   sL   t dddd}g }g }tdd}t|d||| t|dg t|g  dS )	zTest that if argument references type from same module but using full path, no module
        will be imported, and type specification will be striped to local reference.
        N)r   r*   c             S   s   dS )z5
            test(arg0: argparse.Action)
            Nr-   )r   r-   r-   r1   r   1  s    zDStubgencSuite.test_generate_c_function_same_module_arg.<locals>.testr   rG   r   z"def test(arg0: Action) -> Any: ...)ra   r   r   r   )r;   r   r   r   r0   r-   r-   r1   (test_generate_c_function_same_module_arg+  s    
z6StubgencSuite.test_generate_c_function_same_module_argc             C   sP   t dddd}g }g }t| jd}t|d||| t|dg t|dg dS )	zTTest that if return type references type from other module, module will be imported.N)r   r*   c             S   s   dS )z<
            test(arg0: str) -> argparse.Action
            Nr-   )r   r-   r-   r1   r   @  s    zEStubgencSuite.test_generate_c_function_other_module_ret.<locals>.testrG   r   z+def test(arg0: str) -> argparse.Action: ...zimport argparse)ra   r   r[   r   r   )r;   r   r   r   r0   r-   r-   r1   )test_generate_c_function_other_module_ret>  s    z7StubgencSuite.test_generate_c_function_other_module_retc             C   sL   t dddd}g }g }tdd}t|d||| t|dg t|g  dS )	zTest that if return type references type from same module but using full path,
        no module will be imported, and type specification will be striped to local reference.
        N)r   r*   c             S   s   dS )z<
            test(arg0: str) -> argparse.Action
            Nr-   )r   r-   r-   r1   r   Q  s    zDStubgencSuite.test_generate_c_function_same_module_ret.<locals>.testr   rG   r   z"def test(arg0: str) -> Action: ...)ra   r   r   r   )r;   r   r   r   r0   r-   r-   r1   (test_generate_c_function_same_module_retM  s    
z6StubgencSuite.test_generate_c_function_same_module_retc             C   sL   G dd d}g }g }t d|jg ||t|j t|g  t|ddg dS )z>Signatures included by PyBind11 inside property.fget are read.c               @   s&   e Zd ZddddZeeddZdS )zGStubgencSuite.test_generate_c_property_with_pybind11.<locals>.TestClassN)r*   c             S   s   dS )z:
                (self: TestClass) -> str
                Nr-   )r;   r-   r-   r1   get_attributea  s    zUStubgencSuite.test_generate_c_property_with_pybind11.<locals>.TestClass.get_attributerG   )doc)rZ   r[   r\   r  property	attributer-   r-   r-   r1   r   `  s   r   r  z	@propertyzdef attribute(self) -> str: ...N)r   r  r   r   )r;   r   readwrite_propertiesreadonly_propertiesr-   r-   r1   &test_generate_c_property_with_pybind11^  s    

z4StubgencSuite.test_generate_c_property_with_pybind11c             C   sN   G dd d}g }g }t dt|jg ||t|j t|dg t|g  d S )Nc               @   sB   e Zd ZddddZeedddZejeddddZdS )	zJStubgencSuite.test_generate_c_property_with_rw_property.<locals>.TestClassN)r*   c             S   s
   d| _ d S )Nr   )
_attribute)r;   r-   r-   r1   __init__r  s    zSStubgencSuite.test_generate_c_property_with_rw_property.<locals>.TestClass.__init__c             S   s   | j S )N)r  )r;   r-   r-   r1   r  u  s    zTStubgencSuite.test_generate_c_property_with_rw_property.<locals>.TestClass.attribute)valuer*   c             S   s
   || _ d S )N)r  )r;   r  r-   r-   r1   r  y  s    )rZ   r[   r\   r  r  r   r  setterr-   r-   r-   r1   r   q  s
   r   r  zattribute: Any)r   r   r  r   r   )r;   r   r  r  r-   r-   r1   )test_generate_c_property_with_rw_propertyp  s    z7StubgencSuite.test_generate_c_property_with_rw_propertyc          	   C   sT   G dd d}g }g }t |jd}t|d|j||ddd t|dg t|g  d S )Nc               @   s   e Zd ZeddddZdS )zMStubgencSuite.test_generate_c_type_with_single_arg_generic.<locals>.TestClassN)r   r*   c             S   s   dS )zH
                test(self: TestClass, arg0: List[int])
                Nr-   )r;   r   r-   r-   r1   r     s    zRStubgencSuite.test_generate_c_type_with_single_arg_generic.<locals>.TestClass.test)rZ   r[   r\   ra   r   r-   r-   r-   r1   r     s   r   rG   r   r;   )r  r  z+def test(self, arg0: List[int]) -> Any: ...)r   r[   r   r   r   )r;   r   r   r   r0   r-   r-   r1   ,test_generate_c_type_with_single_arg_generic  s    
z:StubgencSuite.test_generate_c_type_with_single_arg_genericc          	   C   sT   G dd d}g }g }t |jd}t|d|j||ddd t|dg t|g  d S )Nc               @   s   e Zd ZeddddZdS )zMStubgencSuite.test_generate_c_type_with_double_arg_generic.<locals>.TestClassN)r   r*   c             S   s   dS )zM
                test(self: TestClass, arg0: Dict[str, int])
                Nr-   )r;   r   r-   r-   r1   r     s    zRStubgencSuite.test_generate_c_type_with_double_arg_generic.<locals>.TestClass.test)rZ   r[   r\   ra   r   r-   r-   r-   r1   r     s   r   rG   r   r;   )r  r  z/def test(self, arg0: Dict[str,int]) -> Any: ...)r   r[   r   r   r   )r;   r   r   r   r0   r-   r-   r1   ,test_generate_c_type_with_double_arg_generic  s    
z:StubgencSuite.test_generate_c_type_with_double_arg_genericc          	   C   sT   G dd d}g }g }t |jd}t|d|j||ddd t|dg t|g  d S )Nc               @   s   e Zd ZeddddZdS )zIStubgencSuite.test_generate_c_type_with_nested_generic.<locals>.TestClassN)r   r*   c             S   s   dS )zS
                test(self: TestClass, arg0: Dict[str, List[int]])
                Nr-   )r;   r   r-   r-   r1   r     s    zNStubgencSuite.test_generate_c_type_with_nested_generic.<locals>.TestClass.test)rZ   r[   r\   ra   r   r-   r-   r-   r1   r     s   r   rG   r   r;   )r  r  z5def test(self, arg0: Dict[str,List[int]]) -> Any: ...)r   r[   r   r   r   )r;   r   r   r   r0   r-   r-   r1   (test_generate_c_type_with_nested_generic  s    
z6StubgencSuite.test_generate_c_type_with_nested_genericc          	   C   sV   G dd d}g }g }t |jd}t|d|j||ddd t|dg t|dg d S )	Nc               @   s   e Zd ZeddddZdS )z[StubgencSuite.test_generate_c_type_with_generic_using_other_module_first.<locals>.TestClassN)r   r*   c             S   s   dS )zY
                test(self: TestClass, arg0: Dict[argparse.Action, int])
                Nr-   )r;   r   r-   r-   r1   r     s    z`StubgencSuite.test_generate_c_type_with_generic_using_other_module_first.<locals>.TestClass.test)rZ   r[   r\   ra   r   r-   r-   r-   r1   r     s   r   rG   r   r;   )r  r  z;def test(self, arg0: Dict[argparse.Action,int]) -> Any: ...zimport argparse)r   r[   r   r   r   )r;   r   r   r   r0   r-   r-   r1   :test_generate_c_type_with_generic_using_other_module_first  s    
zHStubgencSuite.test_generate_c_type_with_generic_using_other_module_firstc          	   C   sV   G dd d}g }g }t |jd}t|d|j||ddd t|dg t|dg d S )	Nc               @   s   e Zd ZeddddZdS )zZStubgencSuite.test_generate_c_type_with_generic_using_other_module_last.<locals>.TestClassN)r   r*   c             S   s   dS )zY
                test(self: TestClass, arg0: Dict[str, argparse.Action])
                Nr-   )r;   r   r-   r-   r1   r     s    z_StubgencSuite.test_generate_c_type_with_generic_using_other_module_last.<locals>.TestClass.test)rZ   r[   r\   ra   r   r-   r-   r-   r1   r     s   r   rG   r   r;   )r  r  z;def test(self, arg0: Dict[str,argparse.Action]) -> Any: ...zimport argparse)r   r[   r   r   r   )r;   r   r   r   r0   r-   r-   r1   9test_generate_c_type_with_generic_using_other_module_last  s    
zGStubgencSuite.test_generate_c_type_with_generic_using_other_module_lastc          	   C   sd   G dd d}g }g }t |jd}t|d|j||ddd t|dddd	dd
g tt|dh d S )Nc               @   s   e Zd ZeddddZdS )zLStubgencSuite.test_generate_c_type_with_overload_pybind11.<locals>.TestClassN)r   r*   c             S   s   dS )z
                __init__(*args, **kwargs)
                Overloaded function.

                1. __init__(self: TestClass, arg0: str) -> None

                2. __init__(self: TestClass, arg0: str, arg1: str) -> None
                Nr-   )r;   r   r-   r-   r1   r    s    	zUStubgencSuite.test_generate_c_type_with_overload_pybind11.<locals>.TestClass.__init__)rZ   r[   r\   ra   r  r-   r-   r-   r1   r     s   r   rG   r  r;   )r  r  z	@overloadz*def __init__(self, arg0: str) -> None: ...z5def __init__(self, arg0: str, arg1: str) -> None: ...z)def __init__(*args, **kwargs) -> Any: ...zfrom typing import overload)r   r[   r   r  r   rd   )r;   r   r   r   r0   r-   r-   r1   +test_generate_c_type_with_overload_pybind11  s    
z9StubgencSuite.test_generate_c_type_with_overload_pybind11)rZ   r[   r\   r]   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r	  r
  r  r  r  r  r  r  r  r  r  r  r-   r-   r-   r1   r     s8   r   c               @   s   e Zd ZddddZdS )ArgSigSuiteN)r*   c             C   sX   t ttddd t ttddd t ttddd t ttddd	d
d d S )Nzasd"dsa)r~   z0ArgSig(name='asd"dsa', type=None, default=False)zasd'dsaz0ArgSig(name="asd'dsa", type=None, default=False)rj   ra   z.ArgSig(name='func', type='str', default=False)T)r   z-ArgSig(name='func', type='str', default=True))r   reprr#   )r;   r-   r-   r1   	test_repr  s    zArgSigSuite.test_repr)rZ   r[   r\   r!  r-   r-   r-   r1   r    s   r  c               @   s   e Zd ZddddZdS )IsValidTypeSuiteN)r*   c             C   s   t dstt dstt ds$tt ds0tt ds<tt dsHtt dsTtt dr`tt d	rltt d
rxtt drtt drtt drtd S )Nr   ra   Z
Foo_Bar234zfoo.barz	List[int]zDict[str, int]Nonezfoo-barzx->yTrueFalsezx,yzx, y)r%   rM   )r;   r-   r-   r1   test_is_valid_type  s    z#IsValidTypeSuite.test_is_valid_type)rZ   r[   r\   r&  r-   r-   r-   r1   r"    s   r"  c               @   sD   e Zd ZddddZddddZddddZddd	d
ZdS )ModuleInspectSuiteN)r*   c          	   C   sn   t  ^}|d}|d k	st|jdks,t|js6t|jd ksDt|jdksRt|jg ks`tW d Q R X d S )NinspectF)r&   get_package_propertiesrM   r~   rT   r.   is_c_modulesubpackages)r;   rg   pr-   r-   r1   test_python_module  s    

z%ModuleInspectSuite.test_python_modulec          	   C   s~   t  n}|d}|d k	st|jdks,t|js6t|js@t|jdksNt|jsXttdd |jD sptW d Q R X d S )Nr^   Fc             s   s   | ]}| d V  qdS )z	unittest.N)
startswith)r/   r   r-   r-   r1   	<genexpr>"  s    z9ModuleInspectSuite.test_python_package.<locals>.<genexpr>)	r&   r)  rM   r~   rT   r.   r*  r+  all)r;   rg   r,  r-   r-   r1   test_python_package  s    



z&ModuleInspectSuite.test_python_packagec          	   C   sd   t  T}|d}|d k	st|jdks,t|jd ks:t|jdksHt|jg ksVtW d Q R X d S )N_socketT)r&   r)  rM   r~   r.   r*  r+  )r;   rg   r,  r-   r-   r1   test_c_module$  s    
z ModuleInspectSuite.test_c_modulec          
   C   sH   t  8}| t}|d W d Q R X t|jdks:tW d Q R X d S )Nzfoobar-non-existentz%No module named 'foobar-non-existent')r&   assertRaisesr'   r)  ra   	exceptionrM   )r;   rg   r   r-   r-   r1   test_non_existent-  s    z$ModuleInspectSuite.test_non_existent)rZ   r[   r\   r-  r1  r3  r6  r-   r-   r-   r1   r'    s   
	r'  )r   r   r*   c             C   sH   t j| |dd d}t j|sD|dd}t j|rD|S |S )Nr   r   z.pyiz/__init__.pyi)r3   r.   r:   r   r   )r   r   r   Zalt_fnamr-   r-   r1   r   4  s    r   )GrH   os.pathr3   r   rJ   r5   r   r^   typesr   typingr   r   r   r   Zmypy.test.helpersr   r   r	   Zmypy.test.datar
   r   Zmypy.errorsr   Zmypy.stubgenr   r   r   r   r   r   r   Zmypy.stubutilr   r   r   Zmypy.stubgencr   r   r   r   r   Zmypy.stubdocr   r   r   r   r    r!   r"   r#   r$   r%   Zmypy.moduleinspectr&   r'   ZTestCaser(   rc   ri   r   r   r   r   r   r   r  r"  r'  ra   r   r-   r-   r-   r1   <module>   sB   $0F  t)c
  m&