2Tests the class syntax for defining a TypedDict.
5# Specification: https://typing.readthedocs.io/en/latest/spec/typeddict.html#typeddict
7from typing import Generic, TypeVar, TypedDict
10class Movie(TypedDict):
14 # > String literal forward references are valid in the value types.
18class Person(TypedDict):
23# > Methods are not allowed, since the runtime type of a TypedDict object
24# > will always be just dict (it is never a subclass of dict).
25class BadTypedDict1(TypedDict):
28 # Methods are not allowed, so this should generate an error.
29 def method1(self): # E
[invalid-typed-dict-statement] TypedDict class cannot have methods
32 # Methods are not allowed, so this should generate an error.
33 @classmethod # E[method2]
Tag 'method2'
[invalid-typed-dict-statement] TypedDict class cannot have methods
34 def method2(cls): # E[method2]
37 # Methods are not allowed, so this should generate an error.
38 @staticmethod # E[method3]
Tag 'method3'
[invalid-typed-dict-statement] TypedDict class cannot have methods
39 def method3(): # E[method3]
43# > Specifying a metaclass is not allowed.
44class BadTypedDict2(TypedDict, metaclass=type): # E
[invalid-typed-dict-header] Custom metaclasses are not supported in `TypedDict` definitions
48# This should generate an error because "other" is not an allowed keyword argument.
49class BadTypedDict3(TypedDict, other=True): # E
[unknown-argument] Unknown keyword argument `other` in `TypedDict` definition
53# > TypedDicts may be made generic by adding Generic[T] among the bases.
57class GenericTypedDict(TypedDict, Generic[T]):
62# > An empty TypedDict can be created by only including pass in the
63# > body (if there is a docstring, pass can be omitted):
64class EmptyDict1(TypedDict):
68class EmptyDict2(TypedDict):
72class MovieTotal(TypedDict, total=True):
76class MovieOptional(TypedDict, total=False):