I have a child class that is utilising Python 3.6+ __set_name__ to ensure that owning classes have annotated the type of the field carrying the child class. If they haven't an exception is raised.
However, any exception raised is always caught by Python and a RuntimeError raised instead.
For instance:
class Child:
def __set_name__(self, owner, name):
raise Exception("OOPS!")
class Owner():
child = Child()
Results in:
Traceback (most recent call last):
File "<stdin>", line 3, in __set_name__
Exception: OOPS!
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: Error calling __set_name__ on 'Child' instance 'child' in 'Owner'
This may well be expected behaviour (can't find a specific reference to __set_name__ exceptions), but maybe also suggests the expectation is that __set_name__ never suffers exception.
The behaviour I'm seeing isn't a problem, given an exception happens under the right conditions. However, it is tricky to test given I can't be sure the exception raised is the one my code raised.
Is there a better way of raising an exception that will lend itself to testing, or indeed a simple way to check the exception wrapped by the RuntimeError is indeed the one my code raised?