Skip to content

Commit e27184a

Browse files
committed
Add new challenges
1 parent e023a65 commit e27184a

File tree

8 files changed

+182
-0
lines changed

8 files changed

+182
-0
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
TODO:
3+
4+
`gen` is a generator that yields a integer, and can accept a string sent to it.
5+
It does not return anything.
6+
"""
7+
8+
9+
def gen():
10+
"""You don't need to implement it"""
11+
...
12+
13+
14+
## End of your code ##
15+
from typing import assert_type
16+
17+
generator = gen()
18+
assert_type(next(generator), int)
19+
generator.send("sss")
20+
generator.send(3) # expect-type-error
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
TODO:
3+
4+
`gen` is a generator that yields a integer, and can accept a string sent to it.
5+
It does not return anything.
6+
"""
7+
8+
from collections.abc import Generator
9+
10+
11+
def gen() -> Generator[int, str, None]:
12+
"""You don't need to implement it"""
13+
...
14+
15+
16+
## End of your code ##
17+
from typing import assert_type
18+
19+
generator = gen()
20+
assert_type(next(generator), int)
21+
generator.send("sss")
22+
generator.send(3) # expect-type-error

challenges/advanced-type/question.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
TODO:
3+
4+
`make_object` takes a class returns an instance of it.
5+
"""
6+
7+
8+
def make_object(cls):
9+
return cls()
10+
11+
12+
## End of your code ##
13+
class MyClass:
14+
pass
15+
16+
17+
def f():
18+
pass
19+
20+
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

challenges/advanced-type/solution.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
TODO:
3+
4+
`make_object` takes a class returns an instance of it.
5+
"""
6+
7+
from typing import Any
8+
9+
10+
def make_object(cls: type[Any]):
11+
return cls()
12+
13+
14+
## End of your code ##
15+
class MyClass:
16+
pass
17+
18+
19+
def f():
20+
pass
21+
22+
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

challenges/basic-kwargs/question.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
TODO:
3+
4+
`foo` takes keyword arguments of type integer or string.
5+
"""
6+
7+
8+
def foo(**kwargs):
9+
...
10+
11+
12+
## End of your code ##
13+
foo(a=1, b="2")
14+
foo(a=[1]) # expect-type-error

challenges/basic-kwargs/solution.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
TODO:
3+
4+
`foo` takes keyword arguments of type integer or string.
5+
"""
6+
7+
8+
def foo(**kwargs: int | str):
9+
...
10+
11+
12+
## End of your code ##
13+
foo(a=1, b="2")
14+
foo(a=[1]) # expect-type-error
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
TODO:
3+
4+
`run_async` takes an awaitable integer.
5+
"""
6+
7+
8+
def run_async():
9+
...
10+
11+
12+
## End of your code ##
13+
from asyncio import Queue
14+
15+
queue: Queue[int] = Queue()
16+
queue2: Queue[str] = Queue()
17+
18+
19+
async def async_function() -> int:
20+
return await queue.get()
21+
22+
23+
async def async_function2() -> str:
24+
return await queue2.get()
25+
26+
27+
run_async(async_function())
28+
run_async(1) # expect-type-error
29+
run_async(async_function2()) # expect-type-error
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
TODO:
3+
4+
`run_async` takes an awaitable integer.
5+
"""
6+
7+
from typing import Awaitable
8+
9+
10+
def run_async(func: Awaitable[int]):
11+
...
12+
13+
14+
## End of your code ##
15+
from asyncio import Queue
16+
17+
queue: Queue[int] = Queue()
18+
queue2: Queue[str] = Queue()
19+
20+
21+
async def async_function() -> int:
22+
return await queue.get()
23+
24+
25+
async def async_function2() -> str:
26+
return await queue2.get()
27+
28+
29+
run_async(async_function())
30+
run_async(1) # expect-type-error
31+
run_async(async_function2()) # expect-type-error

0 commit comments

Comments
 (0)