From d9ca9a7a464f47b1f155b13ed67eb962c219b9ac Mon Sep 17 00:00:00 2001 From: Alejandro Giacometti Date: Tue, 3 May 2022 12:41:29 +0100 Subject: [PATCH 01/14] Allow session headers to be of type None --- stubs/requests/requests/sessions.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/requests/requests/sessions.pyi b/stubs/requests/requests/sessions.pyi index a0143f60e3d6..b88b8da29c77 100644 --- a/stubs/requests/requests/sessions.pyi +++ b/stubs/requests/requests/sessions.pyi @@ -72,7 +72,7 @@ _Verify: TypeAlias = bool | str class Session(SessionRedirectMixin): __attrs__: Any - headers: CaseInsensitiveDict[str] + headers: CaseInsensitiveDict[str | None] auth: _Auth | None proxies: _TextMapping hooks: _Hooks From 249ce7cdd32150c770973da688c91ee0dcefb0d2 Mon Sep 17 00:00:00 2001 From: Alejandro Giacometti Date: Wed, 4 May 2022 15:29:40 +0100 Subject: [PATCH 02/14] Revert "Allow session headers to be of type None" This reverts commit d9ca9a7a464f47b1f155b13ed67eb962c219b9ac. --- stubs/requests/requests/sessions.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/requests/requests/sessions.pyi b/stubs/requests/requests/sessions.pyi index b88b8da29c77..a0143f60e3d6 100644 --- a/stubs/requests/requests/sessions.pyi +++ b/stubs/requests/requests/sessions.pyi @@ -72,7 +72,7 @@ _Verify: TypeAlias = bool | str class Session(SessionRedirectMixin): __attrs__: Any - headers: CaseInsensitiveDict[str | None] + headers: CaseInsensitiveDict[str] auth: _Auth | None proxies: _TextMapping hooks: _Hooks From 0602cc3b0bc8adb6f24ce1145508010ad7c3f0a8 Mon Sep 17 00:00:00 2001 From: Alejandro Giacometti Date: Wed, 4 May 2022 15:44:34 +0100 Subject: [PATCH 03/14] Allow for bytes in header value --- stubs/requests/requests/models.pyi | 4 ++-- stubs/requests/requests/sessions.pyi | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/stubs/requests/requests/models.pyi b/stubs/requests/requests/models.pyi index 38fb47453776..b6ca605f2da7 100644 --- a/stubs/requests/requests/models.pyi +++ b/stubs/requests/requests/models.pyi @@ -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: ... @@ -95,7 +95,7 @@ class Response: __attrs__: Any _content: bytes | None # undocumented status_code: int - headers: CaseInsensitiveDict[str] + headers: CaseInsensitiveDict[str | bytes] raw: Any url: str encoding: str | None diff --git a/stubs/requests/requests/sessions.pyi b/stubs/requests/requests/sessions.pyi index a0143f60e3d6..ac464c174f84 100644 --- a/stubs/requests/requests/sessions.pyi +++ b/stubs/requests/requests/sessions.pyi @@ -72,7 +72,7 @@ _Verify: TypeAlias = bool | str class Session(SessionRedirectMixin): __attrs__: Any - headers: CaseInsensitiveDict[str] + headers: CaseInsensitiveDict[str | bytes] auth: _Auth | None proxies: _TextMapping hooks: _Hooks From b1ad88da728f4d0978ce4be61a4c2926e901ecdf Mon Sep 17 00:00:00 2001 From: Alejandro Giacometti Date: Wed, 4 May 2022 15:45:34 +0100 Subject: [PATCH 04/14] Add _HeadersMapping alias, which allows bytes as well as strings as headers values --- stubs/requests/requests/api.pyi | 30 +++++++++++++++++++--------- stubs/requests/requests/sessions.pyi | 1 + 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/stubs/requests/requests/api.pyi b/stubs/requests/requests/api.pyi index ee6f446e5ca5..0006f6851bfa 100644 --- a/stubs/requests/requests/api.pyi +++ b/stubs/requests/requests/api.pyi @@ -1,7 +1,19 @@ 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, @@ -9,7 +21,7 @@ def request( *, params: _Params | None = ..., data: _Data | None = ..., - headers: _TextMapping | None = ..., + headers: _HeadersMapping | None = ..., cookies: RequestsCookieJar | _TextMapping | None = ..., files: _Files | None = ..., auth: _Auth | None = ..., @@ -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 = ..., @@ -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 = ..., @@ -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 = ..., @@ -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 = ..., @@ -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 = ..., @@ -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 = ..., @@ -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 = ..., diff --git a/stubs/requests/requests/sessions.pyi b/stubs/requests/requests/sessions.pyi index ac464c174f84..3ab318535dca 100644 --- a/stubs/requests/requests/sessions.pyi +++ b/stubs/requests/requests/sessions.pyi @@ -67,6 +67,7 @@ _Params: TypeAlias = Union[ str | bytes, ] _TextMapping: TypeAlias = MutableMapping[str, str] +_HeadersMapping = TypeAlias = MutableMapping[str, str | bytes] _Timeout: TypeAlias = Union[float, tuple[float, float], tuple[float, None]] _Verify: TypeAlias = bool | str From 99968589982fab4e31e0b5f5e47f2e35d7c9cfc3 Mon Sep 17 00:00:00 2001 From: Alejandro Giacometti Date: Wed, 4 May 2022 15:46:16 +0100 Subject: [PATCH 05/14] Add _HeadersMappingUpdate alias, which allows for an empty mapping as well as str and bytes --- stubs/requests/requests/sessions.pyi | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/stubs/requests/requests/sessions.pyi b/stubs/requests/requests/sessions.pyi index 3ab318535dca..a28e2fa5260f 100644 --- a/stubs/requests/requests/sessions.pyi +++ b/stubs/requests/requests/sessions.pyi @@ -68,6 +68,7 @@ _Params: TypeAlias = Union[ ] _TextMapping: TypeAlias = MutableMapping[str, str] _HeadersMapping = TypeAlias = MutableMapping[str, str | bytes] +_HeadersUpdateMapping: TypeAlias = _HeadersMapping | MutableMapping[str, None] _Timeout: TypeAlias = Union[float, tuple[float, float], tuple[float, None]] _Verify: TypeAlias = bool | str @@ -96,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 = ..., @@ -115,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 = ..., @@ -134,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 = ..., @@ -153,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 = ..., @@ -173,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 = ..., @@ -191,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 = ..., @@ -210,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 = ..., @@ -229,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 = ..., From 3368ce656ed69d6730a26bdcb44b75b773734e85 Mon Sep 17 00:00:00 2001 From: Alejandro Giacometti Date: Wed, 4 May 2022 15:55:44 +0100 Subject: [PATCH 06/14] fix type annotation rather than assignment --- stubs/requests/requests/sessions.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/requests/requests/sessions.pyi b/stubs/requests/requests/sessions.pyi index a28e2fa5260f..8dbf173ea612 100644 --- a/stubs/requests/requests/sessions.pyi +++ b/stubs/requests/requests/sessions.pyi @@ -67,7 +67,7 @@ _Params: TypeAlias = Union[ str | bytes, ] _TextMapping: TypeAlias = MutableMapping[str, str] -_HeadersMapping = TypeAlias = MutableMapping[str, str | bytes] +_HeadersMapping: TypeAlias = MutableMapping[str, str | bytes] _HeadersUpdateMapping: TypeAlias = _HeadersMapping | MutableMapping[str, None] _Timeout: TypeAlias = Union[float, tuple[float, float], tuple[float, None]] _Verify: TypeAlias = bool | str From 0e8ff7e3a650c003fbae68c24d6869b19a205c3a Mon Sep 17 00:00:00 2001 From: Alejandro Giacometti Date: Wed, 4 May 2022 15:58:24 +0100 Subject: [PATCH 07/14] `MutableMapping[str, str | bytes] -> MutableMapping[str, str] | MutableMapping[str, bytes]` + simple assignment in alias definition --- stubs/requests/requests/sessions.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stubs/requests/requests/sessions.pyi b/stubs/requests/requests/sessions.pyi index 8dbf173ea612..40f91eb286cc 100644 --- a/stubs/requests/requests/sessions.pyi +++ b/stubs/requests/requests/sessions.pyi @@ -67,8 +67,8 @@ _Params: TypeAlias = Union[ str | bytes, ] _TextMapping: TypeAlias = MutableMapping[str, str] -_HeadersMapping: TypeAlias = MutableMapping[str, str | bytes] -_HeadersUpdateMapping: TypeAlias = _HeadersMapping | MutableMapping[str, None] +_HeadersMapping: TypeAlias = MutableMapping[str, str] | MutableMapping[str, bytes] +_HeadersUpdateMapping: TypeAlias = MutableMapping[str, str] | MutableMapping[str, bytes] | MutableMapping[str, None] _Timeout: TypeAlias = Union[float, tuple[float, float], tuple[float, None]] _Verify: TypeAlias = bool | str From b5ec8347c17d3963216bcd7ccb5b639da0b6ad2e Mon Sep 17 00:00:00 2001 From: Alejandro Giacometti Date: Fri, 6 May 2022 10:54:14 +0100 Subject: [PATCH 08/14] str | bytes | None --- stubs/requests/requests/sessions.pyi | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/stubs/requests/requests/sessions.pyi b/stubs/requests/requests/sessions.pyi index 40f91eb286cc..49c69d3da6a6 100644 --- a/stubs/requests/requests/sessions.pyi +++ b/stubs/requests/requests/sessions.pyi @@ -67,8 +67,16 @@ _Params: TypeAlias = Union[ str | bytes, ] _TextMapping: TypeAlias = MutableMapping[str, str] -_HeadersMapping: TypeAlias = MutableMapping[str, str] | MutableMapping[str, bytes] -_HeadersUpdateMapping: TypeAlias = MutableMapping[str, str] | MutableMapping[str, bytes] | MutableMapping[str, None] +_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 From 6f7eb28127011b54b9db44c01128fbeefdffb124 Mon Sep 17 00:00:00 2001 From: Alejandro Giacometti Date: Mon, 9 May 2022 15:06:18 +0100 Subject: [PATCH 09/14] reverse bytes type on session headers + these are casted to native strings here https://github.com/psf/requests/blob/40956723f27daf5e0d9759208ca69cef236ab339/requests/models.py#L484 --- stubs/requests/requests/sessions.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/requests/requests/sessions.pyi b/stubs/requests/requests/sessions.pyi index 49c69d3da6a6..fe948a09f4b8 100644 --- a/stubs/requests/requests/sessions.pyi +++ b/stubs/requests/requests/sessions.pyi @@ -82,7 +82,7 @@ _Verify: TypeAlias = bool | str class Session(SessionRedirectMixin): __attrs__: Any - headers: CaseInsensitiveDict[str | bytes] + headers: CaseInsensitiveDict[str] auth: _Auth | None proxies: _TextMapping hooks: _Hooks From f9aaf0c5ad6e7a2952922422035529ba571376a8 Mon Sep 17 00:00:00 2001 From: Alejandro Giacometti Date: Mon, 16 May 2022 16:58:17 +0100 Subject: [PATCH 10/14] Revert "reverse bytes type on session headers" This reverts commit 6f7eb28127011b54b9db44c01128fbeefdffb124. --- stubs/requests/requests/sessions.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/requests/requests/sessions.pyi b/stubs/requests/requests/sessions.pyi index fe948a09f4b8..49c69d3da6a6 100644 --- a/stubs/requests/requests/sessions.pyi +++ b/stubs/requests/requests/sessions.pyi @@ -82,7 +82,7 @@ _Verify: TypeAlias = bool | str class Session(SessionRedirectMixin): __attrs__: Any - headers: CaseInsensitiveDict[str] + headers: CaseInsensitiveDict[str | bytes] auth: _Auth | None proxies: _TextMapping hooks: _Hooks From 0d80bdf358664482897a624f5577cd4c32b175ea Mon Sep 17 00:00:00 2001 From: Alejandro Giacometti Date: Mon, 16 May 2022 16:59:43 +0100 Subject: [PATCH 11/14] Response header dict is always `str` --- stubs/requests/requests/models.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/requests/requests/models.pyi b/stubs/requests/requests/models.pyi index b6ca605f2da7..143ca7548da3 100644 --- a/stubs/requests/requests/models.pyi +++ b/stubs/requests/requests/models.pyi @@ -95,7 +95,7 @@ class Response: __attrs__: Any _content: bytes | None # undocumented status_code: int - headers: CaseInsensitiveDict[str | bytes] + headers: CaseInsensitiveDict[str] raw: Any url: str encoding: str | None From 072c5372ac94d9aa6891dfd0b0120f8e3b70f83b Mon Sep 17 00:00:00 2001 From: Alejandro Giacometti Date: Mon, 16 May 2022 17:24:07 +0100 Subject: [PATCH 12/14] PreparedRequests cast headers to native strings as well https://github.com/psf/requests/blob/79f2ec3acc4e24fef6e6ce31ad7b1d4e2f77be31/requests/models.py#L493 --- stubs/requests/requests/models.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/requests/requests/models.pyi b/stubs/requests/requests/models.pyi index 143ca7548da3..38fb47453776 100644 --- a/stubs/requests/requests/models.pyi +++ b/stubs/requests/requests/models.pyi @@ -74,7 +74,7 @@ class Request(RequestHooksMixin): class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): method: str | None url: str | None - headers: CaseInsensitiveDict[str | bytes] + headers: CaseInsensitiveDict[str] body: bytes | str | None hooks: Any def __init__(self) -> None: ... From f189735bf6f2f08669c74982af9c3b30410538dd Mon Sep 17 00:00:00 2001 From: Alejandro Giacometti Date: Mon, 16 May 2022 17:26:17 +0100 Subject: [PATCH 13/14] Fix `CaseInsensitiveDict[str | bytes]` invariance --- stubs/requests/requests/sessions.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/requests/requests/sessions.pyi b/stubs/requests/requests/sessions.pyi index 49c69d3da6a6..5b156d641783 100644 --- a/stubs/requests/requests/sessions.pyi +++ b/stubs/requests/requests/sessions.pyi @@ -82,7 +82,7 @@ _Verify: TypeAlias = bool | str class Session(SessionRedirectMixin): __attrs__: Any - headers: CaseInsensitiveDict[str | bytes] + headers: CaseInsensitiveDict[str] | CaseInsensitiveDict[bytes] | CaseInsensitiveDict[str | bytes] auth: _Auth | None proxies: _TextMapping hooks: _Hooks From ccb434d2783d901e05d2406a39c4567af9b64011 Mon Sep 17 00:00:00 2001 From: Alejandro Giacometti Date: Mon, 16 May 2022 20:14:08 +0100 Subject: [PATCH 14/14] Revert "Fix `CaseInsensitiveDict[str | bytes]` invariance" This reverts commit f189735bf6f2f08669c74982af9c3b30410538dd. --- stubs/requests/requests/sessions.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/requests/requests/sessions.pyi b/stubs/requests/requests/sessions.pyi index 5b156d641783..49c69d3da6a6 100644 --- a/stubs/requests/requests/sessions.pyi +++ b/stubs/requests/requests/sessions.pyi @@ -82,7 +82,7 @@ _Verify: TypeAlias = bool | str class Session(SessionRedirectMixin): __attrs__: Any - headers: CaseInsensitiveDict[str] | CaseInsensitiveDict[bytes] | CaseInsensitiveDict[str | bytes] + headers: CaseInsensitiveDict[str | bytes] auth: _Auth | None proxies: _TextMapping hooks: _Hooks