On 2020-05-01 3:48 a.m., Ram Rachum wrote:
Hi,
Here's something I wanted in Python for many years. If this has been discussed in the past, please refer me to that discussion.
On one hand, it's something that I can't imagine the python-dev community supporting. On the other hand, it would maintain backward compatibility.
I wish there were a 100 more built-in exceptions in Python, that will be very specific about what went wrong.
If I do this:
>>> x, y = range(3)
I know it'll raise a ValueError, because I've memorized that, but it did take me a few years to remember where I should expect ValueError and where I should expect TypeError.
It would be nice if the operation above raised UnpackingOverflowError, which will be a subclass of UnpackingError, along with UnpackingUnderflowError. UnpackingError can be a subclass of ValueError, for backward compatibility.
oh yes please let me distinguish the exception from the unpacking and the exception from the iterator def foo(): yield from () raise ValueError it = foo() try: x, y = it except ValueError: print("Is this a problem with the unpacking or a problem with the generator?") raise also please have unpacking wrap any UnpackingError from the generator into a RuntimeError.
Similarly, if I did this:
>>> def f(x, y): return x + y >>> f(1)
I would get a TypeError. Would be a lot cooler if I got MissingArgumentsError, which would be a subclass of SignatureError, which would be a subclass of TypeError.
There are 2 reasons I want this:
1. When I'm writing a try..except clause, I want to catch a specific exception like MissingArgumentsError rather than ValueError or TypeError. They're too ubiquitous. I don't want some other unexpected failure producing the same ValueError and triggering my except clause.
2. When I get an error, especially from some shitty corporate system that truncates the traceback, I want to get as many hints as possible about what went wrong.
It's true that today, most Python exceptions have good text in their message, like "TypeError: f() missing 1 required positional argument: 'y'". But that isn't guaranteed everywhere, and specific exception types could help.
What do you think?
Ram.
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/XZXWXI... Code of Conduct: http://python.org/psf/codeofconduct/