Skip to content

Commit

Permalink
Do not allow other keywords as well
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Nov 10, 2023
1 parent ba7e309 commit aafc409
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
2 changes: 0 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1745,8 +1745,6 @@ def analyze_typeddict_classdef(self, defn: ClassDef) -> bool:
if info is None:
self.mark_incomplete(defn.name, defn)
else:
if defn.keywords and "metaclass" in defn.keywords:
self.fail('"TypedDict" cannot have a metaclass', defn.keywords["metaclass"])
self.prepare_class_def(defn, info, custom_names=True)
return True
return False
Expand Down
6 changes: 6 additions & 0 deletions mypy/semanal_typeddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ def analyze_typeddict_classdef_fields(
total: bool | None = True
if "total" in defn.keywords:
total = require_bool_literal_argument(self.api, defn.keywords["total"], "total", True)
if defn.keywords and defn.keywords.keys() != {"total"}:
msg = (
'"TypedDict" class definition cannot have keywords '
'other than "total", got: "{}"'
)
self.fail(msg.format(", ".join(defn.keywords)), defn)
required_keys = {
field
for (field, t) in zip(fields, types)
Expand Down
15 changes: 6 additions & 9 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -3404,21 +3404,18 @@ T = TypeVar('T')

class Meta(type): ...

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

class GenericWithMetaKeyword(TypedDict, Generic[T], metaclass=Meta): # E: "TypedDict" cannot have a metaclass
...

class PositionOfErrorIsPrecise(
TypedDict,
metaclass=Meta, # E: "TypedDict" cannot have a metaclass
):
class GenericWithMetaKeyword(TypedDict, Generic[T], metaclass=Meta): # E: "TypedDict" class definition cannot have keywords other than "total", got: "metaclass"
...

# We still don't allow this, because the implementation is much easier
# and it does not make any practical sense to do it:
class WithTypeMeta(TypedDict, metaclass=type): # E: "TypedDict" cannot have a metaclass
class WithTypeMeta(TypedDict, metaclass=type): # E: "TypedDict" class definition cannot have keywords other than "total", got: "metaclass"
...

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"
...
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

0 comments on commit aafc409

Please sign in to comment.