-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
TypedDict: Recognize __getitem__ and __setitem__ access. #2526
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
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -360,64 +360,73 @@ reveal_type(f(g)) # E: Revealed type is '<uninhabited>' | |
|
||
-- Special Method: __getitem__ | ||
|
||
-- TODO: Implement support for this case. | ||
--[case testCanGetItemOfTypedDictWithValidStringLiteralKey] | ||
--from mypy_extensions import TypedDict | ||
--TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int}) | ||
--p = TaggedPoint(type='2d', x=42, y=1337) | ||
--def get_x(p: TaggedPoint) -> int: | ||
-- return p['x'] | ||
--[builtins fixtures/dict.pyi] | ||
[case testCanGetItemOfTypedDictWithValidStringLiteralKey] | ||
from mypy_extensions import TypedDict | ||
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int}) | ||
p = TaggedPoint(type='2d', x=42, y=1337) | ||
reveal_type(p['type']) # E: Revealed type is 'builtins.str' | ||
reveal_type(p['x']) # E: Revealed type is 'builtins.int' | ||
reveal_type(p['y']) # E: Revealed type is 'builtins.int' | ||
[builtins fixtures/dict.pyi] | ||
|
||
-- TODO: Implement support for this case. | ||
--[case testCannotGetItemOfTypedDictWithInvalidStringLiteralKey] | ||
--from mypy_extensions import TypedDict | ||
--TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int}) | ||
--p = TaggedPoint(type='2d', x=42, y=1337) | ||
--def get_z(p: TaggedPoint) -> int: | ||
-- return p['z'] # E: ... 'z' is not a valid key for Point. Expected one of {'x', 'y'}. | ||
--[builtins fixtures/dict.pyi] | ||
[case testCanGetItemOfTypedDictWithValidBytesOrUnicodeLiteralKey] | ||
# flags: --python-version 2.7 | ||
from mypy_extensions import TypedDict | ||
Cell = TypedDict('Cell', {'value': int}) | ||
c = Cell(value=42) | ||
reveal_type(c['value']) # E: Revealed type is 'builtins.int' | ||
reveal_type(c[u'value']) # E: Revealed type is 'builtins.int' | ||
[builtins_py2 fixtures/dict.pyi] | ||
|
||
-- TODO: Implement support for this case. | ||
--[case testCannotGetItemOfTypedDictWithNonLiteralKey] | ||
--from mypy_extensions import TypedDict | ||
--from typing import Union | ||
--TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int}) | ||
--p = TaggedPoint(type='2d', x=42, y=1337) | ||
--def get_coordinate(p: TaggedPoint, key: str) -> Union[str, int]: | ||
-- return p[key] # E: ... Cannot prove 'key' is a valid key for Point. Expected one of {'x', 'y'} | ||
--[builtins fixtures/dict.pyi] | ||
[case testCannotGetItemOfTypedDictWithInvalidStringLiteralKey] | ||
from mypy_extensions import TypedDict | ||
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int}) | ||
p = TaggedPoint(type='2d', x=42, y=1337) | ||
p['z'] # E: 'z' is not a valid item name; expected one of ['type', 'x', 'y'] | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testCannotGetItemOfTypedDictWithNonLiteralKey] | ||
from mypy_extensions import TypedDict | ||
from typing import Union | ||
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int}) | ||
p = TaggedPoint(type='2d', x=42, y=1337) | ||
def get_coordinate(p: TaggedPoint, key: str) -> Union[str, int]: | ||
return p[key] # E: Cannot prove expression is a valid item name; expected one of ['type', 'x', 'y'] | ||
[builtins fixtures/dict.pyi] | ||
|
||
|
||
-- Special Method: __setitem__ | ||
|
||
-- TODO: Implement support for this case. | ||
--[case testCanSetItemOfTypedDictWithValidStringLiteralKey] | ||
--from mypy_extensions import TypedDict | ||
--TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int}) | ||
--p = TaggedPoint(type='2d', x=42, y=1337) | ||
--def set_x(p: TaggedPoint, x: int) -> None: | ||
-- p['x'] = x | ||
--[builtins fixtures/dict.pyi] | ||
[case testCanSetItemOfTypedDictWithValidStringLiteralKeyAndCompatibleValueType] | ||
from mypy_extensions import TypedDict | ||
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int}) | ||
p = TaggedPoint(type='2d', x=42, y=1337) | ||
p['type'] = 'two_d' | ||
p['x'] = 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also try |
||
[builtins fixtures/dict.pyi] | ||
|
||
-- TODO: Implement support for this case. | ||
--[case testCannotSetItemOfTypedDictWithInvalidStringLiteralKey] | ||
--from mypy_extensions import TypedDict | ||
--TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int}) | ||
--p = TaggedPoint(type='2d', x=42, y=1337) | ||
--def set_z(p: TaggedPoint, z: int) -> None: | ||
-- p['z'] = z # E: ... 'z' is not a valid key for Point. Expected one of {'x', 'y'}. | ||
--[builtins fixtures/dict.pyi] | ||
[case testCannotSetItemOfTypedDictWithIncompatibleValueType] | ||
from mypy_extensions import TypedDict | ||
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int}) | ||
p = TaggedPoint(type='2d', x=42, y=1337) | ||
p['x'] = 'y' # E: Argument 2 has incompatible type "str"; expected "int" | ||
[builtins fixtures/dict.pyi] | ||
|
||
-- TODO: Implement support for this case. | ||
--[case testCannotSetItemOfTypedDictWithNonLiteralKey] | ||
--from mypy_extensions import TypedDict | ||
--from typing import Union | ||
--TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int}) | ||
--p = TaggedPoint(type='2d', x=42, y=1337) | ||
--def set_coordinate(p: TaggedPoint, key: str, value: Union[str, int]) -> None: | ||
-- p[key] = value # E: ... Cannot prove 'key' is a valid key for Point. Expected one of {'x', 'y'} | ||
--[builtins fixtures/dict.pyi] | ||
[case testCannotSetItemOfTypedDictWithInvalidStringLiteralKey] | ||
from mypy_extensions import TypedDict | ||
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int}) | ||
p = TaggedPoint(type='2d', x=42, y=1337) | ||
p['z'] = 1 # E: 'z' is not a valid item name; expected one of ['type', 'x', 'y'] | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testCannotSetItemOfTypedDictWithNonLiteralKey] | ||
from mypy_extensions import TypedDict | ||
from typing import Union | ||
TaggedPoint = TypedDict('TaggedPoint', {'type': str, 'x': int, 'y': int}) | ||
p = TaggedPoint(type='2d', x=42, y=1337) | ||
def set_coordinate(p: TaggedPoint, key: str, value: int) -> None: | ||
p[key] = value # E: Cannot prove expression is a valid item name; expected one of ['type', 'x', 'y'] | ||
[builtins fixtures/dict.pyi] | ||
|
||
|
||
-- Special Method: get | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add test case for both
'foo'
andu'foo'
key names in Python 2 mode.