From 51db4055d042788e6cde047e1b480b0e753a9bf3 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Wed, 14 May 2025 15:38:10 +0200 Subject: [PATCH] [requests] Add a _JSON type alias (#14064) --- stubs/requests/requests/api.pyi | 19 +++++++++---------- stubs/requests/requests/models.pyi | 8 +++++--- stubs/requests/requests/sessions.pyi | 26 +++++++++++++------------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/stubs/requests/requests/api.pyi b/stubs/requests/requests/api.pyi index 02ecab01d..49e0ce247 100644 --- a/stubs/requests/requests/api.pyi +++ b/stubs/requests/requests/api.pyi @@ -1,9 +1,8 @@ -from _typeshed import Incomplete from collections.abc import Mapping from http.cookiejar import CookieJar from typing_extensions import TypeAlias -from .models import Response +from .models import _JSON, Response from .sessions import _Auth, _Cert, _Data, _Files, _HooksInput, _Params, _TextMapping, _Timeout, _Verify _HeadersMapping: TypeAlias = Mapping[str, str | bytes | None] @@ -25,7 +24,7 @@ def request( stream: bool | None = ..., verify: _Verify | None = ..., cert: _Cert | None = ..., - json: Incomplete | None = ..., + json: _JSON | None = None, ) -> Response: ... def get( url: str | bytes, @@ -43,7 +42,7 @@ def get( stream: bool | None = ..., verify: _Verify | None = ..., cert: _Cert | None = ..., - json: Incomplete | None = ..., + json: _JSON | None = None, ) -> Response: ... def options( url: str | bytes, @@ -61,7 +60,7 @@ def options( stream: bool | None = ..., verify: _Verify | None = ..., cert: _Cert | None = ..., - json: Incomplete | None = ..., + json: _JSON | None = None, ) -> Response: ... def head( url: str | bytes, @@ -79,12 +78,12 @@ def head( stream: bool | None = ..., verify: _Verify | None = ..., cert: _Cert | None = ..., - json: Incomplete | None = ..., + json: _JSON | None = None, ) -> Response: ... def post( url: str | bytes, data: _Data | None = None, - json: Incomplete | None = None, + json: _JSON | None = None, *, params: _Params | None = ..., headers: _HeadersMapping | None = ..., @@ -115,7 +114,7 @@ def put( stream: bool | None = ..., verify: _Verify | None = ..., cert: _Cert | None = ..., - json: Incomplete | None = ..., + json: _JSON | None = None, ) -> Response: ... def patch( url: str | bytes, @@ -133,7 +132,7 @@ def patch( stream: bool | None = ..., verify: _Verify | None = ..., cert: _Cert | None = ..., - json: Incomplete | None = ..., + json: _JSON | None = None, ) -> Response: ... def delete( url: str | bytes, @@ -151,5 +150,5 @@ def delete( stream: bool | None = ..., verify: _Verify | None = ..., cert: _Cert | None = ..., - json: Incomplete | None = ..., + json: _JSON | None = None, ) -> Response: ... diff --git a/stubs/requests/requests/models.pyi b/stubs/requests/requests/models.pyi index d95a7382e..2747c9799 100644 --- a/stubs/requests/requests/models.pyi +++ b/stubs/requests/requests/models.pyi @@ -3,7 +3,7 @@ from _typeshed import Incomplete, MaybeNone, Unused from collections.abc import Callable, Iterator from json import JSONDecoder from typing import Any -from typing_extensions import Self +from typing_extensions import Self, TypeAlias from urllib3 import exceptions as urllib3_exceptions, fields, filepost, util from urllib3.response import HTTPResponse @@ -13,6 +13,8 @@ from .adapters import HTTPAdapter from .cookies import RequestsCookieJar from .structures import CaseInsensitiveDict as CaseInsensitiveDict +_JSON: TypeAlias = Any # any object that can be serialized to JSON + default_hooks = hooks.default_hooks HTTPBasicAuth = auth.HTTPBasicAuth cookiejar_from_dict = cookies.cookiejar_from_dict @@ -63,7 +65,7 @@ class Request(RequestHooksMixin): headers: Incomplete files: Incomplete data: Incomplete - json: Incomplete + json: _JSON | None params: Incomplete auth: Incomplete cookies: Incomplete @@ -78,7 +80,7 @@ class Request(RequestHooksMixin): auth=None, cookies=None, hooks=None, - json=None, + json: _JSON | None = None, ) -> None: ... def prepare(self) -> PreparedRequest: ... diff --git a/stubs/requests/requests/sessions.pyi b/stubs/requests/requests/sessions.pyi index 61f68b914..ec2211c6b 100644 --- a/stubs/requests/requests/sessions.pyi +++ b/stubs/requests/requests/sessions.pyi @@ -1,10 +1,10 @@ -from _typeshed import Incomplete, SupportsItems, SupportsRead, Unused +from _typeshed import SupportsItems, SupportsRead, Unused from collections.abc import Callable, Iterable, Mapping, MutableMapping from typing import Any, TypedDict from typing_extensions import Self, TypeAlias from . import adapters, auth as _auth, compat, cookies, exceptions, hooks, models, status_codes, utils -from .models import Response +from .models import _JSON, Response from .structures import CaseInsensitiveDict as CaseInsensitiveDict _BaseAdapter: TypeAlias = adapters.BaseAdapter @@ -44,10 +44,10 @@ class SessionRedirectMixin: resp, req, stream: bool = False, - timeout: Incomplete | None = None, + timeout=None, verify: bool = True, - cert: Incomplete | None = None, - proxies: Incomplete | None = None, + cert=None, + proxies=None, yield_requests: bool = False, **adapter_kwargs, ): ... @@ -151,7 +151,7 @@ class Session(SessionRedirectMixin): stream: bool | None = None, verify: _Verify | None = None, cert: _Cert | None = None, - json: Incomplete | None = None, + json: _JSON | None = None, ) -> Response: ... def get( self, @@ -170,7 +170,7 @@ class Session(SessionRedirectMixin): stream: bool | None = ..., verify: _Verify | None = ..., cert: _Cert | None = ..., - json: Incomplete | None = ..., + json: _JSON | None = None, ) -> Response: ... def options( self, @@ -189,7 +189,7 @@ class Session(SessionRedirectMixin): stream: bool | None = ..., verify: _Verify | None = ..., cert: _Cert | None = ..., - json: Incomplete | None = ..., + json: _JSON | None = None, ) -> Response: ... def head( self, @@ -208,13 +208,13 @@ class Session(SessionRedirectMixin): stream: bool | None = ..., verify: _Verify | None = ..., cert: _Cert | None = ..., - json: Incomplete | None = ..., + json: _JSON | None = None, ) -> Response: ... def post( self, url: str | bytes, data: _Data | None = None, - json: Incomplete | None = None, + json: _JSON | None = None, *, params: _Params | None = ..., headers: _HeadersUpdateMapping | None = ..., @@ -246,7 +246,7 @@ class Session(SessionRedirectMixin): stream: bool | None = ..., verify: _Verify | None = ..., cert: _Cert | None = ..., - json: Incomplete | None = ..., + json: _JSON | None = None, ) -> Response: ... def patch( self, @@ -265,7 +265,7 @@ class Session(SessionRedirectMixin): stream: bool | None = ..., verify: _Verify | None = ..., cert: _Cert | None = ..., - json: Incomplete | None = ..., + json: _JSON | None = None, ) -> Response: ... def delete( self, @@ -284,7 +284,7 @@ class Session(SessionRedirectMixin): stream: bool | None = ..., verify: _Verify | None = ..., cert: _Cert | None = ..., - json: Incomplete | None = ..., + json: _JSON | None = None, ) -> Response: ... def send( self,