-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add full request typing signatures for session HTTP methods #8463
Add full request typing signatures for session HTTP methods #8463
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #8463 +/- ##
==========================================
- Coverage 97.64% 97.61% -0.03%
==========================================
Files 107 107
Lines 33067 33103 +36
Branches 3885 3894 +9
==========================================
+ Hits 32288 32314 +26
- Misses 562 570 +8
- Partials 217 219 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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.
This is my initial review with infra focus, I'll let @Dreamsorcerer approve the typing bits.
Thanks for the review! |
a5a61a7
to
680d4f5
Compare
…muoto/aiohttp into feat-improve-request-typing
@Dreamsorcerer Everything is purely 3.11+ overloads (without typing-extensions as a depenency), if you want to take another look now. |
for more information, see https://pre-commit.ci
…muoto/aiohttp into feat-improve-request-typing
Backport to 3.10: 💚 backport PR created✅ Backport PR branch: Backported as #8484 🤖 @patchback |
(cherry picked from commit 9386854)
Thanks |
auth: Union[BasicAuth, None] | ||
allow_redirects: bool | ||
max_redirects: int | ||
compress: Union[str, None] |
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.
This contradicts the documentation for compress
as bool
:
aiohttp/docs/client_reference.rst
Lines 431 to 434 in 48a5e07
:param bool compress: Set to ``True`` if request has to be compressed | |
with deflate encoding. If `compress` can not be combined | |
with a *Content-Encoding* and *Content-Length* headers. | |
``None`` by default (optional). |
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.
It's copied from _request(), not something related to this PR specifically. Feel free to open a PR to fix it. Looking at the code, it's the docs that are inaccurate, though we could expand the typing to allow bool:
aiohttp/aiohttp/client_reqrep.py
Lines 434 to 436 in 48a5e07
elif self.compress: | |
if not isinstance(self.compress, str): | |
self.compress = "deflate" |
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.
So, basically:
None/False/"" = Disable compression
str = use this compression method
Non-str (e.g. True) = use deflate
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.
I'm not entirely clear how the str is used though, I think maybe only deflate and gzip are valid..
Hello! For what it's worth, those types make it difficult to use keyword arguments and |
I don't think that has anything to do with these types. I'm pretty sure your type error is because you pass (e.g.) The only change we've done here is to add typing information so that mypy can give you such errors, instead of having no type checking on that code at all (you'd have seen the same error if we copy/pasted the parameters and didn't use a TypedDict/Unpack). We could consider exporting that TypedDict though, if it's useful for people.. |
I'd also note that using the TypedDict in your PR is also increasing the type safety of your code regardless. As you're now checking that the ssl argument is of the correct type. The previous code used |
Thanks for answering! And yes we used
It's not in its current form, because it requires filling everything. It would be useful if it included |
It does (otherwise your code wouldn't pass, because every parameter would need to be used). |
Sorry, I just remembered why this does not work for me: it's not nox > mypy --strict --show-error-codes elastic_transport/
elastic_transport/_node/_http_aiohttp.py:181: error: "request" of "ClientSession" gets multiple values for keyword argument "data" [misc]
elastic_transport/_node/_http_aiohttp.py:181: error: "request" of "ClientSession" gets multiple values for keyword argument "headers" [misc]
elastic_transport/_node/_http_aiohttp.py:181: error: "request" of "ClientSession" gets multiple values for keyword argument "timeout" [misc] Anyway, this confirms that my workaround is the best option possible for my use case. Thanks. |
What do these changes do?
Improve the request typing by using 3.11's new
Unpack
operator to properly type the kwargs that live on each HTTP method. Usingtyping_extensions
, this is supported on older versions of Python as well.To do this, I did add
typing-extensions
as a requirement on versions of Python before 3.11. I think this trade-off is well worth it, as it drastically improves the static typing checking experience, and the intellisense experience for VSCode users on Pylance.typing-extensions
will also letaiohttp
use other typing backports without having to wait multiple years until the minimum supported version Python catches up.Are there changes in behavior for the user?
Users will be improved type-checking when making requests, if improper/unexpected types are being used, they might notice new typing warnings.
Is it a substantial burden for the maintainers to support this?
No, updates to the request arguments simply need to be mirrored with the typed dict added, which will update all of the core HTTP methods.
Related issue number
Fixes #4749
Checklist
CONTRIBUTORS.txt
CHANGES/
foldername it
<issue_or_pr_num>.<type>.rst
(e.g.588.bugfix.rst
)if you don't have an issue number, change it to the pull request
number after creating the PR
.bugfix
: A bug fix for something the maintainers deemed animproper undesired behavior that got corrected to match
pre-agreed expectations.
.feature
: A new behavior, public APIs. That sort of stuff..deprecation
: A declaration of future API removals and breakingchanges in behavior.
.breaking
: When something public is removed in a breaking way.Could be deprecated in an earlier release.
.doc
: Notable updates to the documentation structure or buildprocess.
.packaging
: Notes for downstreams about unobvious side effectsand tooling. Changes in the test invocation considerations and
runtime assumptions.
.contrib
: Stuff that affects the contributor experience. e.g.Running tests, building the docs, setting up the development
environment.
.misc
: Changes that are hard to assign to any of the abovecategories.
Make sure to use full sentences with correct case and punctuation,
for example:
Use the past tense or the present tense a non-imperative mood,
referring to what's changed compared to the last released version
of this project.