Skip to content

Commit 3816cfa

Browse files
authored
Parameterize SupportsItems to handle Mapping key invariance (#7426)
1 parent b684dcb commit 3816cfa

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

src/requests/_types.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
)
2121

2222
_T_co = TypeVar("_T_co", covariant=True)
23+
_KT_co = TypeVar("_KT_co", covariant=True)
24+
_VT_co = TypeVar("_VT_co", covariant=True)
2325

2426

2527
@runtime_checkable
@@ -28,8 +30,8 @@ def read(self, length: int = ..., /) -> _T_co: ...
2830

2931

3032
@runtime_checkable
31-
class SupportsItems(Protocol):
32-
def items(self) -> Iterable[tuple[Any, Any]]: ...
33+
class SupportsItems(Protocol[_KT_co, _VT_co]):
34+
def items(self) -> Iterable[tuple[_KT_co, _VT_co]]: ...
3335

3436

3537
# These are needed at runtime for default_hooks() return type
@@ -79,15 +81,15 @@ class _ValidatedRequest(PreparedRequest):
7981
str | bytes | int | float | Iterable[str | bytes | int | float] | None
8082
)
8183
ParamsType: TypeAlias = (
82-
Mapping[_ParamsMappingKeyType, _ParamsMappingValueType]
84+
SupportsItems[_ParamsMappingKeyType, _ParamsMappingValueType]
8385
| tuple[tuple[_ParamsMappingKeyType, _ParamsMappingValueType], ...]
8486
| Iterable[tuple[_ParamsMappingKeyType, _ParamsMappingValueType]]
8587
| str
8688
| bytes
8789
| None
8890
)
8991

90-
KVDataType: TypeAlias = Iterable[tuple[Any, Any]] | Mapping[Any, Any]
92+
KVDataType: TypeAlias = Iterable[tuple[Any, Any]] | SupportsItems[Any, Any]
9193

9294
RawDataType: TypeAlias = KVDataType | str | bytes
9395
StreamDataType: TypeAlias = SupportsRead[str | bytes]

src/requests/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def proxy_bypass(host: str) -> bool: # noqa
147147

148148

149149
def dict_to_sequence(
150-
d: _t.SupportsItems | Iterable[tuple[Any, Any]],
150+
d: _t.SupportsItems[Any, Any] | Iterable[tuple[Any, Any]],
151151
) -> Iterable[tuple[Any, Any]]:
152152
"""Returns an internal sequence dictionary update."""
153153

@@ -371,10 +371,10 @@ def from_key_val_list(
371371
def to_key_val_list(value: None) -> None: ...
372372
@overload
373373
def to_key_val_list(
374-
value: Mapping[_KT, _VT] | Iterable[tuple[_KT, _VT]],
374+
value: _t.SupportsItems[_KT, _VT] | Iterable[tuple[_KT, _VT]],
375375
) -> list[tuple[_KT, _VT]]: ...
376376
def to_key_val_list(
377-
value: Mapping[_KT, _VT] | Iterable[tuple[_KT, _VT]] | None,
377+
value: _t.SupportsItems[_KT, _VT] | Iterable[tuple[_KT, _VT]] | None,
378378
) -> list[tuple[_KT, _VT]] | None:
379379
"""Take an object and test to see if it can be represented as a
380380
dictionary. If it can be, return a list of tuples, e.g.,

0 commit comments

Comments
 (0)