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 :)
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.