diff --git a/ChangeLog b/ChangeLog index cb8b79461a..5189034a26 100644 --- a/ChangeLog +++ b/ChangeLog @@ -18,6 +18,10 @@ Release date: TBA Close #3145 +* Exempt annotated assignments without variable from ``class-variable-slots-conflict`` + + Close #3141 + What's New in Pylint 2.4.1? =========================== diff --git a/pylint/checkers/classes.py b/pylint/checkers/classes.py index b8ff1712da..986809fd34 100644 --- a/pylint/checkers/classes.py +++ b/pylint/checkers/classes.py @@ -1135,9 +1135,14 @@ def _check_slots_elt(self, elt, node): "invalid-slots-object", args=inferred.as_string(), node=elt ) - # Check if we have a conflict with a class variable + # Check if we have a conflict with a class variable. class_variable = node.locals.get(inferred.value) if class_variable: + # Skip annotated assignments which don't conflict at all with slots. + if len(class_variable) == 1: + parent = class_variable[0].parent + if isinstance(parent, astroid.AnnAssign) and parent.value is None: + return self.add_message( "class-variable-slots-conflict", args=(inferred.value,), node=elt ) diff --git a/tests/functional/c/class_variable_slots_conflict_exempted.py b/tests/functional/c/class_variable_slots_conflict_exempted.py new file mode 100644 index 0000000000..b6a0edf448 --- /dev/null +++ b/tests/functional/c/class_variable_slots_conflict_exempted.py @@ -0,0 +1,4 @@ +# pylint: disable=missing-docstring,too-few-public-methods +class Example: + __slots__ = ["field"] + field: int