Skip to content

Commit

Permalink
Prevent redefined-outer-name for if t.TYPE_CHECKING
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls authored and Pierre-Sassoulas committed Oct 10, 2022
1 parent 2b89c7c commit 5c22a79
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/7524.bugfix
@@ -0,0 +1,4 @@
Fix false positive for ``redefined-outer-name`` when aliasing ``typing``
e.g. as ``t`` and guarding imports under ``t.TYPE_CHECKING``.

Closes #7524
4 changes: 1 addition & 3 deletions pylint/checkers/variables.py
Expand Up @@ -1222,9 +1222,7 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
# Do not take in account redefined names for the purpose
# of type checking.:
if any(
isinstance(definition.parent, nodes.If)
and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS
for definition in globs[name]
in_type_checking_block(definition) for definition in globs[name]
):
continue

Expand Down
12 changes: 10 additions & 2 deletions tests/functional/r/redefined/redefined_outer_name_type_checking.py
Expand Up @@ -3,19 +3,27 @@
from __future__ import annotations

from typing import TYPE_CHECKING
import typing as t


class Cls:
def func(self, stuff: defaultdict):
# This import makes the definition work.
def func(self, stuff: defaultdict, my_deque: deque):
# These imports make the definition work.
# pylint: disable=import-outside-toplevel
from collections import defaultdict
from collections import deque

obj = defaultdict()
obj2 = deque()
obj.update(stuff)
obj2.append(my_deque)
return obj


if TYPE_CHECKING:
# This import makes the annotations work.
from collections import defaultdict

if t.TYPE_CHECKING:
# This import makes the annotations work.
from collections import deque

0 comments on commit 5c22a79

Please sign in to comment.