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

requests: Allow session headers to be of type None #7773

Merged
merged 14 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions stubs/requests/requests/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Request(RequestHooksMixin):
class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
method: str | None
url: str | None
headers: CaseInsensitiveDict[str]
headers: CaseInsensitiveDict[str | bytes]
body: bytes | str | None
hooks: Any
def __init__(self) -> None: ...
Expand All @@ -95,7 +95,7 @@ class Response:
__attrs__: Any
_content: bytes | None # undocumented
status_code: int
headers: CaseInsensitiveDict[str]
headers: CaseInsensitiveDict[str | bytes]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this is correct? These headers are from the HTTP response, and I'd expect requests to return the same type consistently.

This seems to be the cause of a false positive on this line found by mypy-primer: https://github.com/yurijmikhalevich/rclip/blob/98a2454b988986f6ed67ae6e9fe3342ea82e0f92/rclip/utils.py#L92

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raw: Any
url: str
encoding: str | None
Expand Down
20 changes: 11 additions & 9 deletions stubs/requests/requests/sessions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ _Params: TypeAlias = Union[
str | bytes,
]
_TextMapping: TypeAlias = MutableMapping[str, str]
_HeadersMapping = TypeAlias = MutableMapping[str, str | bytes]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inconvenient because MutableMapping is invariant in its value type, so a dict[str, str] would not be acceptable.

Copy link
Contributor Author

@janrito janrito May 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about

MutableMapping[str, str] | MutableMapping[str, bytes]

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_HeadersUpdateMapping: TypeAlias = _HeadersMapping | MutableMapping[str, None]
_Timeout: TypeAlias = Union[float, tuple[float, float], tuple[float, None]]
_Verify: TypeAlias = bool | str

class Session(SessionRedirectMixin):
__attrs__: Any
headers: CaseInsensitiveDict[str | None]
headers: CaseInsensitiveDict[str | bytes]
auth: _Auth | None
proxies: _TextMapping
hooks: _Hooks
Expand All @@ -95,7 +97,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 +116,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 +135,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 +154,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 +174,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 +192,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 +211,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 +230,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