Skip to content

Commit 40472dd

Browse files
jabilevkivskyi
authored andcommitted
bpo-33018: Improve issubclass() error checking and message. (GH-5944)
This improves error message for situations when a non-class is checked w.r.t. an abstract base class.
1 parent d93b516 commit 40472dd

File tree

4 files changed

+11
-0
lines changed

4 files changed

+11
-0
lines changed

Lib/_py_abc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ def __instancecheck__(cls, instance):
107107

108108
def __subclasscheck__(cls, subclass):
109109
"""Override for issubclass(subclass, cls)."""
110+
if not isinstance(subclass, type):
111+
raise TypeError('issubclass() arg 1 must be a class')
110112
# Check cache
111113
if subclass in cls._abc_cache:
112114
return True

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ Dillon Brock
202202
Richard Brodie
203203
Michael Broghton
204204
Ammar Brohi
205+
Josh Bronson
205206
Daniel Brotsky
206207
Jean Brouwers
207208
Gary S. Brown
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improve consistency of errors raised by ``issubclass()`` when called with a
2+
non-class and an abstract base class as the first and second arguments,
3+
respectively. Patch by Josh Bronson.

Modules/_abc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,11 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
569569
PyObject *subclass)
570570
/*[clinic end generated code: output=b56c9e4a530e3894 input=1d947243409d10b8]*/
571571
{
572+
if (!PyType_Check(subclass)) {
573+
PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class");
574+
return NULL;
575+
}
576+
572577
PyObject *ok, *mro = NULL, *subclasses = NULL, *result = NULL;
573578
Py_ssize_t pos;
574579
int incache;

0 commit comments

Comments
 (0)