o
    b"                     @   s   d 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 G dd deZG dd	 d	eZG d
d deZdS )z%Unit tests for the variables checker.    N)	variables)CheckerTestCaselinter
set_configMessage)	UNDEFINEDc                   @   sz   e Zd ZejZdd Zedddd Zdd Z	d	d
 Z
edddd Zedddd Zedddd Zdd ZdS )TestVariablesCheckerc                 C   B   t d}|   | | W d   dS 1 sw   Y  dS )z4 Issue 78 report a false positive for unused-module z
        from sys import path
        path += ['stuff']
        def func():
            other = 1
            return len(other)
        NastroidparseassertNoMessageswalk)selfmodule r   e/var/www/secure340b-portal/env/lib/python3.10/site-packages/pylint/test/unittest_checker_variables.pytest_bitbucket_issue_78   s   
	"z,TestVariablesChecker.test_bitbucket_issue_78)argparse)Zignored_modulesc                 C   sD   t d}|   | j| W d   dS 1 sw   Y  dS )zMake sure that 'from ... import ...' does not emit a
        'no-name-in-module' with a module that is configured
        to be ignored.
        z:
        from argparse import THIS_does_not_EXIST
        N)r   extract_noder   checkerZvisit_importfromr   noder   r   r   test_no_name_in_module_skipped-   s   
"z3TestVariablesChecker.test_no_name_in_module_skippedc                 C   sl   t d}|jjt d | }|   | j	| | j
| W d    d S 1 s/w   Y  d S )Nz__all__ = []test)r   r   valueeltsappendZConstrootr   r   visit_moduleZleave_module)r   r   r   r   r   r    test_all_elements_without_parent<   s   

"z5TestVariablesChecker.test_all_elements_without_parentc                 C   D   t d}|   | j| W d    d S 1 sw   Y  d S )N2
        from future.builtins import open
        r   r   r   r   r   r   r   r   r   test_redefined_builtin_ignoredD   s   
"z3TestVariablesChecker.test_redefined_builtin_ignoredosZredefining_builtins_modulesc                 C   r!   )Nz%
        from os import open
        r#   r   r   r   r   %test_redefined_builtin_custom_modulesM   s   
"z:TestVariablesChecker.test_redefined_builtin_custom_modulesc                 C   V   t d}| td|jd dd | j| W d    d S 1 s$w   Y  d S Nr"   zredefined-builtinr   openr   argsr   r   assertAddsMessagesr   bodyr   r   r   r   r   r   *test_redefined_builtin_modname_not_ignoredW      "z?TestVariablesChecker.test_redefined_builtin_modname_not_ignoredc                 C   T   t d}|   | j|  | j| W d    d S 1 s#w   Y  d S Nz=
        def test():
            from os import open
        r   r   r   r   r   r   visit_functiondefr   r   r   r   "test_redefined_builtin_in_functionc      
"z7TestVariablesChecker.test_redefined_builtin_in_functionc                 C   sT   t d}td|td}| | | j| W d    d S 1 s#w   Y  d S )Nza
            def func():
                global sys  #@
                import sys, lala
        zglobal-statement)r   
confidence)r   r   r   r   r/   r   Zvisit_global)r   r   msgr   r   r   test_unassigned_globalo   s   "z+TestVariablesChecker.test_unassigned_globalN)__name__
__module____qualname__r   VariablesCheckerCHECKER_CLASSr   r   r   r    r$   r(   r1   r7   r;   r   r   r   r   r      s    
	
	

r   c                       s   e Zd ZejZ fddZdd Zedddd Z	ed	d
dd Z
ed	d
dd Zdd Zdd Zdd Zeedddd Zeedddd Z  ZS ) TestVariablesCheckerWithTearDownc                    s$   t t|   | jj| _g | j_d S N)superrA   setup_methodr   _to_consume_to_consume_backup)r   	__class__r   r   rD      s   
z-TestVariablesCheckerWithTearDown.setup_methodc                 C   s   | j | j_d S rB   )rF   r   rE   )r   methodr   r   r   teardown_method   s   z0TestVariablesCheckerWithTearDown.teardown_method)Z	callback_	_callback)	callbacksc                 C   sN  t d}|   | j| | j| W d   n1 s w   Y  t d}|   | j| | j| W d   n1 sEw   Y  t d}| td|d dd | j| | j| W d   n1 srw   Y  t d}| td|d dd | j| | j| W d   dS 1 sw   Y  dS )	z# Test the --calbacks option works. z^
        def callback_one(abc):
             ''' should not emit unused-argument. '''
        Nzd
        def two_callback(abc, defg):
             ''' should not emit unused-argument. '''
        zY
        def normal_func(abc):
             ''' should emit unused-argument. '''
        zunused-argumentabcr,   z[
        def cb_func(abc):
             ''' Previous callbacks are overridden. '''
        )r   r   r   r   r6   Zleave_functiondefr/   r   r   r   r   r   test_custom_callback_string   s@   

"z<TestVariablesCheckerWithTearDown.test_custom_callback_stringr%   r'   c                 C   r)   r*   r.   r   r   r   r   r1      r2   zKTestVariablesCheckerWithTearDown.test_redefined_builtin_modname_not_ignoredc                 C   r3   r4   r5   r   r   r   r   r7      r8   zCTestVariablesCheckerWithTearDown.test_redefined_builtin_in_functionc                 C   B   t d}|   | | W d    d S 1 sw   Y  d S )Nz"
        import math as _
        r
   r   r   r   r   test_import_as_underscore   s   
"z:TestVariablesCheckerWithTearDown.test_import_as_underscorec                 C   rO   )Nz
        class MyObject(object):
            method1 = lambda func: func()
            method2 = lambda function: function()
        r
   r   r   r   r   test_lambda_in_classdef   s   
"z8TestVariablesCheckerWithTearDown.test_lambda_in_classdefc                 C   r	   )zMake sure variables from parent lambdas
        aren't noted as undefined

        https://github.com/PyCQA/pylint/issues/760
        z)
        lambda x: lambda: x + 1
        Nr
   r   r   r   r   test_nested_lambda   s   
"z3TestVariablesCheckerWithTearDown.test_nested_lambdaarg)Zignored_argument_namesc                 C   r	   )zOMake sure is_ignored_argument_names properly ignores
        function argumentsz2
        def fooby(arg):
            pass
        Nr
   r   r   r   r   &test_ignored_argument_names_no_message   s   
"zGTestVariablesCheckerWithTearDown.test_ignored_argument_names_no_messagezargs|kwargsc                 C   rO   )Nz>
        def fooby(*args, **kwargs):
            pass
        r
   r   r   r   r   (test_ignored_argument_names_starred_args  s   
"zITestVariablesCheckerWithTearDown.test_ignored_argument_names_starred_args)r<   r=   r>   r   r?   r@   rD   rJ   r   rN   r1   r7   rP   rQ   rR   recompilerT   rU   __classcell__r   r   rG   r   rA   |   s"    
.

	
rA   c                   @   s   e Zd ZejZdd ZdS )TestMissingSubmodulec              	   C   s|   t jt jt jtd}tjd| z t	t j|d tj
  }|dks.J W tjd d S tjd w )NZregrtest_datar   Zpackage_allz3E:  3: Undefined variable name 'missing' in __all__)r&   pathjoindirnameabspath__file__sysinsertr   checkreporterfinalizestrippop)r   Z	regr_datagotr   r   r   test_package_all  s   z%TestMissingSubmodule.test_package_allN)r<   r=   r>   r   r?   r@   rg   r   r   r   r   rY     s    rY   )__doc__r_   r&   rV   r   Zpylint.checkersr   Zpylint.testutilsr   r   r   r   Zpylint.interfacesr   r   rA   rY   r   r   r   r   <module>   s   a 