-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
verify() 's API is ambiguous #55
Comments
(there is no |
Damn, I keep forgetting about that. And Bool isn't subclassable, so I guess there would be no way to raise on That being said, #51 will be quite an API break too, so for that matter, not returning True anymore could be done at the same time. |
Warehouse has a defensive workaround for this, in pypi/warehouse#11885. I have no strong preference for a return or raise-style API, as long as it's consistent 🙂, but to prolong the bikeshedding a little more: I'm personally a fan of In other words, a hypothetical breaking API could look something like this: Status = Union[Success, Failure]
def verify(macaroon, key) -> Status:
... ...where |
This stems from pypi/warehouse#8562 and the downtime experienced in PyPI due to my own misunderstanding of the verify API.
So here's the thing: there are 2 pythonic ways to answer the question "was there a problem?":
True
(no problem) orFalse
(a problem). This one is simple and lacks expressivity.raise
. This one is more precise because we get to tell exactly how this failed (cf Verifier.verify raises very general exceptions #51 ). As the zen of python says, "errors should never pass silently".verify's API (as far as I can tell) is:
True
orraise
.This means:
try/except
block will result in crashesLooking at the code:
pymacaroons/pymacaroons/verifier.py
Line 80 in abc36a6
There is a single return value in the method and it returns the constant value
True
, so nothing else can ever be returned from this function, meaning theTrue
bears no information, it could well have been aNone
, or no return value.I think the best solution could be to stop advocating that there is a return value for the verify method, and document the use of wrapping it in a
try/except
(#51 will also be key to doing this). This way the code doesn't even have to change and it can continue returning True for backwards compatibility for as long as you wish. If you ever want to remove it, you can even return an object whose__bool__
method raises aDeprecationWarning
and returnsTrue
(and also ideally override__is__
in case people wroteif verify() is True
.As of today, the README example doesn't wrap the verify() call in a try/except so it can be misleading. Also, it seems the functionnal tests that are advertised as documentation examples do only cover cases where this returns True.
The text was updated successfully, but these errors were encountered: