Skip to content

Commit 8b65bd5

Browse files
committed
Implement closed support in astdiff
1 parent 6a18078 commit 8b65bd5

3 files changed

Lines changed: 62 additions & 1 deletion

File tree

mypy/server/astdiff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ def visit_typeddict_type(self, typ: TypedDictType) -> SnapshotItem:
497497
items = tuple((key, snapshot_type(item_type)) for key, item_type in typ.items.items())
498498
required = tuple(sorted(typ.required_keys))
499499
readonly = tuple(sorted(typ.readonly_keys))
500-
return ("TypedDictType", items, required, readonly)
500+
return ("TypedDictType", items, required, readonly, typ.is_closed)
501501

502502
def visit_literal_type(self, typ: LiteralType) -> SnapshotItem:
503503
return ("LiteralType", snapshot_type(typ.fallback), typ.value)

test-data/unit/diff.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,24 @@ p = Point(dict(x=42, y=1337))
676676
__main__.Point
677677
__main__.p
678678

679+
[case testTypedDict5]
680+
from typing import TypedDict
681+
class Point(TypedDict):
682+
x: int
683+
y: int
684+
p = Point(dict(x=42, y=1337))
685+
[file next.py]
686+
from typing import TypedDict
687+
class Point(TypedDict, closed=True):
688+
x: int
689+
y: int
690+
p = Point(dict(x=42, y=1337))
691+
[builtins fixtures/dict.pyi]
692+
[typing fixtures/typing-typeddict.pyi]
693+
[out]
694+
__main__.Point
695+
__main__.p
696+
679697
[case testTypeAliasSimple]
680698
A = int
681699
B = int

test-data/unit/fine-grained.test

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3745,6 +3745,49 @@ b.py:4: error: ReadOnly TypedDict key "y" TypedDict is mutated
37453745
==
37463746
b.py:3: error: ReadOnly TypedDict key "x" TypedDict is mutated
37473747

3748+
[case testTypedDictUpdateClosed]
3749+
import b
3750+
[file a.py]
3751+
from typing import TypedDict, Union
3752+
class A(TypedDict):
3753+
a: int
3754+
class B(TypedDict, closed=True):
3755+
b: int
3756+
C = Union[A, B]
3757+
[file a.py.2]
3758+
from typing import TypedDict, Union
3759+
class A(TypedDict, closed=True):
3760+
a: int
3761+
class B(TypedDict):
3762+
b: int
3763+
C = Union[A, B]
3764+
[file a.py.3]
3765+
from typing import TypedDict, Union
3766+
class A(TypedDict, closed=True):
3767+
a: int
3768+
class B(TypedDict, closed=True):
3769+
b: int
3770+
C = Union[A, B]
3771+
[file b.py]
3772+
from a import C
3773+
def foo(x: C) -> int:
3774+
if "b" in x:
3775+
return x["b"]
3776+
else:
3777+
return x["a"]
3778+
def bar(x: C) -> int:
3779+
if "a" in x:
3780+
return x["a"]
3781+
else:
3782+
return x["b"]
3783+
[builtins fixtures/dict.pyi]
3784+
[typing fixtures/typing-typeddict.pyi]
3785+
[out]
3786+
b.py:4: error: TypedDict "A" has no key "b"
3787+
==
3788+
b.py:9: error: TypedDict "B" has no key "a"
3789+
==
3790+
37483791
[case testBasicAliasUpdate]
37493792
import b
37503793
[file a.py]

0 commit comments

Comments
 (0)