-
-
Notifications
You must be signed in to change notification settings - Fork 306
/
Copy pathdunder_attributes.py
39 lines (32 loc) · 1.47 KB
/
dunder_attributes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Copyright (c) 2018,2023 Alexander Todorov <atodorov@MrSenko.com>
# Licensed under the GPL 2.0: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
import astroid
from pylint import checkers
from pylint.checkers import utils
class DunderClassAttributeChecker(checkers.BaseChecker):
name = "dunder-class-attribute-checker"
msgs = {
"C4401": (
"Class attributes should not contain double underscores",
"dunder-class-attribute",
'Dunders, e.g. "__some_name__", are reserved for Python. '
"Do not name your class attributes this way!",
)
}
@utils.only_required_for_messages("dunder-class-attribute")
def visit_classdef(self, node):
"""Detect when class attributes use double underscores."""
# we can redefine special methods (e.g. __iter__) and some attributes,
# e.g. __doc__, by declaring them as class attributes. Exclude them from the
# test below.
allowed_attributes = dir([])
for child in node.body:
if isinstance(child, astroid.Assign):
for target in child.targets:
if (
isinstance(target, astroid.AssignName)
and target.name not in allowed_attributes
and target.name.startswith("__")
and target.name.endswith("__")
):
self.add_message("dunder-class-attribute", node=child)