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

Httpsession requests typing #2699

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

tdadela
Copy link
Contributor

@tdadela tdadela commented May 4, 2024

The aim of this PR is to provide better typing annotation support for class HttpSession.
This should also fix #2563.

@tdadela
Copy link
Contributor Author

tdadela commented May 5, 2024

This PR is still not finished (in draft). Requires adding similar annotations to FastHttpSession and more testing.
It would be nice and beneficial if you @cyberw could express if you are happy with this direction of changes.

@cyberw
Copy link
Collaborator

cyberw commented May 5, 2024

I’ll have a look!

@cyberw
Copy link
Collaborator

cyberw commented May 5, 2024

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.

@tdadela
Copy link
Contributor Author

tdadela commented May 5, 2024

I'm not 100% sure, but according to my current understanding of Python typing ecosystem — no.
I'll do more research about it.

I'd like to avoid the extra level of indirection at run time.

What is the main reason behind it?
Easier debugging / performance — necessity to create extra stack frame for additional function call / something else?

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.

@cyberw
Copy link
Collaborator

cyberw commented May 5, 2024

Yes, I’m mostly thinking about runtime performance. Why doesnt it work to define the wrappers in an if-block?

@tdadela
Copy link
Contributor Author

tdadela commented May 5, 2024

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).
I doubt if type checkers were designed to handle such cases.
But I'm just guessing. I don't know.
I'm going to check it.

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.

Pylance errors for catch_response and name parameters in self.client.get()
2 participants