Skip to content

Commit 153acc6

Browse files
committed
New challenges
1 parent 33046b3 commit 153acc6

File tree

6 files changed

+157
-0
lines changed

6 files changed

+157
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
TODO:
3+
4+
Define a decorator that wraps a function and returns a function with the same signature.
5+
The decorator takes an argument `message` of type string
6+
"""
7+
8+
9+
def decorator(message):
10+
...
11+
12+
13+
## End of your code ##
14+
@decorator(message="x")
15+
def foo(a: int, *, b: str) -> None:
16+
...
17+
18+
19+
@decorator # expect-type-error
20+
def bar(a: int, *, b: str) -> None:
21+
...
22+
23+
24+
foo(1, b="2")
25+
foo(1, "2") # expect-type-error
26+
foo(a=1, e="2") # expect-type-error
27+
decorator(1) # expect-type-error
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
TODO:
3+
4+
Define a decorator that wraps a function and returns a function with the same signature.
5+
The decorator takes an argument `message` of type string
6+
"""
7+
from typing import TypeVar
8+
from collections.abc import Callable
9+
10+
# Python < 3.12
11+
#
12+
# T = TypeVar("T", bound=Callable)
13+
#
14+
# def decorator(func: T) -> T:
15+
# return func
16+
17+
18+
# Python >= 3.12
19+
def decorator[T: Callable](message: str) -> Callable[[T], T]:
20+
...
21+
22+
23+
## End of your code ##
24+
@decorator(message="x")
25+
def foo(a: int, *, b: str) -> None:
26+
...
27+
28+
29+
@decorator # expect-type-error
30+
def bar(a: int, *, b: str) -> None:
31+
...
32+
33+
34+
foo(1, b="2")
35+
foo(1, "2") # expect-type-error
36+
foo(a=1, e="2") # expect-type-error
37+
decorator(1) # expect-type-error
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
TODO:
3+
4+
Annotate class `MyContainer`, which should support item access, i.e.
5+
using `[x]` to get, set, and delete an item.
6+
7+
HINT: Use https://github.com/python/typeshed
8+
"""
9+
10+
11+
class MyContainer:
12+
...
13+
14+
15+
## End of your code ##
16+
from typing import assert_type
17+
from collections.abc import Mapping
18+
19+
c = MyContainer()
20+
c[1] = 1
21+
c["2"] = 2
22+
print(c[1])
23+
print(c["2"])
24+
del c[1]
25+
del c["2"]
26+
assert_type(c, dict) # expect-type-error
27+
assert_type(c, Mapping) # expect-type-error
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
TODO:
3+
4+
Annotate class `MyContainer`, which should support item access, i.e.
5+
using `[x]` to get, set, and delete an item.
6+
7+
HINT: Use https://github.com/python/typeshed
8+
"""
9+
10+
from _typeshed import SupportsItemAccess
11+
12+
13+
class MyContainer(SupportsItemAccess):
14+
...
15+
16+
17+
## End of your code ##
18+
from typing import assert_type
19+
from collections.abc import Mapping
20+
21+
c = MyContainer()
22+
c[1] = 1
23+
c["2"] = 2
24+
print(c[1])
25+
print(c["2"])
26+
del c[1]
27+
del c["2"]
28+
assert_type(c, dict) # expect-type-error
29+
assert_type(c, Mapping) # expect-type-error
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
TODO:
3+
4+
Create a new type called Vector, which is a list of float.
5+
"""
6+
7+
8+
## End of your code ##
9+
def foo(v: Vector):
10+
...
11+
12+
13+
foo([1.1, 2])
14+
foo(1) # expect-type-error
15+
foo(["1"]) # expect-type-error
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
TODO:
3+
4+
Create a new type called Vector, which is a list of float.
5+
"""
6+
7+
from typing import TypeAlias
8+
9+
Vector: TypeAlias = list[float]
10+
11+
# Python >= 3.12
12+
# type Vector = list[float]
13+
14+
15+
## End of your code ##
16+
def foo(v: Vector):
17+
...
18+
19+
20+
foo([1.1, 2])
21+
foo(1) # expect-type-error
22+
foo(["1"]) # expect-type-error

0 commit comments

Comments
 (0)