diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2018-05-23 17:36:41 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2018-05-23 17:39:18 +0200 |
commit | 901f652fcf35a5a7b7746e9780770b36518c977c (patch) | |
tree | e72c9245ea5ba0afdfd565ffa93b6ba6bed41e48 /pylint/test/unittest_checker_python3.py | |
parent | aae9a6b4626ac5875e3c34d0e31cc0585dbbb82f (diff) | |
download | pylint-git-901f652fcf35a5a7b7746e9780770b36518c977c.tar.gz |
Rewrite the comprehension-escape and exception-escape to work only on the corresponding nodes
These two checks were implemented in terms of visit_namne, that is, for every name in the tree,
we looked to see if that name leaked. This was resulting in a couple of false positives
and also in a performance issue, since we were calling nodes_of_class() and scope() for each
name node. Instead, this approach uses the visit methods for exception and comprehension nodes
and looks to see from there if the current scope uses leaked names.
This is not the ideal situation as well, ideal would be to have access to the definition
frame of each name, but that requires some extra engineering effort in astroid to get it right.
Close #2106
Diffstat (limited to 'pylint/test/unittest_checker_python3.py')
-rw-r--r-- | pylint/test/unittest_checker_python3.py | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/pylint/test/unittest_checker_python3.py b/pylint/test/unittest_checker_python3.py index 10ea7c1dc..6d2a2debc 100644 --- a/pylint/test/unittest_checker_python3.py +++ b/pylint/test/unittest_checker_python3.py @@ -33,6 +33,7 @@ python2_only = pytest.mark.skipif(sys.version_info[0] > 2, reason='Python 2 only # TODO(cpopa): Port these to the functional test framework instead. class TestPython3Checker(testutils.CheckerTestCase): + CHECKER_CLASS = checker.Python3Checker def check_bad_builtin(self, builtin_name): @@ -714,21 +715,22 @@ class TestPython3Checker(testutils.CheckerTestCase): self.checker.visit_attribute(node) def test_comprehension_escape(self): - list_comp, set_comp, dict_comp = astroid.extract_node(''' - [i for i in range(10)] + assign, escaped_node = astroid.extract_node(''' + a = [i for i in range(10)] #@ i #@ - {c for c in range(10)} - c #@ - {j:j for j in range(10)} - j #@ ''') - message = testutils.Message('comprehension-escape', node=list_comp) + good_module = astroid.parse(''' + {c for c in range(10)} #@ + {j:j for j in range(10)} #@ + [image_child] = [x for x in range(10)] + thumbnail = func(__(image_child)) + ''') + message = testutils.Message('comprehension-escape', node=escaped_node) with self.assertAddsMessages(message): - self.checker.visit_name(list_comp) + self.checker.visit_listcomp(assign.value) - for node in (set_comp, dict_comp): - with self.assertNoMessages(): - self.checker.visit_name(node) + with self.assertNoMessages(): + self.walk(good_module) def test_comprehension_escape_newly_introduced(self): node = astroid.extract_node(''' @@ -740,7 +742,7 @@ class TestPython3Checker(testutils.CheckerTestCase): self.walk(node) def test_exception_escape(self): - bad, good = astroid.extract_node(''' + module = astroid.parse(''' try: 1/0 except ValueError as exc: pass @@ -751,11 +753,11 @@ class TestPython3Checker(testutils.CheckerTestCase): exc = 2 exc #@ ''') - message = testutils.Message('exception-escape', node=bad) + message = testutils.Message('exception-escape', node=module.body[1].value) with self.assertAddsMessages(message): - self.checker.visit_name(bad) + self.checker.visit_excepthandler(module.body[0].handlers[0]) with self.assertNoMessages(): - self.checker.visit_name(good) + self.checker.visit_excepthandler(module.body[2].handlers[0]) def test_bad_sys_attribute(self): node = astroid.extract_node(''' |