Skip to content

Commit 63efa32

Browse files
committed
Refine typed-dict challenges to avoid direct copy of Python documentation
1 parent dbefea0 commit 63efa32

File tree

6 files changed

+100
-84
lines changed

6 files changed

+100
-84
lines changed
Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
"""
22
TODO:
33
4-
Define a class `Point3D` using the predefined Point2D.
5-
`Point3D` needs to have an extra key-value pair: z - int
4+
Define a class `Undergraduate` using the predefined class `Student`.
5+
`Undergraduate` has an extra key `major` of type string
66
"""
77

88
from typing import TypedDict
99

1010

11-
class Point2D(TypedDict):
12-
x: int
13-
y: int
14-
label: str
11+
class Student(TypedDict):
12+
name: str
13+
age: int
14+
school: str
15+
16+
17+
class Undergraduate:
18+
...
1519

1620

1721
## End of your code ##
18-
a: Point3D = {"x": 1, "y": 2, "z": 3, "label": "good"}
19-
a: Point3D = {"x": 1, "z": 2, "label": "good"} # expect-type-error
20-
a: Point3D = {"x": 1, "y": 2, "label": "good"} # expect-type-error
21-
assert Point3D(x=1, y=2, z=3, label="first") == dict(x=1, y=2, z=3, label="first")
22+
a: Undergraduate = {"name": "Tom", "age": 20, "school": "MIT", "major": "Math"}
23+
a: Undergraduate = {"name": "Tom", "age": 20, "school": "MIT"} # expect-type-error
24+
assert Undergraduate(name="Tom", age=20, school="MIT", major="Math") == {
25+
"name": "Tom",
26+
"age": 20,
27+
"school": "MIT",
28+
"major": "Math",
29+
}
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
"""
22
TODO:
33
4-
Define a class `Point3D` using the predefined Point2D.
5-
`Point3D` needs to have an extra key-value pair: z - int
4+
Define a class `Undergraduate` using the predefined class `Student`.
5+
`Undergraduate` has an extra key `major` of type string
66
"""
77

88
from typing import TypedDict
99

1010

11-
class Point2D(TypedDict):
12-
x: int
13-
y: int
14-
label: str
11+
class Student(TypedDict):
12+
name: str
13+
age: int
14+
school: str
1515

1616

17-
class Point3D(Point2D):
18-
z: int
17+
class Undergraduate(Student):
18+
major: str
1919

2020

2121
## End of your code ##
22-
a: Point3D = {"x": 1, "y": 2, "z": 3, "label": "good"}
23-
a: Point3D = {"x": 1, "z": 2, "label": "good"} # expect-type-error
24-
a: Point3D = {"x": 1, "y": 2, "label": "good"} # expect-type-error
25-
assert Point3D(x=1, y=2, z=3, label="first") == dict(x=1, y=2, z=3, label="first")
22+
a: Undergraduate = {"name": "Tom", "age": 20, "school": "MIT", "major": "Math"}
23+
a: Undergraduate = {"name": "Tom", "age": 20, "school": "MIT"} # expect-type-error
24+
assert Undergraduate(name="Tom", age=20, school="MIT", major="Math") == {
25+
"name": "Tom",
26+
"age": 20,
27+
"school": "MIT",
28+
"major": "Math",
29+
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
"""
22
TODO:
33
4-
Define a class `Point2D` that represents a dictionary with three string keys:
5-
x, y, label
6-
7-
The value of each key must be the specified type:
8-
x - int, y - int, label - str
4+
Define a class `Student` that represents a dictionary with three keys:
5+
- name, a string
6+
- age, an integer
7+
- school, a string
98
"""
109

1110

1211
## End of your code ##
13-
a: Point2D = {"x": 1, "y": 2, "label": "good"}
14-
a: Point2D = {"x": 1, "z": 2, "label": "good"} # expect-type-error
15-
a: Point2D = {(1,): 1, "y": 2, "label": "good"} # expect-type-error
16-
a: Point2D = {"x": 1, "y": "2", "label": "good"} # expect-type-error
17-
a: Point2D = {"x": 1, "y": 2} # expect-type-error
18-
b: Point2D = {"z": 3, "label": "bad"} # expect-type-error
19-
assert Point2D(x=1, y=2, label="first") == dict(x=1, y=2, label="first")
12+
a: Student = {"name": "Tom", "age": 15, "school": "Hogwarts"}
13+
a: Student = {"name": 1, "age": 15, "school": "Hogwarts"} # expect-type-error
14+
a: Student = {(1,): "Tom", "age": 2, "school": "Hogwarts"} # expect-type-error
15+
a: Student = {"name": "Tom", "age": "2", "school": "Hogwarts"} # expect-type-error
16+
a: Student = {"name": "Tom", "age": 2} # expect-type-error
17+
assert Student(name="Tom", age=15, school="Hogwarts") == dict(
18+
name="Tom", age=15, school="Hogwarts"
19+
)
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
"""
22
TODO:
33
4-
Define a class `Point2D` that represents a dictionary with three string keys:
5-
x, y, label
6-
7-
The value of each key must be the specified type:
8-
x - int, y - int, label - str
4+
Define a class `Student` that represents a dictionary with three keys:
5+
- name, a string
6+
- age, an integer
7+
- school, a string
98
"""
109

1110
from typing import TypedDict
1211

1312

14-
class Point2D(TypedDict):
15-
x: int
16-
y: int
17-
label: str
13+
class Student(TypedDict):
14+
name: str
15+
age: int
16+
school: str
1817

1918

2019
# Alternatively you can write:
21-
# Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
20+
# Student = TypedDict('Student', {'name': str, 'age': int, 'school': str})
21+
2222

2323
## End of your code ##
24-
a: Point2D = {"x": 1, "y": 2, "label": "good"}
25-
a: Point2D = {"x": 1, "z": 2, "label": "good"} # expect-type-error
26-
a: Point2D = {(1,): 1, "y": 2, "label": "good"} # expect-type-error
27-
a: Point2D = {"x": 1, "y": "2", "label": "good"} # expect-type-error
28-
a: Point2D = {"x": 1, "y": 2} # expect-type-error
29-
b: Point2D = {"z": 3, "label": "bad"} # expect-type-error
30-
assert Point2D(x=1, y=2, label="first") == dict(x=1, y=2, label="first")
24+
a: Student = {"name": "Tom", "age": 15, "school": "Hogwarts"}
25+
a: Student = {"name": 1, "age": 15, "school": "Hogwarts"} # expect-type-error
26+
a: Student = {(1,): "Tom", "age": 2, "school": "Hogwarts"} # expect-type-error
27+
a: Student = {"name": "Tom", "age": "2", "school": "Hogwarts"} # expect-type-error
28+
a: Student = {"name": "Tom", "age": 2} # expect-type-error
29+
assert Student(name="Tom", age=15, school="Hogwarts") == dict(
30+
name="Tom", age=15, school="Hogwarts"
31+
)
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
"""
22
TODO:
33
4-
Define a class `Point2D` that represents a dictionary with three string keys:
5-
x, y, label
4+
Define a class `Student` that represents a dictionary with three keys:
5+
- name, a string
6+
- age, an integer
7+
- school, a string
68
7-
The value of each key must be the specified type:
8-
x - int, y - int, label - str
9-
10-
Note: label is optional
9+
Note: school can be optional
1110
"""
1211

1312

1413
## End of your code ##
15-
a: Point2D = {"x": 1, "y": 2}
16-
a: Point2D = {"x": 1, "y": 2, "label": "good"}
17-
a: Point2D = {"x": 1, "z": 2, "label": "good"} # expect-type-error
18-
a: Point2D = {(1,): 1, "y": 2, "label": "good"} # expect-type-error
19-
a: Point2D = {"x": 1, "y": "2", "label": "good"} # expect-type-error
20-
b: Point2D = {"z": 3, "label": "bad"} # expect-type-error
21-
assert Point2D(x=1, y=2) == dict(x=1, y=2)
22-
assert Point2D(x=1, y=2, label="first") == dict(x=1, y=2, label="first")
14+
a: Student = {"name": "Tom", "age": 15}
15+
a: Student = {"name": "Tom", "age": 15, "school": "Hogwarts"}
16+
a: Student = {"name": 1, "age": 15, "school": "Hogwarts"} # expect-type-error
17+
a: Student = {(1,): "Tom", "age": 2, "school": "Hogwarts"} # expect-type-error
18+
a: Student = {"name": "Tom", "age": "2", "school": "Hogwarts"} # expect-type-error
19+
a: Student = {"z": "Tom", "age": 2} # expect-type-error
20+
assert Student(name="Tom", age=15) == dict(name="Tom", age=15)
21+
assert Student(name="Tom", age=15, school="Hogwarts") == dict(
22+
name="Tom", age=15, school="Hogwarts"
23+
)
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
"""
22
TODO:
33
4-
Define a class `Point2D` that represents a dictionary with three string keys:
5-
x, y, label
4+
Define a class `Student` that represents a dictionary with three keys:
5+
- name, a string
6+
- age, an integer
7+
- school, a string
68
7-
The value of each key must be the specified type:
8-
x - int, y - int, label - str
9-
10-
Note: label is optional
9+
Note: school can be optional
1110
"""
1211

1312
from typing import TypedDict, NotRequired
1413

1514

16-
class Point2D(TypedDict):
17-
x: int
18-
y: int
19-
label: NotRequired[str]
15+
class Student(TypedDict):
16+
name: str
17+
age: int
18+
school: NotRequired[str]
2019

2120

2221
# Alternatively you can write:
23-
# Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': NotRequired[str]})
22+
# Student = TypedDict('Student', {'name': str, 'age': int, 'school': str})
23+
2424

2525
## End of your code ##
26-
a: Point2D = {"x": 1, "y": 2}
27-
a: Point2D = {"x": 1, "y": 2, "label": "good"}
28-
a: Point2D = {"x": 1, "z": 2, "label": "good"} # expect-type-error
29-
a: Point2D = {(1,): 1, "y": 2, "label": "good"} # expect-type-error
30-
a: Point2D = {"x": 1, "y": "2", "label": "good"} # expect-type-error
31-
b: Point2D = {"z": 3, "label": "bad"} # expect-type-error
32-
assert Point2D(x=1, y=2) == dict(x=1, y=2)
33-
assert Point2D(x=1, y=2, label="first") == dict(x=1, y=2, label="first")
26+
a: Student = {"name": "Tom", "age": 15}
27+
a: Student = {"name": "Tom", "age": 15, "school": "Hogwarts"}
28+
a: Student = {"name": 1, "age": 15, "school": "Hogwarts"} # expect-type-error
29+
a: Student = {(1,): "Tom", "age": 2, "school": "Hogwarts"} # expect-type-error
30+
a: Student = {"name": "Tom", "age": "2", "school": "Hogwarts"} # expect-type-error
31+
a: Student = {"z": "Tom", "age": 2} # expect-type-error
32+
assert Student(name="Tom", age=15) == dict(name="Tom", age=15)
33+
assert Student(name="Tom", age=15, school="Hogwarts") == dict(
34+
name="Tom", age=15, school="Hogwarts"
35+
)

0 commit comments

Comments
 (0)