Exception that calls `super().__init__` breaks `copy.copy`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
For some reason calling Exception.__init__ breaks copy.copys ability to detect args. The following code
import copy
class MyException(Exception):
def __init__(self, x):
super().__init__()
self.x = x
my_exc = MyException(5)
copy.copy(my_exc)
gives
Traceback (most recent call last):
File "./foo.py", line 9, in <module>
copy.copy(my_exc)
File "/usr/lib/python3.12/copy.py", line 97, in copy
return _reconstruct(x, None, *rv)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/copy.py", line 253, in _reconstruct
y = func(*args)
^^^^^^^^^^^
TypeError: MyException.__init__() missing 1 required positional argument: 'x'
Removing the super().__init__() line makes everything work as expected, but given that it's generally seen as good practice to call super().__init__() in child classes, and you may be working with a more complex class structure, this seems like pretty bad behavior.
I haven't dug deep enough into copy.copy or BaseException.__init__ to decide which one deserves the blame.
For the reason why I want to copy exceptions, see https://github.com/python-trio/flake8-async/issues/298
CPython versions tested on:
3.9, 3.10, 3.11, 3.12, 3.13, 3.14
Operating systems tested on:
Linux
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the example with copy.copy and inspect the behavior in /usr/lib/python3.12/copy.py alongside BaseException.init. Determine the expected copying behavior for an exception whose initializer calls super().init(), then add a regression test showing that copying preserves the required constructor state without raising TypeError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100