|
12 | 12 | from astroid import nodes |
13 | 13 |
|
14 | 14 | from pylint.checkers import BaseChecker |
15 | | -from pylint.checkers.utils import ( |
16 | | - has_known_bases, |
17 | | - node_frame_class, |
18 | | - only_required_for_messages, |
19 | | -) |
| 15 | +from pylint.checkers.utils import node_frame_class, only_required_for_messages |
20 | 16 | from pylint.typing import MessageDefinitionTuple |
21 | 17 |
|
22 | 18 | if TYPE_CHECKING: |
@@ -72,55 +68,51 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None: |
72 | 68 | ): |
73 | 69 | continue |
74 | 70 |
|
75 | | - # super should not be used on an old style class |
76 | | - if klass.newstyle or not has_known_bases(klass): |
77 | | - # super first arg should not be the class |
78 | | - if not call.args: |
79 | | - continue |
80 | | - |
81 | | - # calling super(type(self), self) can lead to recursion loop |
82 | | - # in derived classes |
83 | | - arg0 = call.args[0] |
84 | | - if ( |
85 | | - isinstance(arg0, nodes.Call) |
86 | | - and isinstance(arg0.func, nodes.Name) |
87 | | - and arg0.func.name == "type" |
88 | | - ): |
89 | | - self.add_message("bad-super-call", node=call, args=("type",)) |
90 | | - continue |
91 | | - |
92 | | - # calling super(self.__class__, self) can lead to recursion loop |
93 | | - # in derived classes |
94 | | - if ( |
95 | | - len(call.args) >= 2 |
96 | | - and isinstance(call.args[1], nodes.Name) |
97 | | - and call.args[1].name == "self" |
98 | | - and isinstance(arg0, nodes.Attribute) |
99 | | - and arg0.attrname == "__class__" |
100 | | - ): |
101 | | - self.add_message( |
102 | | - "bad-super-call", node=call, args=("self.__class__",) |
103 | | - ) |
104 | | - continue |
105 | | - |
106 | | - try: |
107 | | - supcls = call.args and next(call.args[0].infer(), None) |
108 | | - except astroid.InferenceError: |
109 | | - continue |
110 | | - |
111 | | - # If the supcls is in the ancestors of klass super can be used to skip |
112 | | - # a step in the mro() and get a method from a higher parent |
113 | | - if klass is not supcls and all(i != supcls for i in klass.ancestors()): |
114 | | - name = None |
115 | | - # if supcls is not Uninferable, then supcls was inferred |
116 | | - # and use its name. Otherwise, try to look |
117 | | - # for call.args[0].name |
118 | | - if supcls: |
119 | | - name = supcls.name |
120 | | - elif call.args and hasattr(call.args[0], "name"): |
121 | | - name = call.args[0].name |
122 | | - if name: |
123 | | - self.add_message("bad-super-call", node=call, args=(name,)) |
| 71 | + # super first arg should not be the class |
| 72 | + if not call.args: |
| 73 | + continue |
| 74 | + |
| 75 | + # calling super(type(self), self) can lead to recursion loop |
| 76 | + # in derived classes |
| 77 | + arg0 = call.args[0] |
| 78 | + if ( |
| 79 | + isinstance(arg0, nodes.Call) |
| 80 | + and isinstance(arg0.func, nodes.Name) |
| 81 | + and arg0.func.name == "type" |
| 82 | + ): |
| 83 | + self.add_message("bad-super-call", node=call, args=("type",)) |
| 84 | + continue |
| 85 | + |
| 86 | + # calling super(self.__class__, self) can lead to recursion loop |
| 87 | + # in derived classes |
| 88 | + if ( |
| 89 | + len(call.args) >= 2 |
| 90 | + and isinstance(call.args[1], nodes.Name) |
| 91 | + and call.args[1].name == "self" |
| 92 | + and isinstance(arg0, nodes.Attribute) |
| 93 | + and arg0.attrname == "__class__" |
| 94 | + ): |
| 95 | + self.add_message("bad-super-call", node=call, args=("self.__class__",)) |
| 96 | + continue |
| 97 | + |
| 98 | + try: |
| 99 | + supcls = call.args and next(call.args[0].infer(), None) |
| 100 | + except astroid.InferenceError: |
| 101 | + continue |
| 102 | + |
| 103 | + # If the supcls is in the ancestors of klass super can be used to skip |
| 104 | + # a step in the mro() and get a method from a higher parent |
| 105 | + if klass is not supcls and all(i != supcls for i in klass.ancestors()): |
| 106 | + name = None |
| 107 | + # if supcls is not Uninferable, then supcls was inferred |
| 108 | + # and use its name. Otherwise, try to look |
| 109 | + # for call.args[0].name |
| 110 | + if supcls: |
| 111 | + name = supcls.name |
| 112 | + elif call.args and hasattr(call.args[0], "name"): |
| 113 | + name = call.args[0].name |
| 114 | + if name: |
| 115 | + self.add_message("bad-super-call", node=call, args=(name,)) |
124 | 116 |
|
125 | 117 | visit_asyncfunctiondef = visit_functiondef |
126 | 118 |
|
|
0 commit comments