Title: Unclear error message when raising wrong type of exceptions
Type: enhancement Stage: patch review
Components: Interpreter Core Versions: Python 3.7
Status: pending Resolution:
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 2019-03-31 14:39 by serhiy.storchaka.

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
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