Skip to content

Commit 0f4bf10

Browse files
henribruilevkivskyi
authored andcommitted
Ensure that non-total TypedDicts can be falsy (#7745)
Fixes #7743
1 parent 0ba1d99 commit 0f4bf10

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

mypy/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ def __init__(self, items: 'OrderedDict[str, Type]', required_keys: Set[str],
13761376
self.required_keys = required_keys
13771377
self.fallback = fallback
13781378
self.can_be_true = len(self.items) > 0
1379-
self.can_be_false = len(self.items) == 0
1379+
self.can_be_false = len(self.required_keys) == 0
13801380

13811381
def accept(self, visitor: 'TypeVisitor[T]') -> T:
13821382
return visitor.visit_typeddict_type(self)

test-data/unit/check-typeddict.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,25 @@ ll = [b, c]
11261126
f(ll) # E: Argument 1 to "f" has incompatible type "List[TypedDict({'x'?: int, 'z'?: str})]"; expected "A"
11271127
[builtins fixtures/dict.pyi]
11281128

1129+
[case testNonTotalTypedDictCanBeEmpty]
1130+
# flags: --warn-unreachable
1131+
from mypy_extensions import TypedDict
1132+
1133+
class A(TypedDict):
1134+
...
1135+
1136+
class B(TypedDict, total=False):
1137+
x: int
1138+
1139+
a: A = {}
1140+
b: B = {}
1141+
1142+
if not a:
1143+
reveal_type(a) # N: Revealed type is 'TypedDict('__main__.A', {})'
1144+
1145+
if not b:
1146+
reveal_type(b) # N: Revealed type is 'TypedDict('__main__.B', {'x'?: builtins.int})'
1147+
[builtins fixtures/dict.pyi]
11291148

11301149
-- Create Type (Errors)
11311150

0 commit comments

Comments
 (0)