-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_compatibility.py
More file actions
42 lines (29 loc) · 1013 Bytes
/
5_compatibility.py
File metadata and controls
42 lines (29 loc) · 1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from typing import TypedDict
# few keys
class Movie(TypedDict):
name: str
year: int
# more keys
class BookBasedMovie(TypedDict):
name: str
year: int
based_on: str
def print_movie(movie: Movie) -> None:
print(f"{movie['name']} ({movie['year']})")
def print_book_movie(movie: BookBasedMovie) -> None:
print(f"{movie['name']} ({movie['year']}) {movie["based_on"]}")
book_movie: BookBasedMovie = {
"name": "Dune",
"year": 2021,
"based_on": "Dune by Frank Herbert",
}
movie: Movie = {"name": "Dune", "year": 2021}
#Argument of type "Movie" cannot be assigned
# to parameter "movie" of type "BookBasedMovie" in function "print_book_movie"
# "based_on" is missing from "Movie"
# As you can see the function argument defined "movie" of type defined as
# "BookBasedMovie" is missing attribute called "based_on"
# "BookBasedMovie" and "Movie" difference by one additional attribute called
# "based_one"
print_book_movie(movie) # Error
print_movie(book_movie) # OK