← index #1685Issue #3441
Related · high · value 3.512
QUERY · ISSUE

Exception.__init__ raises TypeError if overridden and called by subclass

openby mbueschopened 2015-12-05updated 2024-09-29
bugpy-core
>>> class A(Exception):
...     def __init__(self):
...         Exception.__init__(self)
... 
>>> a = A()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __init__
TypeError: argument should be a 'Exception' not a 'A'

Why is Exception being enforced here, even if A is a subclass of Exception? That makes it hard to subclass from Exception and override __init__.

CANDIDATE · ISSUE

Custom exceptions don't support keyword arguments

closedby larsksopened 2017-11-15updated 2025-06-10
proposed-closeneeds-info

I have code that looks like:

class MyException(Exception):
    def __init__(self, summary, details=None):
        self.summary = summary
        self.details = details


def error():
    raise MyException('oops', details='This is a test')

Calling the error function results in:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "extest.py", line 8, in error
TypeError: function does not take keyword arguments

I think this is mostly a documentation bug. The builtin types documentation has two similar but not quite identical entries:

I'm unclear if this is actually just a new expression of one of the existing cases, or if there should be a new entry along the lines of "Keyword arguments on user-defined exceptions are not supported".

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied