@@ -32,10 +32,21 @@ class Movie(TypedDict):
3232movie = {"name" : "" , "year" : 1900 , "other" : 2 } # E: extra key
3333
3434
35- def func1 (variable_key : str ):
35+ def func1 (variable_key : str , existing_movie : Movie ):
3636 # > A key that is not a literal should generally be rejected.
3737 movie : Movie = {variable_key : "" , "year" : 1900 } # E: variable key
3838
39+ # Destructive operations.
40+ existing_movie [variable_key ] = 1982 # E: variable key
41+ del existing_movie [variable_key ] # E
42+
43+ # Read-only operations.
44+ reveal_type (existing_movie [variable_key ]) # E
45+
46+ # Exceptions.
47+ reveal_type (variable_key in existing_movie ) # `bool`
48+ assert_type (existing_movie .get (variable_key ), object | None )
49+
3950
4051# It's not clear from the spec what type this should be.
4152movie .get ("name" )
@@ -64,3 +75,19 @@ class MovieOptional(TypedDict, total=False):
6475movie_optional .popitem () # E: popitem not allowed
6576
6677del movie_optional ["name" ]
78+
79+
80+ def func2 (variable_key : str , existing_optional_movie : MovieOptional ):
81+ # > A key that is not a literal should generally be rejected.
82+ movie_optional : MovieOptional = {variable_key : "" , "year" : 1900 } # E: variable key
83+
84+ # Destructive operations.
85+ existing_optional_movie [variable_key ] = 1982 # E: variable key
86+ del existing_optional_movie [variable_key ] # E: variable key
87+
88+ # Read-only operations.
89+ reveal_type (existing_optional_movie [variable_key ]) # E
90+
91+ # Exceptions.
92+ reveal_type (variable_key in existing_optional_movie ) # `bool`
93+ assert_type (existing_optional_movie .get (variable_key ), object | None )
0 commit comments