This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Unclear error message when raising wrong type of exceptions
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.7
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Dimitri Merejkowsky, berker.peksag, levkivskyi
Priority: normal Keywords: patch

Created on 2016-10-29 18:16 by Dimitri Merejkowsky, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
0001-Fix-error-message-when-raising-with-the-wrong-type.patch Dimitri Merejkowsky, 2016-10-29 18:16
Messages (2)
msg279689 - (view) Author: Dimitri Merejkowsky (Dimitri Merejkowsky) * Date: 2016-10-29 18:16
Motivation for the patch came from a tweet[1] of David Beazley:

def SomeError(Exception): 

raise SomeError('blah')

(Note the `def` keyword instead of `class`):

If you run that, you get:

> TypeError: exceptions must derive from BaseException

Which is not very helpful.

Attached patch changes the error message to be:

> TypeError: exceptions must derive from BaseException, got NoneType

(By the way, it's very close to what Python2 used to say in this case)
msg280238 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-11-07 20:49
Thanks for the patch, but I find your proposed message less clearer (including NoneType adds unnecessary confusion and people may ask "where did NoneType come from?")

Python 2 is different because of historical reasons (pre-BaseException era) so I don't think we should look at it in this case.

If other core developers give their +1s, the patch needs to be updated to add a test case (it can be added to Lib/test/ and use the Py_TYPE() macro instead of accessing exc->ob_type directly.
Date User Action Args
2022-04-11 14:58:38adminsetgithub: 72745
2021-12-10 09:50:22iritkatrielsetstatus: pending -> closed
resolution: rejected
stage: patch review -> resolved
2019-03-31 14:39:12serhiy.storchakasetstatus: open -> pending
2016-12-23 22:49:03levkivskyisetnosy: + levkivskyi
2016-11-07 20:49:36berker.peksagsetnosy: + berker.peksag

messages: + msg280238
stage: patch review
2016-10-29 18:16:31Dimitri Merejkowskycreate