Skip to content

Commit

Permalink
requests: Fix type of request headers (#7773)
Browse files Browse the repository at this point in the history
Allow bytes values. In `Session` methods, `None` can be used to ignore the session's headers.
  • Loading branch information
janrito authored May 16, 2022
1 parent 487bef5 commit 87abd8a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
30 changes: 21 additions & 9 deletions stubs/requests/requests/api.pyi
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
from typing import Any

from .models import Response
from .sessions import RequestsCookieJar, _Auth, _Cert, _Data, _Files, _HooksInput, _Params, _TextMapping, _Timeout, _Verify
from .sessions import (
RequestsCookieJar,
_Auth,
_Cert,
_Data,
_Files,
_HeadersMapping,
_HooksInput,
_Params,
_TextMapping,
_Timeout,
_Verify,
)

def request(
method: str | bytes,
url: str | bytes,
*,
params: _Params | None = ...,
data: _Data | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand All @@ -27,7 +39,7 @@ def get(
params: _Params | None = ...,
*,
data: _Data | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand All @@ -45,7 +57,7 @@ def options(
*,
params: _Params | None = ...,
data: _Data | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand All @@ -63,7 +75,7 @@ def head(
*,
params: _Params | None = ...,
data: _Data | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand All @@ -82,7 +94,7 @@ def post(
json: Any | None = ...,
*,
params: _Params | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand All @@ -99,7 +111,7 @@ def put(
data: _Data | None = ...,
*,
params: _Params | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand All @@ -117,7 +129,7 @@ def patch(
data: _Data | None = ...,
*,
params: _Params | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand All @@ -135,7 +147,7 @@ def delete(
*,
params: _Params | None = ...,
data: _Data | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand Down
28 changes: 19 additions & 9 deletions stubs/requests/requests/sessions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,22 @@ _Params: TypeAlias = Union[
str | bytes,
]
_TextMapping: TypeAlias = MutableMapping[str, str]
_HeadersMapping: TypeAlias = MutableMapping[str, str] | MutableMapping[str, bytes] | MutableMapping[str, str | bytes]
_HeadersUpdateMapping: TypeAlias = (
MutableMapping[str, str]
| MutableMapping[str, bytes]
| MutableMapping[str, None]
| MutableMapping[str, str | bytes]
| MutableMapping[str, str | None]
| MutableMapping[str, bytes | None]
| MutableMapping[str, str | bytes | None]
)
_Timeout: TypeAlias = Union[float, tuple[float, float], tuple[float, None]]
_Verify: TypeAlias = bool | str

class Session(SessionRedirectMixin):
__attrs__: Any
headers: CaseInsensitiveDict[str]
headers: CaseInsensitiveDict[str | bytes]
auth: _Auth | None
proxies: _TextMapping
hooks: _Hooks
Expand All @@ -95,7 +105,7 @@ class Session(SessionRedirectMixin):
url: str | bytes,
params: _Params | None = ...,
data: _Data | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersUpdateMapping | None = ...,
cookies: None | RequestsCookieJar | _TextMapping = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand All @@ -114,7 +124,7 @@ class Session(SessionRedirectMixin):
*,
params: _Params | None = ...,
data: _Data | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersUpdateMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand All @@ -133,7 +143,7 @@ class Session(SessionRedirectMixin):
*,
params: _Params | None = ...,
data: _Data | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersUpdateMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand All @@ -152,7 +162,7 @@ class Session(SessionRedirectMixin):
*,
params: _Params | None = ...,
data: _Data | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersUpdateMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand All @@ -172,7 +182,7 @@ class Session(SessionRedirectMixin):
json: Any | None = ...,
*,
params: _Params | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersUpdateMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand All @@ -190,7 +200,7 @@ class Session(SessionRedirectMixin):
data: _Data | None = ...,
*,
params: _Params | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersUpdateMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand All @@ -209,7 +219,7 @@ class Session(SessionRedirectMixin):
data: _Data | None = ...,
*,
params: _Params | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersUpdateMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand All @@ -228,7 +238,7 @@ class Session(SessionRedirectMixin):
*,
params: _Params | None = ...,
data: _Data | None = ...,
headers: _TextMapping | None = ...,
headers: _HeadersUpdateMapping | None = ...,
cookies: RequestsCookieJar | _TextMapping | None = ...,
files: _Files | None = ...,
auth: _Auth | None = ...,
Expand Down

0 comments on commit 87abd8a

Please sign in to comment.