Soni L. wrote:
On 2020-04-09 8:48 a.m., Rhodri James wrote:
[Muchly snipped] On 09/04/2020 12:27, Soni L. wrote: To put it simple, unpacking raises ValueError: But if the iterator raises ValueError, there's no way to tell it apart from the unpacking: I don't see how this is any different from any other case when you get the same exception for different errors. If for some reason you really care, subclass ValueError to make a finer-grained exception. And the workaround for this is a bit ugly. We already convert e.g. StopIteration into RuntimeError in many cases, why can't we do so here too? Surely the correct "workaround" is not to do the thing that raises the exception? Technically, I consider it a bug that bugs can shadow API-level exceptions. Any API defining API-level exceptions must carefully control the exceptions it raises. In this case I'd like to use the ValueError from the iterator unpacking API, on my API. But since the iterator unpacking API doesn't control its exceptions, this just does a great job of masking bugs in the code instead. Equally, anything doing computation in __get__ should not propagate
LookupError except where explicitly intended. And that's not how those things are often implemented, unfortunately. There's a reason ppl complain so much about certain frameworks "eating all the exceptions". They use exceptions as part of their API but let user code raise those API-level exceptions, which, because they're part of the API, get handled somewhere.
Strictly speaking, there is any unpackaging error in your example. Your example raises its own ValueError before any unpackaging error is raised.
``` x, y = foo() ```
also raises your own `ValueError` and there is any unpackaging error involved.
On the other hand, an alternative design that does not raise any exception, does raise the proper unpackaging exception. For instance:
``` def foo(): yield True return
x = foo() x, = foo() x, y = foo()
``` Traceback (most recent call last): File "...", line 7, in <module> x, y = foo() ValueError: not enough values to unpack (expected 2, got 1) ```
So, IMHO, you are mixing two different things here. Am I wrong? Are you talking about something different? Thank you.