Skip to content

Commit 29d155a

Browse files
committed
typeddicts - operations with variable keys
1 parent 7e462db commit 29d155a

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

conformance/tests/typeddicts_operations.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,21 @@ class Movie(TypedDict):
3232
movie = {"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.
4152
movie.get("name")
@@ -64,3 +75,19 @@ class MovieOptional(TypedDict, total=False):
6475
movie_optional.popitem() # E: popitem not allowed
6576

6677
del 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

Comments
 (0)