On 2020-04-10 10:15 a.m., Chris Angelico wrote:
On Fri, Apr 10, 2020 at 8:05 PM Greg Ewing email@example.com wrote:
On 10/04/20 8:30 am, Soni L. wrote:
Sometimes, you have an API:
@abc.abstractmethod def get_property_value(self, prop): """Returns the value associated with the given property. Args: prop (DataProperty): The property. Returns: The value associated with the given property. Raises: PropertyError: If the property is not supported by this config source. LookupError: If the property is supported, but isn't
available. ValueError: If the property doesn't have exactly one value. """ raise PropertyError
This doesn't really look like a Python API. It's fairly rare in Python for exceptions to be used to indicate anything other than "something unexpected went wrong".
StopIteration wants to say hello :)
and look at what we ended up doing to that one! it now becomes RuntimeError in many places, because it was getting regularly swallowed previously.
if that's not an argument for this proposal, idk what is.
In your unpacking example, the ValueError isn't really intended as something to be caught under normal circumstances. The assumption is that you'll know how many items to expect when you unpack something, and if you don't get that many, then you have a bug.
You could advocate for unpacking to use a more specific exception to facilitate catching it, but I think most people will consider your use case to be quite rare.
Or alternatively, it may be worth separating the function call from the unpacking. But that's not common enough to really need to deal with.
fwiw if I raise ValueError it does indicate a bug in *your* code, and shouldn't be caught. so that was actually a bad example! sorry.
(altho I can imagine ppl trying to catch it, just because the API defines it... *sigh*)
ChrisA _______________________________________________ Python-ideas mailing list -- firstname.lastname@example.org To unsubscribe send an email to email@example.com https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://firstname.lastname@example.org/message/DJLKUO... Code of Conduct: http://python.org/psf/codeofconduct/