Skip to content

Commit 51cc6ad

Browse files
Feat: Enhance advanced-type challenge with typevar and enforcing return type.
1 parent dafb4d3 commit 51cc6ad

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

challenges/advanced-type/question.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66

77

88
def make_object(cls):
9-
return cls()
9+
...
1010

1111

1212
## End of your code ##
13+
from typing import assert_type
14+
15+
1316
class MyClass:
1417
pass
1518

@@ -18,8 +21,9 @@ def f():
1821
pass
1922

2023

21-
c = make_object(MyClass)
22-
c = make_object(int)
23-
c = make_object(f) # expect-type-error
24-
c = make_object("sss") # expect-type-error
25-
c = make_object(["sss"]) # expect-type-error
24+
assert_type(make_object(MyClass), MyClass)
25+
assert_type(make_object(int), int)
26+
27+
make_object(f) # expect-type-error
28+
make_object("sss") # expect-type-error
29+
make_object(["sss"]) # expect-type-error

challenges/advanced-type/solution.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
`make_object` takes a class returns an instance of it.
55
"""
66

7-
from typing import Any
87

9-
10-
def make_object(cls: type[Any]):
11-
return cls()
8+
def make_object[T](cls: type[T]) -> T:
9+
...
1210

1311

1412
## End of your code ##
13+
from typing import assert_type
14+
15+
1516
class MyClass:
1617
pass
1718

@@ -20,8 +21,9 @@ def f():
2021
pass
2122

2223

23-
c = make_object(MyClass)
24-
c = make_object(int)
25-
c = make_object(f) # expect-type-error
26-
c = make_object("sss") # expect-type-error
27-
c = make_object(["sss"]) # expect-type-error
24+
assert_type(make_object(MyClass), MyClass)
25+
assert_type(make_object(int), int)
26+
27+
make_object(f) # expect-type-error
28+
make_object("sss") # expect-type-error
29+
make_object(["sss"]) # expect-type-error

0 commit comments

Comments
 (0)