From 16b4d339814ef911a2e3bbee69a68f46fb6c3d0c Mon Sep 17 00:00:00 2001 From: Luigi Date: Thu, 1 Oct 2020 12:32:09 +1000 Subject: [PATCH 1/3] bugfix: accepts aliases for __future__ annotations --- pylint/checkers/utils.py | 8 +----- ...stponed_evaluation_activated_with_alias.py | 28 +++++++++++++++++++ ...stponed_evaluation_activated_with_alias.rc | 2 ++ 3 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 tests/functional/p/postponed_evaluation_activated_with_alias.py create mode 100644 tests/functional/p/postponed_evaluation_activated_with_alias.rc diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py index 227c57e95e..3f3ce37467 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -1262,14 +1262,8 @@ def get_node_last_lineno(node: astroid.node_classes.NodeNG) -> int: def is_postponed_evaluation_enabled(node: astroid.node_classes.NodeNG) -> bool: """Check if the postponed evaluation of annotations is enabled""" - name = "annotations" module = node.root() - stmt = module.locals.get(name) - return ( - stmt - and isinstance(stmt[0], astroid.ImportFrom) - and stmt[0].modname == "__future__" - ) + return "annotations" in module.future_imports def is_subclass_of(child: astroid.ClassDef, parent: astroid.ClassDef) -> bool: diff --git a/tests/functional/p/postponed_evaluation_activated_with_alias.py b/tests/functional/p/postponed_evaluation_activated_with_alias.py new file mode 100644 index 0000000000..deebfdf117 --- /dev/null +++ b/tests/functional/p/postponed_evaluation_activated_with_alias.py @@ -0,0 +1,28 @@ +# pylint: disable=missing-docstring,no-self-use,unused-argument,pointless-statement +# pylint: disable=too-few-public-methods,no-name-in-module +from __future__ import annotations as __annotations__ + + +class Class: + @classmethod + def from_string(cls, source) -> Class: + ... + + def validate_b(self, obj: OtherClass) -> bool: + ... + + +class OtherClass: + ... + + +class Example: + obj: Other + + +class Other: + ... + + +class ExampleSelf: + next: ExampleSelf diff --git a/tests/functional/p/postponed_evaluation_activated_with_alias.rc b/tests/functional/p/postponed_evaluation_activated_with_alias.rc new file mode 100644 index 0000000000..a17bb22daf --- /dev/null +++ b/tests/functional/p/postponed_evaluation_activated_with_alias.rc @@ -0,0 +1,2 @@ +[testoptions] +min_pyver=3.7 From 9dbbbf72189189d7e823a9db1ea3fd1ae24f2586 Mon Sep 17 00:00:00 2001 From: Luigi Date: Thu, 1 Oct 2020 17:21:58 +1000 Subject: [PATCH 2/3] Add changelog and contributors info --- CONTRIBUTORS.txt | 2 ++ ChangeLog | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 2facff2b3b..dfa613af83 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -411,3 +411,5 @@ contributors: * Josselin Feist: contributor * David Cain: contributor + +* Luigi Bertaco Cristofolini (luigibertaco): contributor diff --git a/ChangeLog b/ChangeLog index a231cf09b7..cdde3329cb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -33,6 +33,10 @@ Release date: TBA * Fix AttributeError in checkers/refactoring.py +* Fix a bug with postponed evaluation when using aliases for annotations. + + Close #3798 + What's New in Pylint 2.6.0? =========================== From badc42547481aa6fb1481f0a6833821bb562ebd3 Mon Sep 17 00:00:00 2001 From: Luigi Bertaco Cristofolini Date: Thu, 8 Oct 2020 10:57:46 +1100 Subject: [PATCH 3/3] Updated class name --- .../functional/p/postponed_evaluation_activated_with_alias.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/functional/p/postponed_evaluation_activated_with_alias.py b/tests/functional/p/postponed_evaluation_activated_with_alias.py index deebfdf117..4f47247711 100644 --- a/tests/functional/p/postponed_evaluation_activated_with_alias.py +++ b/tests/functional/p/postponed_evaluation_activated_with_alias.py @@ -3,9 +3,9 @@ from __future__ import annotations as __annotations__ -class Class: +class MyClass: @classmethod - def from_string(cls, source) -> Class: + def from_string(cls, source) -> MyClass: ... def validate_b(self, obj: OtherClass) -> bool: