Skip to content

Commit

Permalink
Refine challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
laike9m committed Dec 11, 2023
1 parent 0ff20c6 commit f601242
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
32 changes: 14 additions & 18 deletions challenges/extreme-self-casting/question.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
"""
TODO:
Enhance the Fn[VnCallable].into_callable method to return a Callable with an additional
Any parameter at the beginning (using Concatenate).
This should preserve the remaining parts of the function signature from VnCallable
(i.e., parameters and their types, excluding the suffix), as well as the return type.
"""

from typing import Callable, TypeVar, Generic, Any, assert_type

VnCallable = TypeVar("VnCallable", bound=Callable)
Fn is a class decorator which takes a callable (`f`).
Fn has a `transform_callable` method, which transform `f` into a different callable,
with an additional Any parameter at the beginning, while preserving the remaining parts
of the function signature.
Note: you're only requried to add type annotations without implementing transform_callable.
"""

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

def __init__(self, f: VnCallable) -> None:
class Fn:
def __init__(self, f):
self.f = f

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


## End of your code ##
from typing import assert_type


@Fn
def example(a: int, b: str, c: float, *, d: bool = False) -> None:
return
Expand All @@ -34,7 +30,7 @@ def example(a: int, b: str, c: float, *, d: bool = False) -> None:
assert_type(example.f(1, "1", 1.0, d=False), None)

a: Any = 11111111
b = example.into_callable()(a, 1, "1", 1.0, d=False)
b = example.transform_callable()(a, 1, "1", 1.0, d=False)
assert_type(b, None)

example.into_callable()(1, "1", 1.0, d=False) # expect-type-error
example.transform_callable()(1, "1", 1.0, d=False) # expect-type-error
24 changes: 16 additions & 8 deletions challenges/extreme-self-casting/solution.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""
TODO:
Enhance the Fn[VnCallable].into_callable method to return a Callable with an additional
Any parameter at the beginning (using Concatenate).
This should preserve the remaining parts of the function signature from VnCallable
(i.e., parameters and their types, excluding the suffix), as well as the return type.
Fn is a class decorator which takes a callable (`f`).
Fn has a `transform_callable` method, which transform `f` into a different callable,
with an additional Any parameter at the beginning, while preserving the remaining parts
of the function signature.
Note: you're only requried to add type annotations without implementing transform_callable.
"""

from typing import Callable, Concatenate, ParamSpec, TypeVar, Generic, Any, assert_type

from typing import Callable, Concatenate, ParamSpec, TypeVar, Generic, Any

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

def into_callable(self: "Fn[Callable[P, R]]") -> Callable[Concatenate[Any, P], R]:
def transform_callable(
self: "Fn[Callable[P, R]]",
) -> Callable[Concatenate[Any, P], R]:
...


## End of your code ##
from typing import assert_type


@Fn
def example(a: int, b: str, c: float, *, d: bool = False) -> None:
return
Expand All @@ -31,7 +39,7 @@ def example(a: int, b: str, c: float, *, d: bool = False) -> None:
assert_type(example.f(1, "1", 1.0, d=False), None)

a: Any = 11111111
b = example.into_callable()(a, 1, "1", 1.0, d=False)
b = example.transform_callable()(a, 1, "1", 1.0, d=False)
assert_type(b, None)

example.into_callable()(1, "1", 1.0, d=False) # expect-type-error
example.transform_callable()(1, "1", 1.0, d=False) # expect-type-error
23 changes: 19 additions & 4 deletions challenges/extreme-self-casting/solution2.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
from typing import Callable, Concatenate, Any, assert_type
"""
TODO:
Fn is a class decorator which takes a callable (`f`).
Fn has a `transform_callable` method, which transform `f` into a different callable,
with an additional Any parameter at the beginning, while preserving the remaining parts
of the function signature.
Note: you're only requried to add type annotations without implementing transform_callable.
"""


from typing import Callable, Concatenate, Any


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

def into_callable(self) -> Callable[Concatenate[Any, P], R]:
def transform_callable(self) -> Callable[Concatenate[Any, P], R]:
...


## End of your code ##
from typing import assert_type


@Fn
def example(a: int, b: str, c: float, *, d: bool = False) -> None:
return
Expand All @@ -18,7 +33,7 @@ def example(a: int, b: str, c: float, *, d: bool = False) -> None:
assert_type(example.f(1, "1", 1.0, d=False), None)

a: Any = 11111111
b = example.into_callable()(a, 1, "1", 1.0, d=False)
b = example.transform_callable()(a, 1, "1", 1.0, d=False)
assert_type(b, None)

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

0 comments on commit f601242

Please sign in to comment.