ApprovalTests.Python icon indicating copy to clipboard operation
ApprovalTests.Python copied to clipboard

Infinite recursion when repr'ing Exception types

Open apmorton opened this issue 3 months ago • 1 comments

https://github.com/approvals/ApprovalTests.Python/blob/4f6b9ee9d559204e88f47ee66de1ffb1ba3812d8/approvaltests/approval_exception.py#L4-L11

>>> repr(approvaltests.ApprovalException('hello'))

RecursionError                            Traceback (most recent call last)
Cell In[4], line 1
----> 1 repr(approvaltests.ApprovalException('hello'))

RecursionError: maximum recursion depth exceeded while getting the repr of an object

ReporterMissingException also suffers from this.

https://github.com/search?q=repo%3Aapprovals%2FApprovalTests.Python%20super().init(self)&type=code

The pythonic way to write exceptions like this would be:

class MyException(Exception):
    pass

You can then format the message at the raise-site, or use a static-method.

If you override the constructor signature of an exception you also need to worry about implementing __reduce__ correctly, as the built-in default for Exception will very likely be incorrect.

apmorton avatar Sep 23 '25 14:09 apmorton

Thanks for bringing this to our attention!

JayBazuzi avatar Sep 23 '25 17:09 JayBazuzi