Skip to content

Commit

Permalink
Handle redefinitions in case of type checking imports.
Browse files Browse the repository at this point in the history
Close #2834
  • Loading branch information
PCManticore committed Sep 17, 2019
1 parent 8bf8fe1 commit 344f685
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -16,6 +16,10 @@ Release date: TBA

Close #2540

* Handle redefinitions in case of type checking imports.

Close #2834

* Added a new check, ``consider-using-sys-exit``

This check is emitted when we detect that a quit() or exit() is invoked
Expand Down
9 changes: 9 additions & 0 deletions pylint/checkers/variables.py
Expand Up @@ -942,6 +942,15 @@ def visit_functiondef(self, node):
# It is a __future__ directive, not a symbol.
continue

# Do not take in account redefined names for the purpose
# of type checking.:
if any(
isinstance(definition.parent, astroid.If)
and definition.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS
for definition in globs[name]
):
continue

line = definition.fromlineno
if not self._is_name_ignored(stmt, name):
self.add_message(
Expand Down
20 changes: 20 additions & 0 deletions tests/functional/r/redefined_outer_name_type_checking.py
@@ -0,0 +1,20 @@
# pylint: disable=missing-module-docstring,missing-class-docstring,too-few-public-methods
# pylint: disable=no-self-use,missing-function-docstring
from __future__ import annotations

from typing import TYPE_CHECKING


class Cls:
def func(self, stuff: defaultdict):
# This import makes the definition work.
from collections import defaultdict

obj = defaultdict()
obj.update(stuff)
return obj


if TYPE_CHECKING:
# This import makes the annotations work.
from collections import defaultdict
2 changes: 2 additions & 0 deletions tests/functional/r/redefined_outer_name_type_checking.rc
@@ -0,0 +1,2 @@
[testoptions]
min_pyver=3.7

0 comments on commit 344f685

Please sign in to comment.