1- from typing import TypedDict , Tuple , Union
1+ from typing import TypedDict
22from typing_extensions import NotRequired , Required
33
44
@@ -8,19 +8,18 @@ class Movie(TypedDict, total=False):
88 year : int
99
1010
11- m = Movie (title = ' The Matrix' , year = 1999 )
11+ m = Movie (title = " The Matrix" , year = 1999 )
1212# m = Movie()
1313print (m )
1414
1515
1616# --- Assignment Based TypedDict ---
17- Movie2 = TypedDict ('Movie2' , {
18- 'title' : Required [str ],
19- 'year' : int ,
20- }, total = False )
17+ class Movie2 (TypedDict , total = False ):
18+ title : Required [str ]
19+ year : int
2120
2221
23- m2 = Movie2 (title = ' The Matrix Reloaded' , year = 2003 )
22+ m2 = Movie2 (title = " The Matrix Reloaded" , year = 2003 )
2423# m2 = Movie2()
2524print (m2 )
2625
@@ -31,19 +30,19 @@ class Movie(TypedDict, total=False):
3130
3231
3332# --- Required[] inside other Required[] (error) ---
34- '''
33+ """
3534Movie3 = TypedDict('Movie3', {
3635 'title': Required[Union[
3736 Required[str],
3837 bytes
3938 ]],
4039 'year': int,
4140}, total=False)
42- '''
41+ """
4342
4443
4544# --- Required[] used within TypedDict but not at top level (error) ---
46- '''
45+ """
4746Movie4 = TypedDict('Movie4', {
4847 'title': Union[
4948 Required[str],
@@ -58,7 +57,7 @@ class Movie(TypedDict, total=False):
5857 ],
5958 'year': int,
6059}, total=False)
61- '''
60+ """
6261
6362
6463# ==============================================================================
@@ -68,18 +67,17 @@ class MovieN(TypedDict):
6867 year : NotRequired [int ]
6968
7069
71- m = MovieN (title = ' The Matrix' , year = 1999 )
70+ m = MovieN (title = " The Matrix" , year = 1999 )
7271# m = MovieN()
7372print (m )
7473
7574
7675# --- Assignment Based TypedDict ---
77- MovieN2 = TypedDict ('MovieN2' , {
78- 'title' : str ,
79- 'year' : NotRequired [int ],
80- })
76+ class MovieN2 (TypedDict ):
77+ title : str
78+ year : NotRequired [int ]
8179
8280
83- m2 = MovieN2 (title = ' The Matrix Reloaded' , year = 2003 )
81+ m2 = MovieN2 (title = " The Matrix Reloaded" , year = 2003 )
8482# m2 = MovieN2()
85- print (m2 )
83+ print (m2 )
0 commit comments