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

[requests] Loosen HTTPError's Response type #10766

Closed
wants to merge 1 commit into from

Conversation

akx
Copy link
Contributor

@akx akx commented Sep 25, 2023

requests itself allows a None type for Response; it will internally attempt to read it from the request passed.

There is thus no strict guarantee that a HTTPError will have a truthy request.

Fixes #10764
Follows up on #10740

See https://github.com/psf/requests/blob/881281250f74549f560408e5546d95a8cd73ce28/src/requests/exceptions.py#L17-L24 for the relevant inference code.

`requests` itself allows a `None` type for `Response`;
it will internally attempt to read it from the `request` passed.

There is thus no strict guarantee that a `HTTPError`
will have a truthy `request`.

Fixes python#10764
@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

cloud-init (https://github.com/canonical/cloud-init)
+ cloudinit/url_helper.py:343: error: Item "None" of "Response | None" has no attribute "status_code"  [union-attr]
+ cloudinit/url_helper.py:344: error: Item "None" of "Response | None" has no attribute "headers"  [union-attr]
- tests/unittests/sources/azure/test_errors.py:169: error: Missing named argument "response" for "HTTPError"  [call-arg]

paasta (https://github.com/yelp/paasta)
- paasta_tools/mesos_maintenance.py:213: error: Missing named argument "response" for "HTTPError"  [call-arg]

poetry (https://github.com/python-poetry/poetry)
+ src/poetry/publishing/uploader.py:35: error: Item "None" of "Response | None" has no attribute "status_code"  [union-attr]
+ src/poetry/publishing/uploader.py:35: error: Item "None" of "Response | None" has no attribute "reason"  [union-attr]
+ src/poetry/publishing/uploader.py:35: error: Item "None" of "Response | None" has no attribute "content"  [union-attr]

twine (https://github.com/pypa/twine)
+ twine/__main__.py:36: error: Item "None" of "Response | None" has no attribute "status_code"  [union-attr]
+ twine/__main__.py:39: error: Item "None" of "Response | None" has no attribute "url"  [union-attr]
+ twine/__main__.py:39: error: Item "None" of "Response | None" has no attribute "reason"  [union-attr]

materialize (https://github.com/MaterializeInc/materialize)
+ misc/python/materialize/cloudtest/util/web_request.py:27: error: Item "None" of "Response | None" has no attribute "status_code"  [union-attr]
+ misc/python/materialize/cloudtest/util/web_request.py:28: error: Item "None" of "Response | None" has no attribute "reason"  [union-attr]
+ misc/python/materialize/cloudtest/util/web_request.py:29: error: Item "None" of "Response | None" has no attribute "content"  [union-attr]
+ misc/python/materialize/cloudtest/util/web_request.py:70: error: Item "None" of "Response | None" has no attribute "status_code"  [union-attr]
+ misc/python/materialize/cloudtest/util/web_request.py:102: error: Item "None" of "Response | None" has no attribute "status_code"  [union-attr]
+ misc/python/materialize/cloudtest/util/web_request.py:134: error: Item "None" of "Response | None" has no attribute "status_code"  [union-attr]
+ misc/python/materialize/cloudtest/util/web_request.py:172: error: Item "None" of "Response | None" has no attribute "status_code"  [union-attr]

schemathesis (https://github.com/schemathesis/schemathesis)
+ src/schemathesis/cli/output/default.py: note: In function "display_service_error":
+ src/schemathesis/cli/output/default.py:357: error: Item "None" of "Optional[Response]" has no attribute "status_code"  [union-attr]
+ src/schemathesis/cli/__init__.py: note: In function "handle_service_error":
+ src/schemathesis/cli/__init__.py:1198: error: Item "None" of "Optional[Response]" has no attribute "status_code"  [union-attr]
+ src/schemathesis/cli/__init__.py:1199: error: Item "None" of "Optional[Response]" has no attribute "json"  [union-attr]
+ src/schemathesis/cli/__init__.py:1200: error: Item "None" of "Optional[Response]" has no attribute "status_code"  [union-attr]
+ src/schemathesis/cli/__init__.py: note: In function "login":
+ src/schemathesis/cli/__init__.py:1316: error: Item "None" of "Optional[Response]" has no attribute "json"  [union-attr]

apprise (https://github.com/caronc/apprise)
- test/helpers/rest.py:63: error: Missing named argument "response" for "HTTPError"  [call-arg]
- test/test_attach_http.py:59: error: Missing named argument "response" for "HTTPError"  [call-arg]
- test/test_config_http.py:54: error: Missing named argument "response" for "HTTPError"  [call-arg]

openlibrary (https://github.com/internetarchive/openlibrary)
+ openlibrary/catalog/get_ia.py: note: In function "urlopen_keep_trying":
+ openlibrary/catalog/get_ia.py:25: error: Item "None" of "Response | None" has no attribute "status_code"  [union-attr]

@akx
Copy link
Contributor Author

akx commented Sep 25, 2023

Ah, actually, well, the ecosystem test speaks for itself – this is probably not a good idea.

@akx akx closed this Sep 25, 2023
@moltob
Copy link

moltob commented Sep 25, 2023

And this was also the wrong thing you loosened up. :-) The request is the issue, not the response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typing of requests.HTTPError possibly too strict
2 participants