-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Implement class syntax for TypedDict #2808
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
5 commits
Select commit
Hold shift + click to select a range
8f35601
Class syntax for TypedDict
ilevkivskyi 19044c1
Add tests; fix minor points
ilevkivskyi 3686845
Fix tests; formatting
ilevkivskyi dca9810
Prohibit overwriting fields on merging/extending
ilevkivskyi ee08aa8
Response to review comments
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,151 @@ p = Point(x='meaning_of_life', y=1337) # E: Incompatible types (expression has | |
[builtins fixtures/dict.pyi] | ||
|
||
|
||
-- Define TypedDict (Class syntax) | ||
|
||
[case testCanCreateTypedDictWithClass] | ||
# flags: --python-version 3.6 | ||
from mypy_extensions import TypedDict | ||
|
||
class Point(TypedDict): | ||
x: int | ||
y: int | ||
|
||
p = Point(x=42, y=1337) | ||
reveal_type(p) # E: Revealed type is 'TypedDict(x=builtins.int, y=builtins.int, _fallback=typing.Mapping[builtins.str, builtins.int])' | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testCanCreateTypedDictWithSubclass] | ||
# flags: --python-version 3.6 | ||
from mypy_extensions import TypedDict | ||
|
||
class Point1D(TypedDict): | ||
x: int | ||
class Point2D(Point1D): | ||
y: int | ||
r: Point1D | ||
p: Point2D | ||
reveal_type(r) # E: Revealed type is 'TypedDict(x=builtins.int, _fallback=__main__.Point1D)' | ||
reveal_type(p) # E: Revealed type is 'TypedDict(x=builtins.int, y=builtins.int, _fallback=__main__.Point2D)' | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testCanCreateTypedDictWithSubclass2] | ||
# flags: --python-version 3.6 | ||
from mypy_extensions import TypedDict | ||
|
||
class Point1D(TypedDict): | ||
x: int | ||
class Point2D(TypedDict, Point1D): # We also allow to include TypedDict in bases, it is simply ignored at runtime | ||
y: int | ||
|
||
p: Point2D | ||
reveal_type(p) # E: Revealed type is 'TypedDict(x=builtins.int, y=builtins.int, _fallback=__main__.Point2D)' | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testCanCreateTypedDictClassEmpty] | ||
# flags: --python-version 3.6 | ||
from mypy_extensions import TypedDict | ||
|
||
class EmptyDict(TypedDict): | ||
pass | ||
|
||
p = EmptyDict() | ||
reveal_type(p) # E: Revealed type is 'TypedDict(_fallback=typing.Mapping[builtins.str, builtins.None])' | ||
[builtins fixtures/dict.pyi] | ||
|
||
|
||
-- Define TypedDict (Class syntax errors) | ||
|
||
[case testCanCreateTypedDictWithClassOldVersion] | ||
# flags: --python-version 3.5 | ||
from mypy_extensions import TypedDict | ||
|
||
class Point(TypedDict): # E: TypedDict class syntax is only supported in Python 3.6 | ||
pass | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testCannotCreateTypedDictWithClassOtherBases] | ||
# flags: --python-version 3.6 | ||
from mypy_extensions import TypedDict | ||
|
||
class A: pass | ||
|
||
class Point1D(TypedDict, A): # E: All bases of a new TypedDict must be TypedDict types | ||
x: int | ||
class Point2D(Point1D, A): # E: All bases of a new TypedDict must be TypedDict types | ||
y: int | ||
|
||
p: Point2D | ||
reveal_type(p) # E: Revealed type is 'TypedDict(x=builtins.int, y=builtins.int, _fallback=__main__.Point2D)' | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testCannotCreateTypedDictWithClassWithOtherStuff] | ||
# flags: --python-version 3.6 | ||
from mypy_extensions import TypedDict | ||
|
||
class Point(TypedDict): | ||
x: int | ||
y: int = 1 # E: Right hand side values are not supported in TypedDict | ||
def f(): pass # E: Invalid statement in TypedDict definition; expected "field_name: field_type" | ||
z = int # E: Invalid statement in TypedDict definition; expected "field_name: field_type" | ||
|
||
p = Point(x=42, y=1337, z='whatever') | ||
reveal_type(p) # E: Revealed type is 'TypedDict(x=builtins.int, y=builtins.int, z=builtins.str, _fallback=typing.Mapping[builtins.str, builtins.object])' | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testCannotCreateTypedDictWithClassUnderscores] | ||
# flags: --python-version 3.6 | ||
from mypy_extensions import TypedDict | ||
|
||
class Point(TypedDict): | ||
x: int | ||
_y: int # E: TypedDict field name cannot start with an underscore: _y | ||
|
||
p: Point | ||
reveal_type(p) # E: Revealed type is 'TypedDict(x=builtins.int, _y=builtins.int, _fallback=__main__.Point)' | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testCannotCreateTypedDictWithClassOverwriting] | ||
# flags: --python-version 3.6 | ||
from mypy_extensions import TypedDict | ||
|
||
class Bad(TypedDict): | ||
x: int | ||
x: str # E: Duplicate TypedDict field "x" | ||
|
||
b: Bad | ||
reveal_type(b) # E: Revealed type is 'TypedDict(x=builtins.int, _fallback=__main__.Bad)' | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testCannotCreateTypedDictWithClassOverwriting2] | ||
# flags: --python-version 3.6 | ||
from mypy_extensions import TypedDict | ||
|
||
class Point1(TypedDict): | ||
x: int | ||
class Point2(TypedDict): | ||
x: float | ||
class Bad(Point1, Point2): # E: Cannot overwrite TypedDict field "x" while merging | ||
pass | ||
|
||
b: Bad | ||
reveal_type(b) # E: Revealed type is 'TypedDict(x=builtins.int, _fallback=__main__.Bad)' | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testCannotCreateTypedDictWithClassOverwriting2] | ||
# flags: --python-version 3.6 | ||
from mypy_extensions import TypedDict | ||
|
||
class Point1(TypedDict): | ||
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. Add test case for duplicate fields within a single definition. Example:
|
||
x: int | ||
class Point2(Point1): | ||
x: float # E: Cannot overwrite TypedDict field "x" while extending | ||
|
||
p2: Point2 | ||
reveal_type(p2) # E: Revealed type is 'TypedDict(x=builtins.int, _fallback=__main__.Point2)' | ||
[builtins fixtures/dict.pyi] | ||
|
||
|
||
-- Subtyping | ||
|
||
[case testCanConvertTypedDictToItself] | ||
|
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.
Why not allow fields starting with underscores? Existing JSON data may use underscore prefixes, I think.
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.
I agree this is probably an unnecessary limitation (I copied it from the functional form, and that one probably copied it from
namedtuple
). Maybe it is worth making a separate PR removing this limitation from both forms?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.
That sounds reasonable.
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.
The no-underscore-prefix limitation allows extending the syntax of the functional form with new keywords in the future if it becomes useful.
I believe the no-underscore-prefix limitation for namedtuples used a similar rationale.