Skip to content

Commit 5c10a67

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

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

challenges/advanced-type/question.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
"""
66

77

8-
def make_object(cls):
9-
return cls()
8+
def make_object(cls): ...
109

1110

1211
## End of your code ##
12+
from typing import assert_type
13+
14+
1315
class MyClass:
1416
pass
1517

@@ -18,8 +20,9 @@ def f():
1820
pass
1921

2022

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
23+
assert_type(make_object(MyClass), MyClass)
24+
assert_type(make_object(int), int)
25+
26+
make_object(f) # expect-type-error
27+
make_object("sss") # expect-type-error
28+
make_object(["sss"]) # expect-type-error

challenges/advanced-type/solution.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
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: ...
129

1310

1411
## End of your code ##
12+
from typing import assert_type
13+
14+
1515
class MyClass:
1616
pass
1717

@@ -20,8 +20,9 @@ def f():
2020
pass
2121

2222

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
23+
assert_type(make_object(MyClass), MyClass)
24+
assert_type(make_object(int), int)
25+
26+
make_object(f) # expect-type-error
27+
make_object("sss") # expect-type-error
28+
make_object(["sss"]) # expect-type-error

0 commit comments

Comments
 (0)