Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unittest_checker_variables.py 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2014-2017 Claudiu Popa <pcmanticore@gmail.com>
  3. # Copyright (c) 2014 Google, Inc.
  4. # Copyright (c) 2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  5. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  6. # Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
  7. # Copyright (c) 2017 Ville Skyttä <ville.skytta@iki.fi>
  8. # Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com>
  9. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  10. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  11. """Unit tests for the variables checker."""
  12. import sys
  13. import os
  14. import astroid
  15. from pylint.checkers import variables
  16. from pylint.testutils import CheckerTestCase, linter, set_config, Message
  17. class TestVariablesChecker(CheckerTestCase):
  18. CHECKER_CLASS = variables.VariablesChecker
  19. def test_bitbucket_issue_78(self):
  20. """ Issue 78 report a false positive for unused-module """
  21. module = astroid.parse("""
  22. from sys import path
  23. path += ['stuff']
  24. def func():
  25. other = 1
  26. return len(other)
  27. """)
  28. with self.assertNoMessages():
  29. self.walk(module)
  30. @set_config(ignored_modules=('argparse',))
  31. def test_no_name_in_module_skipped(self):
  32. """Make sure that 'from ... import ...' does not emit a
  33. 'no-name-in-module' with a module that is configured
  34. to be ignored.
  35. """
  36. node = astroid.extract_node("""
  37. from argparse import THIS_does_not_EXIST
  38. """)
  39. with self.assertNoMessages():
  40. self.checker.visit_importfrom(node)
  41. def test_all_elements_without_parent(self):
  42. node = astroid.extract_node('__all__ = []')
  43. node.value.elts.append(astroid.Const('test'))
  44. root = node.root()
  45. with self.assertNoMessages():
  46. self.checker.visit_module(root)
  47. self.checker.leave_module(root)
  48. def test_redefined_builtin_ignored(self):
  49. node = astroid.parse('''
  50. from future.builtins import open
  51. ''')
  52. with self.assertNoMessages():
  53. self.checker.visit_module(node)
  54. @set_config(redefining_builtins_modules=('os',))
  55. def test_redefined_builtin_custom_modules(self):
  56. node = astroid.parse('''
  57. from os import open
  58. ''')
  59. with self.assertNoMessages():
  60. self.checker.visit_module(node)
  61. @set_config(redefining_builtins_modules=('os',))
  62. def test_redefined_builtin_modname_not_ignored(self):
  63. node = astroid.parse('''
  64. from future.builtins import open
  65. ''')
  66. with self.assertAddsMessages(
  67. Message('redefined-builtin', node=node.body[0], args='open')):
  68. self.checker.visit_module(node)
  69. @set_config(redefining_builtins_modules=('os',))
  70. def test_redefined_builtin_in_function(self):
  71. node = astroid.extract_node('''
  72. def test():
  73. from os import open
  74. ''')
  75. with self.assertNoMessages():
  76. self.checker.visit_module(node.root())
  77. self.checker.visit_functiondef(node)
  78. class TestVariablesCheckerWithTearDown(CheckerTestCase):
  79. CHECKER_CLASS = variables.VariablesChecker
  80. def setup_method(self):
  81. super(TestVariablesCheckerWithTearDown, self).setup_method()
  82. self._to_consume_backup = self.checker._to_consume
  83. self.checker._to_consume = []
  84. def teardown_method(self, method):
  85. self.checker._to_consume = self._to_consume_backup
  86. @set_config(callbacks=('callback_', '_callback'))
  87. def test_custom_callback_string(self):
  88. """ Test the --calbacks option works. """
  89. node = astroid.extract_node("""
  90. def callback_one(abc):
  91. ''' should not emit unused-argument. '''
  92. """)
  93. with self.assertNoMessages():
  94. self.checker.visit_functiondef(node)
  95. self.checker.leave_functiondef(node)
  96. node = astroid.extract_node("""
  97. def two_callback(abc, defg):
  98. ''' should not emit unused-argument. '''
  99. """)
  100. with self.assertNoMessages():
  101. self.checker.visit_functiondef(node)
  102. self.checker.leave_functiondef(node)
  103. node = astroid.extract_node("""
  104. def normal_func(abc):
  105. ''' should emit unused-argument. '''
  106. """)
  107. with self.assertAddsMessages(
  108. Message('unused-argument', node=node['abc'], args='abc')):
  109. self.checker.visit_functiondef(node)
  110. self.checker.leave_functiondef(node)
  111. node = astroid.extract_node("""
  112. def cb_func(abc):
  113. ''' Previous callbacks are overridden. '''
  114. """)
  115. with self.assertAddsMessages(
  116. Message('unused-argument', node=node['abc'], args='abc')):
  117. self.checker.visit_functiondef(node)
  118. self.checker.leave_functiondef(node)
  119. @set_config(redefining_builtins_modules=('os',))
  120. def test_redefined_builtin_modname_not_ignored(self):
  121. node = astroid.parse('''
  122. from future.builtins import open
  123. ''')
  124. with self.assertAddsMessages(
  125. Message('redefined-builtin', node=node.body[0], args='open')):
  126. self.checker.visit_module(node)
  127. @set_config(redefining_builtins_modules=('os',))
  128. def test_redefined_builtin_in_function(self):
  129. node = astroid.extract_node('''
  130. def test():
  131. from os import open
  132. ''')
  133. with self.assertNoMessages():
  134. self.checker.visit_module(node.root())
  135. self.checker.visit_functiondef(node)
  136. def test_import_as_underscore(self):
  137. node = astroid.parse('''
  138. import math as _
  139. ''')
  140. with self.assertNoMessages():
  141. self.walk(node)
  142. def test_lambda_in_classdef(self):
  143. # Make sure lambda doesn't raises
  144. # Undefined-method in class function
  145. # Issue 1824
  146. # https://github.com/PyCQA/pylint/issues/1824
  147. node = astroid.parse('''
  148. class MyObject(object):
  149. method1 = lambda func: func()
  150. method2 = lambda function: function()
  151. ''')
  152. with self.assertNoMessages():
  153. self.walk(node)
  154. def test_scope_in_defaults(self):
  155. # Should not emit undefined variable
  156. node = astroid.extract_node('''
  157. def foof(x=[i for i in [1, 2]]):
  158. return x
  159. ''')
  160. # Trickier to detect
  161. node = astroid.extract_node('''
  162. def foof(x=[(i, 1) for i in [1, 2]]):
  163. return x
  164. ''')
  165. with self.assertNoMessages():
  166. self.walk(node)
  167. # Lambdas have their own scope
  168. node = astroid.extract_node('''
  169. def foof(x=lambda zoo: zoo):
  170. return x
  171. ''')
  172. with self.assertNoMessages():
  173. self.walk(node)
  174. class TestMissingSubmodule(CheckerTestCase):
  175. CHECKER_CLASS = variables.VariablesChecker
  176. def test_package_all(self):
  177. regr_data = os.path.join(os.path.dirname(os.path.abspath(__file__)),
  178. 'regrtest_data')
  179. sys.path.insert(0, regr_data)
  180. try:
  181. linter.check(os.path.join(regr_data, 'package_all'))
  182. got = linter.reporter.finalize().strip()
  183. assert got == "E: 3: Undefined variable name 'missing' in __all__"
  184. finally:
  185. sys.path.pop(0)