Skip to content

Commit 5c22a79

Browse files
jacobtylerwallsPierre-Sassoulas
authored andcommitted
Prevent redefined-outer-name for if t.TYPE_CHECKING
1 parent 2b89c7c commit 5c22a79

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

doc/whatsnew/fragments/7524.bugfix

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix false positive for ``redefined-outer-name`` when aliasing ``typing``
2+
e.g. as ``t`` and guarding imports under ``t.TYPE_CHECKING``.
3+
4+
Closes #7524

pylint/checkers/variables.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1222,9 +1222,7 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
12221222
# Do not take in account redefined names for the purpose
12231223
# of type checking.:
12241224
if any(
1225-
isinstance(definition.parent, nodes.If)
1226-
and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS
1227-
for definition in globs[name]
1225+
in_type_checking_block(definition) for definition in globs[name]
12281226
):
12291227
continue
12301228

tests/functional/r/redefined/redefined_outer_name_type_checking.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,27 @@
33
from __future__ import annotations
44

55
from typing import TYPE_CHECKING
6+
import typing as t
67

78

89
class Cls:
9-
def func(self, stuff: defaultdict):
10-
# This import makes the definition work.
10+
def func(self, stuff: defaultdict, my_deque: deque):
11+
# These imports make the definition work.
1112
# pylint: disable=import-outside-toplevel
1213
from collections import defaultdict
14+
from collections import deque
1315

1416
obj = defaultdict()
17+
obj2 = deque()
1518
obj.update(stuff)
19+
obj2.append(my_deque)
1620
return obj
1721

1822

1923
if TYPE_CHECKING:
2024
# This import makes the annotations work.
2125
from collections import defaultdict
26+
27+
if t.TYPE_CHECKING:
28+
# This import makes the annotations work.
29+
from collections import deque

0 commit comments

Comments
 (0)