-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Feature/expose request interface #7186
Feature/expose request interface #7186
Conversation
I don't think we want so much copy/paste. I think there is a better alterative coming up though: https://peps.python.org/pep-0692/ |
I think it may not be necessary to wait for version 3.12, as precise type annotations for **kwargs are now supported using TypedDict starting from MyPy version 0.981 (python/mypy#13471). This means that **kwargs: Any can now be rewritten as **kwargs: Unpack[HttpClientRequest], for example. We need to update Here is an example that works on python 3.10 import sys
from typing import (
Any,
Optional,
TypedDict,
Union
)
if sys.version_info >= (3, 11):
from typing import ( # noqa: F401
Unpack,
)
else:
from typing_extensions import ( # noqa: F401
Unpack,
)
StrOrURL = Union[str, bytes, "URL"]
class HttpClientRequest(TypedDict):
data: Optional[Union[str, bytes, dict]]
json: Optional[Union[dict, list]]
cookies: Optional[dict[str, str]]
headers: Optional[dict[str, str]]
allow_redirects: Optional[bool]
def get(url: StrOrURL, *, allow_redirects: bool = True, **kwargs: Unpack[HttpClientRequest]) -> Any:
"""
A function that sends an HTTP GET request.
:param url: The URL to send the request to.
:param allow_redirects: Whether or not to follow redirects.
:param kwargs: Additional parameters to pass to the request.
:return: The response from the server.
"""
# Example usage
param = {"data": None, "json": None, "cookies": None, "headers": None, "allow_redirects": True}
get(url="https://example.com", **param)
get(url="https://example.com", allow_redirects=True, data=None, json=None) |
That is PEP 692. It's still not stable and disabled by default (python/mypy#14697) and requires Unpack from Python 3.12. We can use typing_extensions if needed, but would be nice to start dropping typing_extensions as a dependency if possible. So, I'd probably still wait till 3.12 is released, though we can start preparing a PR beforehand if anyone wants to start looking at it. |
This has been done with Unpack now. |
What do these changes do?
Display ClientSession methods' parameters in the help window.
Before
![image](https://user-images.githubusercontent.com/41659814/215239898-020189f1-50f0-4945-80a7-8a2410304bb0.png)
![image](https://user-images.githubusercontent.com/41659814/215239914-97905a1b-1fea-46b5-a5e3-fefeb047b555.png)
After
Are there changes in behavior for the user?
Yes
Related issue number
#7185
Checklist
CONTRIBUTORS.txt
CHANGES
folder<issue_id>.<type>
for example (588.bugfix)issue_id
change it to the pr id after creating the pr.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.