diff --git a/ChangeLog b/ChangeLog index 6fcce762ea..2775fb3ad7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -29,6 +29,11 @@ Release date: TBA Closes #5930 +* Fix false positive for ``unused-variable`` for classes inside functions + and where a metaclass is provided via a call. + + Closes #4020 + What's New in Pylint 2.13.7? ============================ diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst index c28b94023a..ffac2c2171 100644 --- a/doc/whatsnew/2.13.rst +++ b/doc/whatsnew/2.13.rst @@ -607,6 +607,11 @@ Other Changes Closes #5769 +* Fix false positive for ``unused-variable`` for classes inside functions + and where a metaclass is provided via a call. + + Closes #4020 + * Only raise ``not-callable`` when all the inferred values of a property are not callable. Closes #5931 diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 65432d8ca7..9b2b2bd7a0 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -2813,6 +2813,10 @@ def _check_classdef_metaclasses(self, klass, parent_node): while not isinstance(attr, nodes.Name): attr = attr.expr name = attr.name + elif isinstance(klass._metaclass, nodes.Call) and isinstance( + klass._metaclass.func, nodes.Name + ): + name = klass._metaclass.func.name elif metaclass: name = metaclass.root().name diff --git a/tests/functional/b/bugfix_local_scope_metaclass_1177.py b/tests/functional/b/bugfix_local_scope_metaclass_1177.py index 22c07a330f..06f36be522 100644 --- a/tests/functional/b/bugfix_local_scope_metaclass_1177.py +++ b/tests/functional/b/bugfix_local_scope_metaclass_1177.py @@ -20,6 +20,16 @@ class Class2(metaclass=Meta2): return Class2 +def func_scope_with_metaclass_from_call(): + def get_type(): + return type + + class Class2(metaclass=get_type()): + pass + + return Class2 + + class ClassScope: class Meta3(type): pass