Skip to content

Commit 7905ec1

Browse files
cdce8pPierre-Sassoulas
authored andcommitted
Fix false-positive for unused-import on class keyword arguments
1 parent 12acc43 commit 7905ec1

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

ChangeLog

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

7878
* Improve check if class is subscriptable PEP585
7979

80+
* Fix false-positive for ``unused-import`` on class keyword arguments
81+
82+
Closes #3202
83+
8084

8185
What's New in Pylint 2.7.2?
8286
===========================

pylint/checkers/variables.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,14 @@ def visit_name(self, node):
976976
):
977977
continue
978978

979+
# Ignore inner class scope for keywords in class definition
980+
if (
981+
current_consumer.scope_type == "class"
982+
and isinstance(node.parent, astroid.Keyword)
983+
and isinstance(node.parent.parent, astroid.ClassDef)
984+
):
985+
continue
986+
979987
# if the name node is used as a function default argument's value or as
980988
# a decorator, then start from the parent frame of the function instead
981989
# of the function frame - and thus open an inner class scope
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Test false-positive for unused-import on class keyword arguments
3+
4+
https://github.com/PyCQA/pylint/issues/3202
5+
"""
6+
# pylint: disable=missing-docstring,too-few-public-methods,invalid-name,import-error
7+
8+
# Imports don't exist! Only check `unused-import`
9+
from const import DOMAIN
10+
from const import DOMAIN_2
11+
from const import DOMAIN_3
12+
13+
14+
class Child:
15+
def __init_subclass__(cls, **kwargs):
16+
pass
17+
18+
class Parent(Child, domain=DOMAIN):
19+
pass
20+
21+
22+
# Alternative 1
23+
class Parent_2(Child, domain=DOMAIN_2):
24+
DOMAIN_2 = DOMAIN_2
25+
26+
27+
# Alternative 2
28+
class A:
29+
def __init__(self, arg):
30+
pass
31+
32+
class B:
33+
CONF = "Hello World"
34+
SCHEMA = A(arg=CONF)
35+
36+
37+
# Test normal instantiation
38+
A(arg=DOMAIN_3)

0 commit comments

Comments
 (0)