diff options
author | Andrew Simmons <a.simmons@deakin.edu.au> | 2020-04-20 23:36:26 +1000 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2020-04-22 08:26:43 +0200 |
commit | aa1940e35a8c78cc362f112b6bf498f97ee6640d (patch) | |
tree | ded8e0db3bd1bc0b5c422784d630e86226c76eab | |
parent | d1484608e89c3e7e2900183e9fb9cbc183de1fb5 (diff) | |
download | pylint-git-aa1940e35a8c78cc362f112b6bf498f97ee6640d.tar.gz |
Fix false positive for ``undefined-variable`` when using class attribute as return type annotation (#1976)
-rw-r--r-- | ChangeLog | 3 | ||||
-rw-r--r-- | pylint/checkers/variables.py | 4 | ||||
-rw-r--r-- | tests/unittest_checker_variables.py | 17 |
3 files changed, 23 insertions, 1 deletions
@@ -7,9 +7,10 @@ What's New in Pylint 2.5.0? Release date: TBA -* Fix a false positive for ``undefined-variable`` when using class attribute in decorator +* Fix a false positive for ``undefined-variable`` when using class attribute in decorator or as type hint. Close #511 + Close #1976 * Remove HTML quoting of messages in JSON output. diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index ac1d176ee..390bb5931 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1261,6 +1261,10 @@ class VariablesChecker(BaseChecker): ) or frame.args.parent_of(node) or (frame.decorators and frame.decorators.parent_of(node)) + or ( + frame.returns + and (node is frame.returns or frame.returns.parent_of(node)) + ) ) return in_annotation_or_default_or_decorator diff --git a/tests/unittest_checker_variables.py b/tests/unittest_checker_variables.py index f33dbf8af..ff7edf367 100644 --- a/tests/unittest_checker_variables.py +++ b/tests/unittest_checker_variables.py @@ -145,6 +145,23 @@ class TestVariablesChecker(CheckerTestCase): with self.assertNoMessages(): self.walk(module) + def test_return_type_annotation(self): + """ Make sure class attributes in scope for return type annotations. + + https://github.com/PyCQA/pylint/issues/1976 + """ + module = astroid.parse( + """ + class MyObject: + class MyType: + pass + def my_method(self) -> MyType: + pass + """ + ) + with self.assertNoMessages(): + self.walk(module) + class TestVariablesCheckerWithTearDown(CheckerTestCase): |