Skip to content

Commit f601242

Browse files
committed
Refine challenge
1 parent 0ff20c6 commit f601242

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed
Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
"""
22
TODO:
33
4-
Enhance the Fn[VnCallable].into_callable method to return a Callable with an additional
5-
Any parameter at the beginning (using Concatenate).
6-
This should preserve the remaining parts of the function signature from VnCallable
7-
(i.e., parameters and their types, excluding the suffix), as well as the return type.
8-
"""
9-
10-
from typing import Callable, TypeVar, Generic, Any, assert_type
11-
12-
VnCallable = TypeVar("VnCallable", bound=Callable)
4+
Fn is a class decorator which takes a callable (`f`).
5+
Fn has a `transform_callable` method, which transform `f` into a different callable,
6+
with an additional Any parameter at the beginning, while preserving the remaining parts
7+
of the function signature.
138
9+
Note: you're only requried to add type annotations without implementing transform_callable.
10+
"""
1411

15-
class Fn(Generic[VnCallable]):
16-
# you MUST NOT modify the Generic defination.
1712

18-
def __init__(self, f: VnCallable) -> None:
13+
class Fn:
14+
def __init__(self, f):
1915
self.f = f
2016

21-
def into_callable(self):
22-
# TODO: annotate self parameter, not required to touch the function body.
23-
# NOTE: the test case requires a Any prefix param before VnCallable's parameters.
24-
# information is enough for type checker to infer these types.
17+
def transform_callable(self):
2518
...
2619

2720

2821
## End of your code ##
22+
from typing import assert_type
23+
24+
2925
@Fn
3026
def example(a: int, b: str, c: float, *, d: bool = False) -> None:
3127
return
@@ -34,7 +30,7 @@ def example(a: int, b: str, c: float, *, d: bool = False) -> None:
3430
assert_type(example.f(1, "1", 1.0, d=False), None)
3531

3632
a: Any = 11111111
37-
b = example.into_callable()(a, 1, "1", 1.0, d=False)
33+
b = example.transform_callable()(a, 1, "1", 1.0, d=False)
3834
assert_type(b, None)
3935

40-
example.into_callable()(1, "1", 1.0, d=False) # expect-type-error
36+
example.transform_callable()(1, "1", 1.0, d=False) # expect-type-error
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
"""
22
TODO:
33
4-
Enhance the Fn[VnCallable].into_callable method to return a Callable with an additional
5-
Any parameter at the beginning (using Concatenate).
6-
This should preserve the remaining parts of the function signature from VnCallable
7-
(i.e., parameters and their types, excluding the suffix), as well as the return type.
4+
Fn is a class decorator which takes a callable (`f`).
5+
Fn has a `transform_callable` method, which transform `f` into a different callable,
6+
with an additional Any parameter at the beginning, while preserving the remaining parts
7+
of the function signature.
8+
9+
Note: you're only requried to add type annotations without implementing transform_callable.
810
"""
911

10-
from typing import Callable, Concatenate, ParamSpec, TypeVar, Generic, Any, assert_type
12+
13+
from typing import Callable, Concatenate, ParamSpec, TypeVar, Generic, Any
1114

1215
P = ParamSpec("P")
1316
R = TypeVar("R", covariant=True)
@@ -18,11 +21,16 @@ class Fn(Generic[VnCallable]):
1821
def __init__(self, f: VnCallable) -> None:
1922
self.f = f
2023

21-
def into_callable(self: "Fn[Callable[P, R]]") -> Callable[Concatenate[Any, P], R]:
24+
def transform_callable(
25+
self: "Fn[Callable[P, R]]",
26+
) -> Callable[Concatenate[Any, P], R]:
2227
...
2328

2429

2530
## End of your code ##
31+
from typing import assert_type
32+
33+
2634
@Fn
2735
def example(a: int, b: str, c: float, *, d: bool = False) -> None:
2836
return
@@ -31,7 +39,7 @@ def example(a: int, b: str, c: float, *, d: bool = False) -> None:
3139
assert_type(example.f(1, "1", 1.0, d=False), None)
3240

3341
a: Any = 11111111
34-
b = example.into_callable()(a, 1, "1", 1.0, d=False)
42+
b = example.transform_callable()(a, 1, "1", 1.0, d=False)
3543
assert_type(b, None)
3644

37-
example.into_callable()(1, "1", 1.0, d=False) # expect-type-error
45+
example.transform_callable()(1, "1", 1.0, d=False) # expect-type-error
Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1-
from typing import Callable, Concatenate, Any, assert_type
1+
"""
2+
TODO:
3+
4+
Fn is a class decorator which takes a callable (`f`).
5+
Fn has a `transform_callable` method, which transform `f` into a different callable,
6+
with an additional Any parameter at the beginning, while preserving the remaining parts
7+
of the function signature.
8+
9+
Note: you're only requried to add type annotations without implementing transform_callable.
10+
"""
11+
12+
13+
from typing import Callable, Concatenate, Any
214

315

416
class Fn[R, **P]():
517
def __init__(self, f: Callable[P, R]) -> None:
618
self.f = f
719

8-
def into_callable(self) -> Callable[Concatenate[Any, P], R]:
20+
def transform_callable(self) -> Callable[Concatenate[Any, P], R]:
921
...
1022

1123

1224
## End of your code ##
25+
from typing import assert_type
26+
27+
1328
@Fn
1429
def example(a: int, b: str, c: float, *, d: bool = False) -> None:
1530
return
@@ -18,7 +33,7 @@ def example(a: int, b: str, c: float, *, d: bool = False) -> None:
1833
assert_type(example.f(1, "1", 1.0, d=False), None)
1934

2035
a: Any = 11111111
21-
b = example.into_callable()(a, 1, "1", 1.0, d=False)
36+
b = example.transform_callable()(a, 1, "1", 1.0, d=False)
2237
assert_type(b, None)
2338

24-
example.into_callable()(1, "1", 1.0, d=False) # expect-type-error
39+
example.transform_callable()(1, "1", 1.0, d=False) # expect-type-error

0 commit comments

Comments
 (0)