Skip to content

Commit aafc409

Browse files
committed
Do not allow other keywords as well
1 parent ba7e309 commit aafc409

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

mypy/semanal.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,8 +1745,6 @@ def analyze_typeddict_classdef(self, defn: ClassDef) -> bool:
17451745
if info is None:
17461746
self.mark_incomplete(defn.name, defn)
17471747
else:
1748-
if defn.keywords and "metaclass" in defn.keywords:
1749-
self.fail('"TypedDict" cannot have a metaclass', defn.keywords["metaclass"])
17501748
self.prepare_class_def(defn, info, custom_names=True)
17511749
return True
17521750
return False

mypy/semanal_typeddict.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@ def analyze_typeddict_classdef_fields(
323323
total: bool | None = True
324324
if "total" in defn.keywords:
325325
total = require_bool_literal_argument(self.api, defn.keywords["total"], "total", True)
326+
if defn.keywords and defn.keywords.keys() != {"total"}:
327+
msg = (
328+
'"TypedDict" class definition cannot have keywords '
329+
'other than "total", got: "{}"'
330+
)
331+
self.fail(msg.format(", ".join(defn.keywords)), defn)
326332
required_keys = {
327333
field
328334
for (field, t) in zip(fields, types)

test-data/unit/check-typeddict.test

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3404,21 +3404,18 @@ T = TypeVar('T')
34043404

34053405
class Meta(type): ...
34063406

3407-
class WithMetaKeyword(TypedDict, metaclass=Meta): # E: "TypedDict" cannot have a metaclass
3407+
class WithMetaKeyword(TypedDict, metaclass=Meta): # E: "TypedDict" class definition cannot have keywords other than "total", got: "metaclass"
34083408
...
34093409

3410-
class GenericWithMetaKeyword(TypedDict, Generic[T], metaclass=Meta): # E: "TypedDict" cannot have a metaclass
3411-
...
3412-
3413-
class PositionOfErrorIsPrecise(
3414-
TypedDict,
3415-
metaclass=Meta, # E: "TypedDict" cannot have a metaclass
3416-
):
3410+
class GenericWithMetaKeyword(TypedDict, Generic[T], metaclass=Meta): # E: "TypedDict" class definition cannot have keywords other than "total", got: "metaclass"
34173411
...
34183412

34193413
# We still don't allow this, because the implementation is much easier
34203414
# and it does not make any practical sense to do it:
3421-
class WithTypeMeta(TypedDict, metaclass=type): # E: "TypedDict" cannot have a metaclass
3415+
class WithTypeMeta(TypedDict, metaclass=type): # E: "TypedDict" class definition cannot have keywords other than "total", got: "metaclass"
3416+
...
3417+
3418+
class OtherKeywords(TypedDict, a=1, b=2, c=3, total=True): # E: "TypedDict" class definition cannot have keywords other than "total", got: "a, b, c, total"
34223419
...
34233420
[builtins fixtures/dict.pyi]
34243421
[typing fixtures/typing-typeddict.pyi]

0 commit comments

Comments
 (0)