Skip to content

Handle TypedDict in diff and deps #4510

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 3 commits into from
Jan 29, 2018
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
2 changes: 2 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,7 @@ def analyze_typeddict_classdef(self, defn: ClassDef) -> bool:
fields, types, required_keys = self.check_typeddict_classdef(defn)
info = self.build_typeddict_typeinfo(defn.name, fields, types, required_keys)
defn.info.replaced = info
defn.info = info
node.node = info
defn.analyzed = TypedDictExpr(info)
defn.analyzed.line = defn.line
Expand Down Expand Up @@ -1360,6 +1361,7 @@ def analyze_typeddict_classdef(self, defn: ClassDef) -> bool:
required_keys.update(new_required_keys)
info = self.build_typeddict_typeinfo(defn.name, keys, types, required_keys)
defn.info.replaced = info
defn.info = info
node.node = info
defn.analyzed = TypedDictExpr(info)
defn.analyzed.line = defn.line
Expand Down
7 changes: 5 additions & 2 deletions mypy/server/astdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def visit_typeddict_type(self, left: TypedDictType) -> bool:
if isinstance(self.right, TypedDictType):
if left.items.keys() != self.right.items.keys():
return False
if left.required_keys != self.right.required_keys:
return False
for (_, left_item_type, right_item_type) in left.zip(self.right):
if not is_identical_type(left_item_type, right_item_type):
return False
Expand Down Expand Up @@ -306,13 +308,13 @@ def snapshot_definition(node: Optional[SymbolNode],
# type_vars
# bases
# _promote
# typeddict_type
attrs = (node.is_abstract,
node.is_enum,
node.fallback_to_any,
node.is_named_tuple,
node.is_newtype,
snapshot_optional_type(node.tuple_type),
snapshot_optional_type(node.typeddict_type),
[base.fullname() for base in node.mro])
prefix = node.fullname()
symbol_table = snapshot_symbol_table(prefix, node.names)
Expand Down Expand Up @@ -407,7 +409,8 @@ def visit_tuple_type(self, typ: TupleType) -> SnapshotItem:
def visit_typeddict_type(self, typ: TypedDictType) -> SnapshotItem:
items = tuple((key, snapshot_type(item_type))
for key, item_type in typ.items.items())
return ('TypedDictType', items)
required = tuple(sorted(typ.required_keys))
return ('TypedDictType', items, required)

def visit_union_type(self, typ: UnionType) -> SnapshotItem:
# Sort and remove duplicates so that we can use equality to test for
Expand Down
19 changes: 14 additions & 5 deletions mypy/server/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class 'mod.Cls'. This can also refer to an attribute inherited from a
ImportFrom, CallExpr, CastExpr, TypeVarExpr, TypeApplication, IndexExpr, UnaryExpr, OpExpr,
ComparisonExpr, GeneratorExpr, DictionaryComprehension, StarExpr, PrintStmt, ForStmt, WithStmt,
TupleExpr, ListExpr, OperatorAssignmentStmt, DelStmt, YieldFromExpr, Decorator, Block,
TypeInfo, FuncBase, OverloadedFuncDef, RefExpr, SuperExpr, Var, NamedTupleExpr,
TypeInfo, FuncBase, OverloadedFuncDef, RefExpr, SuperExpr, Var, NamedTupleExpr, TypedDictExpr,
LDEF, MDEF, GDEF,
op_methods, reverse_op_methods, ops_with_inplace_method, unary_op_methods
)
Expand Down Expand Up @@ -151,7 +151,6 @@ def __init__(self,
# TODO (incomplete):
# from m import *
# await
# TypedDict
# protocols
# metaclasses
# type aliases
Expand Down Expand Up @@ -199,6 +198,8 @@ def visit_class_def(self, o: ClassDef) -> None:
self.add_type_dependencies(base, target=target)
if o.info.tuple_type:
self.add_type_dependencies(o.info.tuple_type, target=make_trigger(target))
if o.info.typeddict_type:
Copy link
Member

Choose a reason for hiding this comment

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

I am mostly in favor of "unification" of TypedDict and NamedTuple, but why can't you just use o.info.replaced.typeddict_type here? (Then you wouldn't need to change semanal.py.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

replaced doesn't get serialized, so the right thing to do seemed to be following NamedTuple and actually doing the replacement.

self.add_type_dependencies(o.info.typeddict_type, target=make_trigger(target))
# TODO: Add dependencies based on remaining TypeInfo attributes.
super().visit_class_def(o)
self.is_class = old_is_class
Expand Down Expand Up @@ -237,7 +238,6 @@ def visit_block(self, o: Block) -> None:

def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
# TODO: Implement all assignment special forms, including these:
# TypedDict
# Enum
# type aliases
rvalue = o.rvalue
Expand All @@ -258,6 +258,12 @@ def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
self.add_type_dependencies(typ, target=make_trigger(prefix))
attr_target = make_trigger('%s.%s' % (prefix, name))
self.add_type_dependencies(typ, target=attr_target)
elif isinstance(rvalue, CallExpr) and isinstance(rvalue.analyzed, TypedDictExpr):
# Depend on the underlying typeddict type
info = rvalue.analyzed.info
assert info.typeddict_type is not None
prefix = '%s.%s' % (self.scope.current_full_target(), info.name())
self.add_type_dependencies(info.typeddict_type, target=make_trigger(prefix))
else:
# Normal assignment
super().visit_assignment_stmt(o)
Expand Down Expand Up @@ -703,8 +709,11 @@ def visit_type_var(self, typ: TypeVarType) -> List[str]:
return triggers

def visit_typeddict_type(self, typ: TypedDictType) -> List[str]:
# TODO: implement
return []
triggers = []
for item in typ.items.values():
triggers.extend(get_type_triggers(item))
triggers.extend(get_type_triggers(typ.fallback))
return triggers

def visit_unbound_type(self, typ: UnboundType) -> List[str]:
return []
Expand Down
44 changes: 44 additions & 0 deletions test-data/unit/deps.test
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,47 @@ x = 1
<pkg.a> -> pkg.mod
<pkg.mod> -> pkg
<pkg> -> m

[case testTypedDict]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': int})
p = Point(dict(x=42, y=1337))
def foo(x: Point) -> int:
return x['x'] + x['y']
[builtins fixtures/dict.pyi]
[out]
<m.Point> -> <m.foo>, <m.p>, m, m.foo
<m.p> -> m
<mypy_extensions.TypedDict> -> m

[case testTypedDict2]
from mypy_extensions import TypedDict
class A: pass
Point = TypedDict('Point', {'x': int, 'y': A})
p = Point(dict(x=42, y=A()))
def foo(x: Point) -> int:
return x['x']
[builtins fixtures/dict.pyi]
[out]
<m.A.__init__> -> m
<m.A> -> <m.Point>, <m.foo>, <m.p>, m, m.A, m.foo
<m.Point> -> <m.foo>, <m.p>, m, m.foo
<m.p> -> m
<mypy_extensions.TypedDict> -> m

[case testTypedDict3]
from mypy_extensions import TypedDict
class A: pass
class Point(TypedDict):
x: int
y: A
p = Point(dict(x=42, y=A()))
def foo(x: Point) -> int:
return x['x']
[builtins fixtures/dict.pyi]
[out]
<m.A.__init__> -> m
<m.A> -> <m.Point>, <m.foo>, <m.p>, m, m.A, m.foo
<m.Point> -> <m.foo>, <m.p>, m, m.Point, m.foo
<m.p> -> m
<mypy_extensions.TypedDict> -> m
56 changes: 56 additions & 0 deletions test-data/unit/diff.test
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,59 @@ B = Enum('B', 'y')
[out]
__main__.B.x
__main__.B.y

[case testTypedDict]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': int})
p = Point(dict(x=42, y=1337))
[file next.py]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': str})
p = Point(dict(x=42, y='lurr'))
[builtins fixtures/dict.pyi]
[out]
__main__.Point
__main__.p

[case testTypedDict2]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Test also that removing or adding a field triggers a TypedDict type. And test that changing totality triggers the type.

from mypy_extensions import TypedDict
class Point(TypedDict):
x: int
y: int
p = Point(dict(x=42, y=1337))
[file next.py]
from mypy_extensions import TypedDict
class Point(TypedDict):
x: int
y: str
p = Point(dict(x=42, y='lurr'))
[builtins fixtures/dict.pyi]
[out]
__main__.Point
__main__.p

[case testTypedDict3]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': int})
p = Point(dict(x=42, y=1337))
[file next.py]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int})
p = Point(dict(x=42))
[builtins fixtures/dict.pyi]
[out]
__main__.Point
__main__.p

[case testTypedDict4]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': int})
p = Point(dict(x=42, y=1337))
[file next.py]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': int}, total=False)
p = Point(dict(x=42, y=1337))
[builtins fixtures/dict.pyi]
[out]
__main__.Point
__main__.p
56 changes: 56 additions & 0 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -1720,3 +1720,59 @@ def f(x: a.N) -> None:
f(a.x)
[out]
==

[case testTypedDictRefresh]
[builtins fixtures/dict.pyi]
import a
[file a.py]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': int})
p = Point(dict(x=42, y=1337))
[file a.py.2]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': int})
p = Point(dict(x=42, y=1337))
[out]
==

[case testTypedDictUpdate]
import b
[file a.py]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': int})
p = Point(dict(x=42, y=1337))
[file a.py.2]
from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': int, 'y': str})
p = Point(dict(x=42, y='lurr'))
[file b.py]
from a import Point
def foo(x: Point) -> int:
return x['x'] + x['y']
[builtins fixtures/dict.pyi]
[out]
==
b.py:3: error: Unsupported operand types for + ("int" and "str")

[case testTypedDictUpdate2]
import b
[file a.py]
from mypy_extensions import TypedDict
class Point(TypedDict):
x: int
y: int
p = Point(dict(x=42, y=1337))
[file a.py.2]
from mypy_extensions import TypedDict
class Point(TypedDict):
x: int
y: str
p = Point(dict(x=42, y='lurr'))
[file b.py]
from a import Point
def foo(x: Point) -> int:
return x['x'] + x['y']
[builtins fixtures/dict.pyi]
[out]
==
b.py:3: error: Unsupported operand types for + ("int" and "str")
4 changes: 2 additions & 2 deletions test-data/unit/semanal-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ MypyFile:1(
ImportFrom:1(mypy_extensions, [TypedDict])
ClassDef:2(
A
BaseTypeExpr(
NameExpr(TypedDict [mypy_extensions.TypedDict]))
BaseType(
typing.Mapping[builtins.str, builtins.str])
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's this change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A consequence of the semanal changes that replace the TypeInfo with the replaced version.

ExpressionStmt:3(
StrExpr(foo))
AssignmentStmt:4(
Expand Down