Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions src/requests/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ class _ValidatedRequest(PreparedRequest):
bytes | str | Iterable[bytes | str] | SupportsRead[bytes | str] | None
)

HeadersType: TypeAlias = CaseInsensitiveDict[str] | Mapping[str, str | bytes]
HeadersUpdateType: TypeAlias = Mapping[str, str | bytes | None]
HeadersType: TypeAlias = MutableMapping[str, str | bytes] | None

CookiesType: TypeAlias = RequestsCookieJar | Mapping[str, str]

Expand Down Expand Up @@ -145,7 +144,7 @@ class _ValidatedRequest(PreparedRequest):
# TypedDicts for Unpack kwargs (PEP 692)

class BaseRequestKwargs(TypedDict, total=False):
headers: Mapping[str, str | bytes] | None
headers: HeadersType
cookies: RequestsCookieJar | CookieJar | dict[str, str] | None
files: FilesType
auth: AuthType
Expand Down
5 changes: 3 additions & 2 deletions src/requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
)

if TYPE_CHECKING:
from collections.abc import MutableMapping
from http.cookiejar import CookieJar

from typing_extensions import Self
Expand Down Expand Up @@ -310,7 +311,7 @@ class Request(RequestHooksMixin):

method: str | None
url: _t.UriType | None
headers: CaseInsensitiveDict[str] | Mapping[str, str | bytes] | None
headers: MutableMapping[str, str | bytes]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not headerstype here too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

None was wrong. We always fallback to {} when you pass None into the constructor. The *Type aliases are all mapping input from the public API, this is representing what we store.

Otherwise you need to both validate headers is not None and Mutable. I made a similar change for status_code on Response.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I guess as a tangent to that, | None being baked into the types was an opinion. I don't feel super strongly about that, it was just for terseness and keeping the None requirement grouped. We could do HeadersType | None everywhere it's used now. That applies to most of the *Types.

files: _t.FilesType
data: _t.DataType
json: _t.JsonType
Expand All @@ -322,7 +323,7 @@ def __init__(
self,
method: str | None = None,
url: _t.UriType | None = None,
headers: Mapping[str, str | bytes] | None = None,
headers: _t.HeadersType = None,
files: _t.FilesType = None,
data: _t.DataType = None,
params: _t.ParamsType = None,
Expand Down
2 changes: 1 addition & 1 deletion src/requests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def request(
url: _t.UriType,
params: _t.ParamsType = None,
data: _t.DataType = None,
headers: Mapping[str, str | bytes] | None = None,
headers: _t.HeadersType = None,
cookies: RequestsCookieJar | CookieJar | dict[str, str] | None = None,
files: _t.FilesType = None,
auth: _t.AuthType = None,
Expand Down
Loading