|
10 | 10 |
|
11 | 11 | from pylint.checkers import BaseChecker |
12 | 12 | from pylint.checkers.utils import safe_infer |
| 13 | +from pylint.constants import DUNDER_METHODS |
13 | 14 | from pylint.interfaces import HIGH |
14 | 15 |
|
15 | 16 | if TYPE_CHECKING: |
16 | 17 | from pylint.lint import PyLinter |
17 | 18 |
|
18 | 19 |
|
19 | | -DUNDER_METHODS: dict[tuple[int, int], dict[str, str]] = { |
20 | | - (0, 0): { |
21 | | - "__init__": "Instantiate class directly", |
22 | | - "__del__": "Use del keyword", |
23 | | - "__repr__": "Use repr built-in function", |
24 | | - "__str__": "Use str built-in function", |
25 | | - "__bytes__": "Use bytes built-in function", |
26 | | - "__format__": "Use format built-in function, format string method, or f-string", |
27 | | - "__lt__": "Use < operator", |
28 | | - "__le__": "Use <= operator", |
29 | | - "__eq__": "Use == operator", |
30 | | - "__ne__": "Use != operator", |
31 | | - "__gt__": "Use > operator", |
32 | | - "__ge__": "Use >= operator", |
33 | | - "__hash__": "Use hash built-in function", |
34 | | - "__bool__": "Use bool built-in function", |
35 | | - "__getattr__": "Access attribute directly or use getattr built-in function", |
36 | | - "__getattribute__": "Access attribute directly or use getattr built-in function", |
37 | | - "__setattr__": "Set attribute directly or use setattr built-in function", |
38 | | - "__delattr__": "Use del keyword", |
39 | | - "__dir__": "Use dir built-in function", |
40 | | - "__get__": "Use get method", |
41 | | - "__set__": "Use set method", |
42 | | - "__delete__": "Use del keyword", |
43 | | - "__instancecheck__": "Use isinstance built-in function", |
44 | | - "__subclasscheck__": "Use issubclass built-in function", |
45 | | - "__call__": "Invoke instance directly", |
46 | | - "__len__": "Use len built-in function", |
47 | | - "__length_hint__": "Use length_hint method", |
48 | | - "__getitem__": "Access item via subscript", |
49 | | - "__setitem__": "Set item via subscript", |
50 | | - "__delitem__": "Use del keyword", |
51 | | - "__iter__": "Use iter built-in function", |
52 | | - "__next__": "Use next built-in function", |
53 | | - "__reversed__": "Use reversed built-in function", |
54 | | - "__contains__": "Use in keyword", |
55 | | - "__add__": "Use + operator", |
56 | | - "__sub__": "Use - operator", |
57 | | - "__mul__": "Use * operator", |
58 | | - "__matmul__": "Use @ operator", |
59 | | - "__truediv__": "Use / operator", |
60 | | - "__floordiv__": "Use // operator", |
61 | | - "__mod__": "Use % operator", |
62 | | - "__divmod__": "Use divmod built-in function", |
63 | | - "__pow__": "Use ** operator or pow built-in function", |
64 | | - "__lshift__": "Use << operator", |
65 | | - "__rshift__": "Use >> operator", |
66 | | - "__and__": "Use & operator", |
67 | | - "__xor__": "Use ^ operator", |
68 | | - "__or__": "Use | operator", |
69 | | - "__radd__": "Use + operator", |
70 | | - "__rsub__": "Use - operator", |
71 | | - "__rmul__": "Use * operator", |
72 | | - "__rmatmul__": "Use @ operator", |
73 | | - "__rtruediv__": "Use / operator", |
74 | | - "__rfloordiv__": "Use // operator", |
75 | | - "__rmod__": "Use % operator", |
76 | | - "__rdivmod__": "Use divmod built-in function", |
77 | | - "__rpow__": "Use ** operator or pow built-in function", |
78 | | - "__rlshift__": "Use << operator", |
79 | | - "__rrshift__": "Use >> operator", |
80 | | - "__rand__": "Use & operator", |
81 | | - "__rxor__": "Use ^ operator", |
82 | | - "__ror__": "Use | operator", |
83 | | - "__iadd__": "Use += operator", |
84 | | - "__isub__": "Use -= operator", |
85 | | - "__imul__": "Use *= operator", |
86 | | - "__imatmul__": "Use @= operator", |
87 | | - "__itruediv__": "Use /= operator", |
88 | | - "__ifloordiv__": "Use //= operator", |
89 | | - "__imod__": "Use %= operator", |
90 | | - "__ipow__": "Use **= operator", |
91 | | - "__ilshift__": "Use <<= operator", |
92 | | - "__irshift__": "Use >>= operator", |
93 | | - "__iand__": "Use &= operator", |
94 | | - "__ixor__": "Use ^= operator", |
95 | | - "__ior__": "Use |= operator", |
96 | | - "__neg__": "Multiply by -1 instead", |
97 | | - "__pos__": "Multiply by +1 instead", |
98 | | - "__abs__": "Use abs built-in function", |
99 | | - "__invert__": "Use ~ operator", |
100 | | - "__complex__": "Use complex built-in function", |
101 | | - "__int__": "Use int built-in function", |
102 | | - "__float__": "Use float built-in function", |
103 | | - "__round__": "Use round built-in function", |
104 | | - "__trunc__": "Use math.trunc function", |
105 | | - "__floor__": "Use math.floor function", |
106 | | - "__ceil__": "Use math.ceil function", |
107 | | - "__enter__": "Invoke context manager directly", |
108 | | - "__aenter__": "Invoke context manager directly", |
109 | | - "__copy__": "Use copy.copy function", |
110 | | - "__deepcopy__": "Use copy.deepcopy function", |
111 | | - "__fspath__": "Use os.fspath function instead", |
112 | | - }, |
113 | | - (3, 10): { |
114 | | - "__aiter__": "Use aiter built-in function", |
115 | | - "__anext__": "Use anext built-in function", |
116 | | - }, |
117 | | -} |
118 | | - |
119 | | - |
120 | 20 | class DunderCallChecker(BaseChecker): |
121 | 21 | """Check for unnecessary dunder method calls. |
122 | 22 |
|
123 | 23 | Docs: https://docs.python.org/3/reference/datamodel.html#basic-customization |
124 | | - We exclude __new__, __subclasses__, __init_subclass__, __set_name__, |
125 | | - __class_getitem__, __missing__, __exit__, __await__, |
126 | | - __aexit__, __getnewargs_ex__, __getnewargs__, __getstate__, |
127 | | - __setstate__, __reduce__, __reduce_ex__, |
128 | | - and __index__ (see https://github.com/PyCQA/pylint/issues/6795) |
| 24 | + We exclude names in list pylint.constants.EXTRA_DUNDER_METHODS such as |
| 25 | + __index__ (see https://github.com/PyCQA/pylint/issues/6795) |
129 | 26 | since these either have no alternative method of being called or |
130 | 27 | have a genuine use case for being called manually. |
131 | 28 |
|
|
0 commit comments