Skip to content

Commit 1a1dea5

Browse files
authored
Merge pull request #3868 from luigibertaco/from_future_annotations_as
bugfix: accepts aliases for __future__ annotations
2 parents 83bc759 + badc425 commit 1a1dea5

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

CONTRIBUTORS.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ contributors:
412412

413413
* David Cain: contributor
414414

415+
* Luigi Bertaco Cristofolini (luigibertaco): contributor
416+
415417
* Or Bahari
416418

417419
* Joshua Cannon: contributor

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ Release date: TBA
3838

3939
* Fix AttributeError in checkers/refactoring.py
4040

41+
* Fix a bug with postponed evaluation when using aliases for annotations.
42+
43+
Close #3798
44+
4145
* Fix minor documentation issues
4246

4347
What's New in Pylint 2.6.0?

pylint/checkers/utils.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,14 +1262,8 @@ def get_node_last_lineno(node: astroid.node_classes.NodeNG) -> int:
12621262

12631263
def is_postponed_evaluation_enabled(node: astroid.node_classes.NodeNG) -> bool:
12641264
"""Check if the postponed evaluation of annotations is enabled"""
1265-
name = "annotations"
12661265
module = node.root()
1267-
stmt = module.locals.get(name)
1268-
return (
1269-
stmt
1270-
and isinstance(stmt[0], astroid.ImportFrom)
1271-
and stmt[0].modname == "__future__"
1272-
)
1266+
return "annotations" in module.future_imports
12731267

12741268

12751269
def is_subclass_of(child: astroid.ClassDef, parent: astroid.ClassDef) -> bool:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# pylint: disable=missing-docstring,no-self-use,unused-argument,pointless-statement
2+
# pylint: disable=too-few-public-methods,no-name-in-module
3+
from __future__ import annotations as __annotations__
4+
5+
6+
class MyClass:
7+
@classmethod
8+
def from_string(cls, source) -> MyClass:
9+
...
10+
11+
def validate_b(self, obj: OtherClass) -> bool:
12+
...
13+
14+
15+
class OtherClass:
16+
...
17+
18+
19+
class Example:
20+
obj: Other
21+
22+
23+
class Other:
24+
...
25+
26+
27+
class ExampleSelf:
28+
next: ExampleSelf
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[testoptions]
2+
min_pyver=3.7

0 commit comments

Comments
 (0)