Parameterize SupportsItems to handle Mapping key invariance (#7426)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nate Prewitt · committed by GitHub · parent b684dcb · Browse files · 2 files·+9 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+9 added−7 removed
- other:+9 added−7 removed
Provenance
- Commit
3816cfa- Tree
d7dc5cd- Author
Nate Prewitt <nate.prewitt@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
b684dcb
2 files changed+9 added−7 removed
modified:src/requests/_types.py+6 added-4 removed
18 unmodified lines19 runtime_checkable,20 )21 22 _T_co = TypeVar("_T_co", covariant=True)23+_KT_co = TypeVar("_KT_co", covariant=True)24+_VT_co = TypeVar("_VT_co", covariant=True)25 26 27 @runtime_checkable28 class SupportsRead(Protocol[_T_co]):29 def read(self, length: int = ..., /) -> _T_co: ...30 31 32 @runtime_checkable31-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]]: ...35 36 37 # These are needed at runtime for default_hooks() return type38 HookType: TypeAlias = Callable[["Response"], Any]41 unmodified lines80 _ParamsMappingValueType: TypeAlias = (81 str | bytes | int | float | Iterable[str | bytes | int | float] | None82 )83 ParamsType: TypeAlias = (82- Mapping[_ParamsMappingKeyType, _ParamsMappingValueType]84+ SupportsItems[_ParamsMappingKeyType, _ParamsMappingValueType]85 | tuple[tuple[_ParamsMappingKeyType, _ParamsMappingValueType], ...]86 | Iterable[tuple[_ParamsMappingKeyType, _ParamsMappingValueType]]87 | str88 | bytes89 | None90 )91 90- KVDataType: TypeAlias = Iterable[tuple[Any, Any]] | Mapping[Any, Any]92+ KVDataType: TypeAlias = Iterable[tuple[Any, Any]] | SupportsItems[Any, Any]93 94 RawDataType: TypeAlias = KVDataType | str | bytes95 StreamDataType: TypeAlias = SupportsRead[str | bytes]96 EncodableDataType: TypeAlias = RawDataType | StreamDataType82 unmodified linesmodified:src/requests/utils.py+3 added-3 removed
145 unmodified lines146 return proxy_bypass_registry(host)147 148 149 def dict_to_sequence(150- d: _t.SupportsItems | Iterable[tuple[Any, Any]],150+ d: _t.SupportsItems[Any, Any] | Iterable[tuple[Any, Any]],151 ) -> Iterable[tuple[Any, Any]]:152 """Returns an internal sequence dictionary update."""153 154 if isinstance(d, _SupportsItems):215 unmodified lines370 @overload371 def to_key_val_list(value: None) -> None: ...372 @overload373 def to_key_val_list(374- value: Mapping[_KT, _VT] | Iterable[tuple[_KT, _VT]],374+ value: _t.SupportsItems[_KT, _VT] | Iterable[tuple[_KT, _VT]],375 ) -> list[tuple[_KT, _VT]]: ...376 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,378 ) -> list[tuple[_KT, _VT]] | None:379 """Take an object and test to see if it can be represented as a380 dictionary. If it can be, return a list of tuples, e.g.,381 772 unmodified lines