Skip to content

Commit 0adf671

Browse files
authored
Remove old-style classes code, remove check for new-style class (#9925)
Python 3 is only new style classes.
1 parent 643d525 commit 0adf671

File tree

8 files changed

+70
-96
lines changed

8 files changed

+70
-96
lines changed

doc/user_guide/checkers/features.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,8 @@ Exceptions checker Messages
474474
:raising-bad-type (E0702): *Raising %s while only classes or instances are allowed*
475475
Used when something which is neither a class nor an instance is raised (i.e.
476476
a `TypeError` will be raised).
477-
:raising-non-exception (E0710): *Raising a new style class which doesn't inherit from BaseException*
478-
Used when a new style class which doesn't inherit from BaseException is
479-
raised.
477+
:raising-non-exception (E0710): *Raising a class which doesn't inherit from BaseException*
478+
Used when a class which doesn't inherit from BaseException is raised.
480479
:misplaced-bare-raise (E0704): *The raise statement is not inside an except clause*
481480
Used when a bare raise is not used inside an except clause. This generates an
482481
error, since there are no active exceptions to be reraised. An exception to

doc/whatsnew/fragments/9925.internal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Remove old-style classes (Python 2) code and remove check for new-style class since everything is new-style in Python 3. Updated doc for exception checker to remove reference to new style class.
2+
3+
Refs #9925

pylint/checkers/classes/class_checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,7 @@ def _check_in_slots(self, node: nodes.AssignAttr) -> None:
17781778
klass = inferred._proxied
17791779
if not has_known_bases(klass):
17801780
return
1781-
if "__slots__" not in klass.locals or not klass.newstyle:
1781+
if "__slots__" not in klass.locals:
17821782
return
17831783
# If `__setattr__` is defined on the class, then we can't reason about
17841784
# what will happen when assigning to an attribute.

pylint/checkers/exceptions.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,9 @@ def _is_raising(body: list[nodes.NodeNG]) -> bool:
9292
{"old_names": [("E0703", "bad-exception-context")]},
9393
),
9494
"E0710": (
95-
"Raising a new style class which doesn't inherit from BaseException",
95+
"Raising a class which doesn't inherit from BaseException",
9696
"raising-non-exception",
97-
"Used when a new style class which doesn't inherit from "
98-
"BaseException is raised.",
97+
"Used when a class which doesn't inherit from BaseException is raised.",
9998
),
10099
"E0711": (
101100
"NotImplemented raised - should raise NotImplementedError",
@@ -262,12 +261,11 @@ def visit_instance(self, instance: objects.ExceptionInstance) -> None:
262261

263262
def visit_classdef(self, node: nodes.ClassDef) -> None:
264263
if not utils.inherit_from_std_ex(node) and utils.has_known_bases(node):
265-
if node.newstyle:
266-
self._checker.add_message(
267-
"raising-non-exception",
268-
node=self._node,
269-
confidence=INFERENCE,
270-
)
264+
self._checker.add_message(
265+
"raising-non-exception",
266+
node=self._node,
267+
confidence=INFERENCE,
268+
)
271269

272270
def visit_tuple(self, _: nodes.Tuple) -> None:
273271
self._checker.add_message(

pylint/checkers/newstyle.py

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
from astroid import nodes
1313

1414
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
2016
from pylint.typing import MessageDefinitionTuple
2117

2218
if TYPE_CHECKING:
@@ -72,55 +68,51 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
7268
):
7369
continue
7470

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,))
124116

125117
visit_asyncfunctiondef = visit_functiondef
126118

tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_raised.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
# pylint:disable=too-few-public-methods,import-error,missing-docstring, not-callable, import-outside-toplevel
2-
"""test pb with exceptions and old/new style classes"""
2+
"""test pb with exceptions and classes"""
33

44

55
class ValidException(Exception):
66
"""Valid Exception."""
77

8-
class OldStyleClass:
9-
"""Not an exception."""
10-
118
class NewStyleClass:
129
"""Not an exception."""
1310

14-
1511
def good_case():
1612
"""raise"""
1713
raise ValidException('hop')
@@ -31,22 +27,10 @@ def good_case3():
3127
import io
3228
raise io.BlockingIOError
3329

34-
def bad_case0():
35-
"""raise"""
36-
# +2:<3.0:[nonstandard-exception]
37-
# +1:>=3.0:[raising-non-exception]
38-
raise OldStyleClass('hop')
39-
4030
def bad_case1():
4131
"""raise"""
4232
raise NewStyleClass() # [raising-non-exception]
4333

44-
def bad_case2():
45-
"""raise"""
46-
# +2:<3.0:[nonstandard-exception]
47-
# +1:>=3.0:[raising-non-exception]
48-
raise OldStyleClass('hop')
49-
5034
def bad_case3():
5135
"""raise"""
5236
raise NewStyleClass # [raising-non-exception]
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
raising-non-exception:38:4:38:30:bad_case0:Raising a new style class which doesn't inherit from BaseException:INFERENCE
2-
raising-non-exception:42:4:42:25:bad_case1:Raising a new style class which doesn't inherit from BaseException:INFERENCE
3-
raising-non-exception:48:4:48:30:bad_case2:Raising a new style class which doesn't inherit from BaseException:INFERENCE
4-
raising-non-exception:52:4:52:23:bad_case3:Raising a new style class which doesn't inherit from BaseException:INFERENCE
5-
notimplemented-raised:56:4:56:31:bad_case4:NotImplemented raised - should raise NotImplementedError:HIGH
6-
raising-bad-type:60:4:60:11:bad_case5:Raising int while only classes or instances are allowed:INFERENCE
7-
raising-bad-type:64:4:64:14:bad_case6:Raising NoneType while only classes or instances are allowed:INFERENCE
8-
raising-non-exception:68:4:68:14:bad_case7:Raising a new style class which doesn't inherit from BaseException:INFERENCE
9-
raising-non-exception:72:4:72:15:bad_case8:Raising a new style class which doesn't inherit from BaseException:INFERENCE
10-
raising-non-exception:76:4:76:14:bad_case9:Raising a new style class which doesn't inherit from BaseException:INFERENCE
11-
raising-bad-type:110:4:110:18:bad_case10:Raising str while only classes or instances are allowed:INFERENCE
1+
raising-non-exception:32:4:32:25:bad_case1:Raising a class which doesn't inherit from BaseException:INFERENCE
2+
raising-non-exception:36:4:36:23:bad_case3:Raising a class which doesn't inherit from BaseException:INFERENCE
3+
notimplemented-raised:40:4:40:31:bad_case4:NotImplemented raised - should raise NotImplementedError:HIGH
4+
raising-bad-type:44:4:44:11:bad_case5:Raising int while only classes or instances are allowed:INFERENCE
5+
raising-bad-type:48:4:48:14:bad_case6:Raising NoneType while only classes or instances are allowed:INFERENCE
6+
raising-non-exception:52:4:52:14:bad_case7:Raising a class which doesn't inherit from BaseException:INFERENCE
7+
raising-non-exception:56:4:56:15:bad_case8:Raising a class which doesn't inherit from BaseException:INFERENCE
8+
raising-non-exception:60:4:60:14:bad_case9:Raising a class which doesn't inherit from BaseException:INFERENCE
9+
raising-bad-type:94:4:94:18:bad_case10:Raising str while only classes or instances are allowed:INFERENCE
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
raising-non-exception:13:0:13:22::Raising a new style class which doesn't inherit from BaseException:INFERENCE
1+
raising-non-exception:13:0:13:22::Raising a class which doesn't inherit from BaseException:INFERENCE

0 commit comments

Comments
 (0)