Skip to content

Commit

Permalink
Fix false-positive for unused-import on class keyword arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored and Pierre-Sassoulas committed Mar 28, 2021
1 parent 12acc43 commit 7905ec1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ Release date: TBA

* Improve check if class is subscriptable PEP585

* Fix false-positive for ``unused-import`` on class keyword arguments

Closes #3202


What's New in Pylint 2.7.2?
===========================
Expand Down
8 changes: 8 additions & 0 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,14 @@ def visit_name(self, node):
):
continue

# Ignore inner class scope for keywords in class definition
if (
current_consumer.scope_type == "class"
and isinstance(node.parent, astroid.Keyword)
and isinstance(node.parent.parent, astroid.ClassDef)
):
continue

# if the name node is used as a function default argument's value or as
# a decorator, then start from the parent frame of the function instead
# of the function frame - and thus open an inner class scope
Expand Down
38 changes: 38 additions & 0 deletions tests/functional/u/unused/unused_import_class_def_keyword.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Test false-positive for unused-import on class keyword arguments
https://github.com/PyCQA/pylint/issues/3202
"""
# pylint: disable=missing-docstring,too-few-public-methods,invalid-name,import-error

# Imports don't exist! Only check `unused-import`
from const import DOMAIN
from const import DOMAIN_2
from const import DOMAIN_3


class Child:
def __init_subclass__(cls, **kwargs):
pass

class Parent(Child, domain=DOMAIN):
pass


# Alternative 1
class Parent_2(Child, domain=DOMAIN_2):
DOMAIN_2 = DOMAIN_2


# Alternative 2
class A:
def __init__(self, arg):
pass

class B:
CONF = "Hello World"
SCHEMA = A(arg=CONF)


# Test normal instantiation
A(arg=DOMAIN_3)

0 comments on commit 7905ec1

Please sign in to comment.