8000 Implement class syntax for TypedDict by ilevkivskyi · Pull Request #2808 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

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 5 commits into from
Feb 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix tests; formatting
  • Loading branch information
ilevkivskyi committed Feb 5, 2017
commit 368684586ff36fd202d6763d64e70b88d47cd1bb
9 changes: 5 additions & 4 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,18 +963,19 @@ def analyze_typeddict_classdef(self, defn: ClassDef) -> bool:
node = self.lookup(defn.name, defn)
if node is not None:
node.kind = GDEF # TODO in process_namedtuple_definition also applies here
if (len(defn.base_type_exprs) and isinstance(defn.base_type_exprs[0], RefExpr) and
if (len(defn.base_type_exprs) == 1 and
isinstance(defn.base_type_exprs[0], RefExpr) and
defn.base_type_exprs[0].fullname == 'mypy_extensions.TypedDict'):
# Building a new TypedDict
fields, types = self.check_typeddict_classdef(defn)
node.node = self.build_typeddict_typeinfo(defn.name, fields, types)
return True
# Extending/merging existing TypedDicts
if any(not isinstance(expr, RefExpr) or
expr.fullname != 'mypy_extensions.TypedDict' and not self.is_typeddict(expr)
for expr in defn.base_type_exprs):
expr.fullname != 'mypy_extensions.TypedDict' and
not self.is_typeddict(expr) for expr in defn.base_type_exprs):
self.fail("All bases of a new TypedDict must be TypedDict's", defn)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammar nit: Replace TypedDict's with TypedDict types or similar.

fields, types = self.check_typeddict_classdef(defn)
# Extending/merging existing TypedDicts
typeddict_bases = list(filter(self.is_typeddict, defn.base_type_exprs))
newfields = [] # type: List[str]
newtypes = [] # type: List[Type]
Expand Down
14 changes: 7 additions & 7 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ class Point1D(TypedDict):
x: int
class Point2D(Point1D):
y: int

r: Point1D
p: Point2D
reveal_type(p) # E: Revealed type is 'TypedDict(x=builtins.int, y=builtins.int, _fallback=typing.Mapping[builtins.str, builtins.int])'
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]
Expand All @@ -100,7 +101,7 @@ class Point2D(TypedDict, Point1D): # We also allow to include TypedDict in bases
y: int

p: Point2D
reveal_type(p) # E: Revealed type is 'TypedDict(x=builtins.int, y=builtins.int, _fallback=typing.Mapping[builtins.str, builtins.int])'
reveal_type(p) # E: Revealed type is 'TypedDict(x=builtins.int, y=builtins.int, _fallback=__main__.Point2D)'
[builtins fixtures/dict.pyi]

[case testCanCreateTypedDictClassEmpty]
Expand All @@ -122,8 +123,7 @@ reveal_type(p) # E: Revealed type is 'TypedDict(_fallback=typing.Mapping[builti
from mypy_extensions import TypedDict

class Point(TypedDict): # E: TypedDict class syntax is only supported in Python 3.6
x: int
y: int
pass
[builtins fixtures/dict.pyi]

[case testCannotCreateTypedDictWithClassOtherBases]
Expand All @@ -138,7 +138,7 @@ class Point2D(Point1D, A): # E: All bases of a new TypedDict must be TypedDict's
y: int

p: Point2D
reveal_type(p) # E: Revealed type is 'TypedDict(x=builtins.int, y=builtins.int, _fallback=typing.Mapping[builtins.str, builtins.int])'
reveal_type(p) # E: Revealed type is 'TypedDict(x=builtins.int, y=builtins.int, _fallback=__main__.Point2D)'
[builtins fixtures/dict.pyi]

[case testCannotCreateTypedDictWithClassWithOtherStuff]
Expand All @@ -164,7 +164,7 @@ class Point(TypedDict):
_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=typing.Mapping[builtins.str, builtins.int])'
reveal_type(p) # E: Revealed type is 'TypedDict(x=builtins.int, _y=builtins.int, _fallback=__main__.Point)'
[builtins fixtures/dict.pyi]


Expand Down
0