Skip to content
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

Eliminate side-effects from the ClientResponse.ok property #5404

Merged
merged 4 commits into from
Jan 14, 2021
Merged

Eliminate side-effects from the ClientResponse.ok property #5404

merged 4 commits into from
Jan 14, 2021

Conversation

adamko147
Copy link
Contributor

Make ClientResponse.ok property only check the self.status for 400+ status codes and reuse it in raise_for_status method

Are there changes in behavior for the user?

Yes. User will be allowed to use code

res = await session.get('url-returning-400-status')
if not res.ok:
    body = await res.text()

Related issue number

fixes #5403

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Jan 13, 2021
@webknjaz
Copy link
Member

@adamko147 your local Git is misconfigured: the commit author email doesn't match any emails verified on GitHub. You may want to fix that.

@codecov
Copy link

codecov bot commented Jan 13, 2021

Codecov Report

Merging #5404 (c48553c) into master (3401bef) will increase coverage by 0.40%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5404      +/-   ##
==========================================
+ Coverage   96.74%   97.15%   +0.40%     
==========================================
  Files          41       41              
  Lines        8736     8742       +6     
  Branches     1402     1402              
==========================================
+ Hits         8452     8493      +41     
+ Misses        149      130      -19     
+ Partials      135      119      -16     
Flag Coverage Δ
unit 97.04% <100.00%> (+0.33%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
aiohttp/client_reqrep.py 97.52% <100.00%> (+0.15%) ⬆️
aiohttp/cookiejar.py 98.78% <0.00%> (+<0.01%) ⬆️
aiohttp/web_request.py 95.71% <0.00%> (+0.01%) ⬆️
aiohttp/multipart.py 96.27% <0.00%> (+0.01%) ⬆️
aiohttp/streams.py 97.45% <0.00%> (+0.01%) ⬆️
aiohttp/payload.py 95.37% <0.00%> (+0.02%) ⬆️
aiohttp/web.py 97.43% <0.00%> (+0.02%) ⬆️
aiohttp/web_response.py 98.09% <0.00%> (+0.47%) ⬆️
aiohttp/http_parser.py 97.15% <0.00%> (+0.65%) ⬆️
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3401bef...c48553c. Read the comment docs.

@adamko147
Copy link
Contributor Author

@webknjaz thanks! Do I need to open new PR with correct email?

@Dreamsorcerer
Copy link
Member

I think we should be able to make a simple regression test in test_client_response.py to ensure this doesn't happen again.

@webknjaz
Copy link
Member

@adamko147 you can set up the correct name and email locally with git config --global user.name 'Adam Horacek' and git config --global user.email '[email protected]'. Then, do git commit --amend --reset-author. After this, your local and remote commits will differ and you'll need to git push --force-with-lease -u origin HEAD:fix-5403 so that it appears rewritten on GitHub. Updating a branch that is attached to a PR will automatically update the PR so you don't need to open a new one, just update your branch.

@adamko147
Copy link
Contributor Author

@webknjaz thanks, wasn't sure whether I'm able to do force-push. should be ok now

@webknjaz
Copy link
Member

wasn't sure whether I'm able to do force-push. should be ok now

The branch is in your own fork, you can do anything you please there with no restrictions unless you set up your fork to forbid things.

Co-authored-by: Sviatoslav Sydorenko <[email protected]>
@adamko147
Copy link
Contributor Author

Hi @Dreamsorcerer, I'm looking into the tests... most of the tests there mock the content.read method, however in this case mocking content.read doesn't seems to me right as the exception is raised there based on the content._exception
So I was thinking of test calling response.ok and assert that response.release() has not been called. Do you think it would be ok?

@Dreamsorcerer
Copy link
Member

Sounds good to me. Probably just a assert not resp.closed() will cover it.

@adamko147 adamko147 requested a review from webknjaz January 13, 2021 20:42
@@ -1243,3 +1243,22 @@ def test_response_links_empty(loop: Any, session: Any) -> None:
)
response._headers = CIMultiDict()
assert response.links == {}


def test_response_not_closed_after_get_ok() -> None:
Copy link
Member

@webknjaz webknjaz Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use mocker.spy() on response.raise_for_status method to verify that it wasn't called in addition to checking the side-effects.

https://github.com/pytest-dev/pytest-mock#spy

@webknjaz webknjaz changed the title ClientResponse.ok property only check the self.status for 400+ codes Eliminate side-effects from the ClientResponse.ok property Jan 14, 2021
@webknjaz webknjaz merged commit 3250c5d into aio-libs:master Jan 14, 2021
@webknjaz
Copy link
Member

Hm... Automatic backport failed for some obscure reason: https://github.com/aio-libs/aiohttp/runs/1701449441?check_suite_focus=true#step:3:31.

@adamko147 mind creating a backport PR to 3.8 using cherry_picker?
Ref: https://docs.aiohttp.org/en/latest/contributing.html#backporting

@adamko147 adamko147 deleted the fix-5403 branch January 14, 2021 14:25
webknjaz pushed a commit that referenced this pull request Feb 1, 2021
…5407)

This change makes it so accessing `ClientResponse.ok` only does the status
code check.

Prior to this commit, it'd call `ClientResponse.raise_for_status()` which
in turn, closed the underlying TCP session whenever the status was 400 or
higher making it effectively impossible to keep working with the response,
including reading the HTTP response payload.

PR #5404 by @adamko147

Fixes #5403

Co-authored-by: Sviatoslav Sydorenko <[email protected]>
(cherry picked from commit 3250c5d)

Co-authored-by: Adam Horacek <[email protected]>
alandtse pushed a commit to alandtse/aiohttp that referenced this pull request Feb 14, 2021
This change makes it so accessing `ClientResponse.ok` only does the status
code check.

Prior to this commit, it'd call `ClientResponse.raise_for_status()` which
in turn, closed the underlying TCP session whenever the status was 400 or
higher making it effectively impossible to keep working with the response,
including reading the HTTP response payload.

PR aio-libs#5404 by @adamko147

Fixes aio-libs#5403

Co-authored-by: Sviatoslav Sydorenko <[email protected]>
commonism pushed a commit to commonism/aiohttp that referenced this pull request Apr 27, 2021
This change makes it so accessing `ClientResponse.ok` only does the status
code check.

Prior to this commit, it'd call `ClientResponse.raise_for_status()` which
in turn, closed the underlying TCP session whenever the status was 400 or
higher making it effectively impossible to keep working with the response,
including reading the HTTP response payload.

PR aio-libs#5404 by @adamko147

Fixes aio-libs#5403

Co-authored-by: Sviatoslav Sydorenko <[email protected]>
commonism pushed a commit to commonism/aiohttp that referenced this pull request Apr 27, 2021
This change makes it so accessing `ClientResponse.ok` only does the status
code check.

Prior to this commit, it'd call `ClientResponse.raise_for_status()` which
in turn, closed the underlying TCP session whenever the status was 400 or
higher making it effectively impossible to keep working with the response,
including reading the HTTP response payload.

PR aio-libs#5404 by @adamko147

Fixes aio-libs#5403

Co-authored-by: Sviatoslav Sydorenko <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't read response content after checking response.ok
3 participants