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

Feature/expose request interface #7186

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jujumilk3
Copy link

@jujumilk3 jujumilk3 commented Jan 28, 2023

What do these changes do?

Display ClientSession methods' parameters in the help window.

Before
image
After
image

Are there changes in behavior for the user?

Yes

Related issue number

#7185

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .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.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Jan 28, 2023
@Dreamsorcerer
Copy link
Member

Dreamsorcerer commented Jan 28, 2023

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/
Seems we'll need to wait until Python 3.12 to use it.

@DavidRomanovizc
Copy link
Contributor

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/ Seems we'll need to wait until Python 3.12 to use it.

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 mypy to 0.981 and create possibly types.py which will contain our custom types

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)

@Dreamsorcerer
Copy link
Member

Dreamsorcerer commented May 4, 2023

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/ Seems we'll need to wait until Python 3.12 to use it.

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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants