← index #1685Issue #400
Related · high · value 1.771
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

Problems with constructors of subclassed C-level classes

closedby pfalconopened 2014-03-30updated 2014-05-10
class MyError(Exception):
    pass

e = MyError()
e2 = MyError("foobar")

Traceback (most recent call last):
  File "exc-sub.py", line 5, in <module>
TypeError: function takes 0 positional arguments but 1 were given

Exception is for 2nd instantiation line. Superclass constructor has following signature: Exception.init(*args).

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