-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Httpsession requests typing #2699
Conversation
…r type annotations support
This PR is still not finished (in draft). Requires adding similar annotations to FastHttpSession and more testing. |
I’ll have a look! |
Is it possible to put all the override definitions in a if typing_checking-block as well? I'd like to avoid the extra level of indirection at run time. |
I'm not 100% sure, but according to my current understanding of Python typing ecosystem — no.
What is the main reason behind it? Source code of methods in the base class is very short: def get(self, url, **kwargs):
kwargs.setdefault("allow_redirects", True)
return self.request("GET", url, **kwargs)
def options(self, url, **kwargs):
kwargs.setdefault("allow_redirects", True)
return self.request("OPTIONS", url, **kwargs)
def head(self, url, **kwargs):
kwargs.setdefault("allow_redirects", False)
return self.request("HEAD", url, **kwargs)
def post(self, url, data=None, json=None, **kwargs):
return self.request("POST", url, data=data, json=json, **kwargs)
def put(self, url, data=None, **kwargs):
return self.request("PUT", url, data=data, **kwargs)
def patch(self, url, data=None, **kwargs):
return self.request("PATCH", url, data=data, **kwargs)
def delete(self, url, **kwargs):
return self.request("DELETE", url, **kwargs) What do you think about following possible implementation in Locust: def get(self, url: str | bytes, **kwargs: Unpack[RESTKwargs]): # type: ignore[override]
kwargs.setdefault("allow_redirects", True)
return self.request("GET", url, **kwargs) # type: ignore[misc] The number of function call would remain the same. |
Yes, I’m mostly thinking about runtime performance. Why doesnt it work to define the wrappers in an if-block? |
I think about it as defining types of method that de facto doesn't exist (in contrast to a function with the same name in a base class). |
hi @tdadela did you have a chance to try it out? Or should I try it? |
I will do it this weekend.
…On Tue, 4 Jun 2024, 20:53 Lars Holmberg, ***@***.***> wrote:
hi @tdadela <https://github.com/tdadela> did you have a chance to try it
out? Or should I try it?
—
Reply to this email directly, view it on GitHub
<#2699 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJM4KUOWQAYPRYWYS4S7BMLZFYEJ3AVCNFSM6AAAAABHHFTPXCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNBYGE4TKMBQGQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Sorry for the delay. I see 3 options:
|
I like the version in the PR now. Have you tried it with vscode/pyright? Maybe add a constraint to the typing_extensions dependency to only download it on Python versions <3.11? |
Co-authored-by: Oscar Butler-Aldridge <[email protected]>
… return type for consistency
@cyberw Do you prefer the last or the last but one version? Also, I've realized that # type: ignore[misc] is no longer necessary, probably since calling self.request directly. I'll remove it later. (To not make the impression it's connected with the last commit). |
Yes. It works well. (I hope I checked everything correctly.) |
@0scarB Thank you very much for your support! |
Tested the latest commit in my project. LGTM! The return types are now correctly shown by pyright. Happy to help |
Looks good to me too. @tdadela If you feel it is ready to go, move it out of draft and I'll merge it. Ideally I'd like to use the same typing for FastHttpSession but we can do that later on... |
Today I tried to make return type argument dependent (catch_response) using
Yup, soon in another PR. |
thx! |
The aim of this PR is to provide better typing annotation support for
class HttpSession
.This should also fix #2563.