summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Mueller <30130371+cdce8p@users.noreply.github.com>2021-03-06 14:40:22 +0100
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-03-06 16:21:18 +0100
commitd554704641efba34ce0d3c22a3f91acf06dd814f (patch)
tree7ae4e3e1c05d1cb29bb127176e6e36b8287c98e9
parent947ee47c87d34abcd8982a61c12888e26111175d (diff)
downloadpylint-git-d554704641efba34ce0d3c22a3f91acf06dd814f.tar.gz
Add is_class_var utils function
-rw-r--r--pylint/checkers/base.py10
-rw-r--r--pylint/checkers/utils.py10
2 files changed, 12 insertions, 8 deletions
diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py
index 33aa8a037..393a433fd 100644
--- a/pylint/checkers/base.py
+++ b/pylint/checkers/base.py
@@ -1988,16 +1988,10 @@ class NameChecker(_BasicChecker):
elif isinstance(frame, astroid.ClassDef):
if not list(frame.local_attr_ancestors(node.name)):
for ancestor in frame.ancestors():
- if ( # pylint: disable=too-many-boolean-expressions
+ if (
ancestor.name == "Enum"
and ancestor.root().name == "enum"
- or isinstance(node.parent, astroid.AnnAssign)
- and (
- isinstance(node.parent.annotation, astroid.Subscript)
- and node.parent.annotation.value.name == "ClassVar"
- or isinstance(node.parent.annotation, astroid.Name)
- and node.parent.annotation.name == "ClassVar"
- )
+ or utils.is_class_var(node)
):
self._check_name("class_const", node.name, node)
break
diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py
index 29f774a75..37abc3a87 100644
--- a/pylint/checkers/utils.py
+++ b/pylint/checkers/utils.py
@@ -1459,3 +1459,13 @@ def is_attribute_typed_annotation(
):
return True
return False
+
+
+def is_class_var(node: astroid.AssignName) -> bool:
+ """Test if node has `ClassVar` annotation."""
+ return isinstance(node.parent, astroid.AnnAssign) and (
+ isinstance(node.parent.annotation, astroid.Subscript)
+ and node.parent.annotation.value.name == "ClassVar"
+ or isinstance(node.parent.annotation, astroid.Name)
+ and node.parent.annotation.name == "ClassVar"
+ )