-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Improve dict
's constructor type
#8517
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
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e445714
Improve `dict`'s constructor type
sobolevn 899ad8f
Update __init__.pyi
sobolevn 875499d
Update managers.pyi
sobolevn c58eb45
Add tests for `dict`
sobolevn 0af83f7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f16225a
Fix `flake8`?
sobolevn a8306b5
Update test_dict.py
sobolevn 1768683
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b6bf122
Update test_dict.py
sobolevn 878abd8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e672805
Update test_dict.py
sobolevn bd26068
Also address `SupportsKeysAndGetItem` overload
sobolevn 084fd42
Try `MYPY` always true hack
sobolevn 5700f7b
Try another hack
sobolevn 1b8af4a
Try one more
sobolevn b1cff10
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1a510e0
I give up
sobolevn 1fb823a
Last one
sobolevn 4ed2249
I can add suggestions now! Wow, such awesome!
sobolevn 063411c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# pyright: reportUnnecessaryTypeIgnoreComment=true | ||
|
||
from typing import Dict, Generic, Iterable, Tuple, TypeVar | ||
from typing_extensions import assert_type | ||
|
||
# These do follow `__init__` overloads order: | ||
# mypy and pyright have different opinions about this one: | ||
# mypy raises: 'Need type annotation for "bad"' | ||
# pyright is fine with it. | ||
# bad = dict() | ||
good: Dict[str, str] = dict() | ||
assert_type(good, Dict[str, str]) | ||
|
||
assert_type(dict(arg=1), Dict[str, int]) | ||
|
||
_KT = TypeVar("_KT") | ||
_VT = TypeVar("_VT") | ||
|
||
|
||
class KeysAndGetItem(Generic[_KT, _VT]): | ||
def keys(self) -> Iterable[_KT]: | ||
... | ||
|
||
def __getitem__(self, __k: _KT) -> _VT: | ||
... | ||
|
||
|
||
kt1: KeysAndGetItem[int, str] = KeysAndGetItem() | ||
assert_type(dict(kt1), Dict[int, str]) | ||
dict(kt1, arg="a") # type: ignore | ||
|
||
kt2: KeysAndGetItem[str, int] = KeysAndGetItem() | ||
assert_type(dict(kt2, arg=1), Dict[str, int]) | ||
|
||
|
||
def test_iterable_tuple_overload(x: Iterable[Tuple[int, str]]) -> Dict[int, str]: | ||
return dict(x) | ||
|
||
|
||
i1: Iterable[Tuple[int, str]] = [(1, "a"), (2, "b")] | ||
test_iterable_tuple_overload(i1) | ||
dict(i1, arg="a") # type: ignore | ||
|
||
i2: Iterable[Tuple[str, int]] = [("a", 1), ("b", 2)] | ||
assert_type(dict(i2, arg=1), Dict[str, int]) | ||
|
||
i3: Iterable[str] = ["a.b"] | ||
assert_type(dict(string.split(".") for string in i3), Dict[str, str]) | ||
sobolevn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dict(["foo", "bar", "baz"]) # type: ignore |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.