Skip to content

Commit 3b27904

Browse files
authored
Accept dict() in TypedDict context (#8749)
Previously only {} was accepted for an empty TypedDict, which was inconsistent.
1 parent 2c8d76e commit 3b27904

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

mypy/semanal.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,12 +3539,10 @@ def visit_call_expr(self, expr: CallExpr) -> None:
35393539
self.add_exports(expr.args[0].items)
35403540

35413541
def translate_dict_call(self, call: CallExpr) -> Optional[DictExpr]:
3542-
"""Translate 'dict(x=y, ...)' to {'x': y, ...}.
3542+
"""Translate 'dict(x=y, ...)' to {'x': y, ...} and 'dict()' to {}.
35433543
35443544
For other variants of dict(...), return None.
35453545
"""
3546-
if not call.args:
3547-
return None
35483546
if not all(kind == ARG_NAMED for kind in call.arg_kinds):
35493547
# Must still accept those args.
35503548
for a in call.args:

test-data/unit/check-typeddict.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,3 +2042,15 @@ class SomeGeneric(Generic[_DICT_T]):
20422042
self._data['foo'] = 1
20432043
[builtins fixtures/dict.pyi]
20442044
[typing fixtures/typing-typeddict.pyi]
2045+
2046+
[case testTypedDictCreatedWithEmptyDict]
2047+
from typing import TypedDict
2048+
2049+
class TD(TypedDict, total=False):
2050+
foo: int
2051+
bar: int
2052+
2053+
d: TD = dict()
2054+
d2: TD = dict(foo=1)
2055+
[builtins fixtures/dict.pyi]
2056+
[typing fixtures/typing-typeddict.pyi]

0 commit comments

Comments
 (0)