2Tests the "alternative" (non-class) syntax for defining a TypedDict.
5# Specification: https://typing.readthedocs.io/en/latest/spec/typeddict.html#alternative-syntax
7from typing import TypedDict
10Movie = TypedDict("Movie", {"name": str, "year": int, "illegal key name": bool})
12movie: Movie = {"name": "Blade Runner", "year": 1982, "illegal key name": True}
14MovieOptional = TypedDict("MovieOptional", {"name": str, "year": int}, total=False)
16movie_opt1: MovieOptional = {}
17movie_opt2: MovieOptional = {"year": 1982}
19# > A type checker is only expected to accept a dictionary display expression as
20# > the second argument to TypedDict. In particular, a variable that refers to a
21# > dictionary object does not need to be supported, to simplify implementation.
22my_dict = {"name": str}
23BadTypedDict1 = TypedDict("BadTypedDict1", my_dict) # E
Expected a ty diagnostic for this line
26# This should generate an error because it uses a non-str key.
27BadTypedDict2 = TypedDict("BadTypedDict2", {1: str}) # E
Expected a ty diagnostic for this line
30# This should generate an error because it uses a non-matching name.
31BadTypedDict3 = TypedDict("WrongName", {"name": str}) # E
Expected a ty diagnostic for this line
34# This should generate an error because of the additional parameter.
35BadTypedDict4 = TypedDict("BadTypedDict4", {"name": str}, total=False, other=False) # E
Expected a ty diagnostic for this line
38# > The keyword-argument syntax is deprecated in 3.11 and will be removed in 3.13.
39# > It may also be unsupported by static type checkers.
41Movie2 = TypedDict("Movie2", name=str, year=int) # E?
44movie2 = {"name": "Blade Runner", "year": 1982} # E?: May generate errors if keyword-argument syntax is unsupported
45movie2 = {"name": "Blade Runner", "year": ""} # E?: Incorrect type for year