-
Notifications
You must be signed in to change notification settings - Fork 950
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
Remove passing exception as args to super in APIError #1477
Remove passing exception as args to super in APIError #1477
Conversation
2491328
to
e0d3187
Compare
The lint is failing for unrelated reasons. Please bear with us while #1478 is solved :) |
Hi @mike-flowers-airbnb thank you for this improvement! I need to run a couple of tests first. As you remove the use of the function |
Sure thing, removed the obsolete function |
@mike-flowers-airbnb problem should be solved now, please rebase your branch on laster commit on master branch in order to pass the CI. |
b053bf7
to
774944c
Compare
✅ Done , thanks! |
Remove obsolete function and typing annotations
774944c
to
0469541
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi I ran som regression tests. I found an issue in the current design:
- current a user is allowed to do something like this:
try:
sheet = file.del_worksheet_by_id(112341462553536987)
except gspread.exceptions.APIError as e:
pprint(e.args[0]["status"])
In our current code base, this will return:
- the first argument passed to the
Exception
parent class - this argument is a
dict
, so it will look for the keycode
in thisdict
. - This returns the error status returned by the API.
With your current implementation we face the following error with the above code:
Traceback (most recent call last):
File "/..../exception_test.py", line 10, in <module>
print(e.args[0]["code"])
TypeError: 'Response' object is not subscriptable
This is because the first argument passed to the Exception
parent class is no longer a dict but a Response
object.
I tried the below code that would allow us to make the changes and keep the old behavior and prevent regression.
class APIError(GSpreadException):
"""Errors coming from the API itself,
such as when we attempt to retrieve things that don't exist."""
def __init__(self, response: Response):
super().__init__(response)
self.response: Response = response
self.error: Mapping[str, Any] = response.json()["error"]
self.code: int = self.error["code"]
@property
def args(self):
return [dict(self.error)]
the above @property
on args
will allow us to catch anyone trying to access the args
attribute and return the error object as a dict.
@alifeee what do you think of this workaround ?
apart from that, which could introduce a breaking change, I am fine with the changes.
Appreciate the callout here @lavigne958 . Definitely don't want to be breaking exception catch flows. I did some searching and found that there is actually a proper form to expose the ability for pickling to work when args don't match. I had incorrectly assumed that args were supposed to match the underlying |
that's a great news, if I understand correctly your point, you don't need these changes anymore from gspread right ? let me know if that's ok with you and I'll put your issue in the next major release milestone. |
Sorry I mean that I can change this PR so that the exception implements |
I just pushed a new commit to implement it. You'll notice that the |
thank you for the prompt reply, I'll have a look and test it. looks qiet good to me ! |
@lavigne958 , hey there, just checking in to see if anything else is needed for this PR. Thanks. |
Hi so far what is mostly needed is "time", I didn't have any time recently to take a look. With the coming days I will have more time to take a look at it. Sorry for the delay. |
No worries, definitely understand, thanks for the update! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good to me, with regression test as extra ! thank you.
Only a small detail about the indentation just in case then: looks good to merge
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
First, thanks for this tool!
We have a pipeline where exceptions from packages such as
gspread
are pickled and stored for further processing. Unfortunately, theAPIError
exception is not able to be unpickled as the args it passed to theException
super are different from what is passed into the exception itself.The sample test extension exposes this error as seen below:
This PR ultimately only replaces the extracted error with the raw response passed to the kwargs. Given that
__str__
and__repr__
are overloaded and all other usage of this exception use the other members, there is no visible impact from this change.