Skip to content

Support dict(foo.split() for foo in bar) with bytes #10072

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

Merged
merged 2 commits into from
Apr 22, 2023
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
4 changes: 3 additions & 1 deletion stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1031,10 +1031,12 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def __init__(self, __iterable: Iterable[tuple[_KT, _VT]]) -> None: ...
@overload
def __init__(self: dict[str, _VT], __iterable: Iterable[tuple[str, _VT]], **kwargs: _VT) -> None: ...
# Next overload is for dict(string.split(sep) for string in iterable)
# Next two overloads are for dict(string.split(sep) for string in iterable)
# Cannot be Iterable[Sequence[_T]] or otherwise dict(["foo", "bar", "baz"]) is not an error
@overload
def __init__(self: dict[str, str], __iterable: Iterable[list[str]]) -> None: ...
@overload
def __init__(self: dict[bytes, bytes], __iterable: Iterable[list[bytes]]) -> None: ...
def __new__(cls, *args: Any, **kwargs: Any) -> Self: ...
def copy(self) -> dict[_KT, _VT]: ...
def keys(self) -> dict_keys[_KT, _VT]: ...
Expand Down
2 changes: 2 additions & 0 deletions stdlib/collections/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class UserDict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def __init__(self: UserDict[str, _VT], __iterable: Iterable[tuple[str, _VT]], **kwargs: _VT) -> None: ...
@overload
def __init__(self: UserDict[str, str], __iterable: Iterable[list[str]]) -> None: ...
@overload
def __init__(self: UserDict[bytes, bytes], __iterable: Iterable[list[bytes]]) -> None: ...
def __len__(self) -> int: ...
def __getitem__(self, key: _KT) -> _VT: ...
def __setitem__(self, key: _KT, item: _VT) -> None: ...
Expand Down
2 changes: 2 additions & 0 deletions stdlib/multiprocessing/managers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ class SyncManager(BaseManager):
@overload
def dict(self, __iterable: Iterable[list[str]]) -> DictProxy[str, str]: ...
@overload
def dict(self, __iterable: Iterable[list[bytes]]) -> DictProxy[bytes, bytes]: ...
@overload
def list(self, __sequence: Sequence[_T]) -> ListProxy[_T]: ...
@overload
def list(self) -> ListProxy[Any]: ...
Expand Down
4 changes: 4 additions & 0 deletions test_cases/stdlib/builtins/check_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@ def test_iterable_tuple_overload(x: Iterable[tuple[int, str]]) -> dict[int, str]
assert_type(dict(i2, arg=1), Dict[str, int])

i3: Iterable[str] = ["a.b"]
i4: Iterable[bytes] = [b"a.b"]
assert_type(dict(string.split(".") for string in i3), Dict[str, str])
assert_type(dict(string.split(b".") for string in i4), Dict[bytes, bytes])

dict(["foo", "bar", "baz"]) # type: ignore
dict([b"foo", b"bar", b"baz"]) # type: ignore