From f60124240d10161ed6a933fdfc1ce1f473ce509d Mon Sep 17 00:00:00 2001 From: laike9m Date: Sun, 10 Dec 2023 16:57:04 -0800 Subject: [PATCH] Refine challenge --- challenges/extreme-self-casting/question.py | 32 +++++++++----------- challenges/extreme-self-casting/solution.py | 24 ++++++++++----- challenges/extreme-self-casting/solution2.py | 23 +++++++++++--- 3 files changed, 49 insertions(+), 30 deletions(-) diff --git a/challenges/extreme-self-casting/question.py b/challenges/extreme-self-casting/question.py index 7c42208..b1f9373 100644 --- a/challenges/extreme-self-casting/question.py +++ b/challenges/extreme-self-casting/question.py @@ -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 @@ -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 diff --git a/challenges/extreme-self-casting/solution.py b/challenges/extreme-self-casting/solution.py index 1c6f688..5fa7d26 100644 --- a/challenges/extreme-self-casting/solution.py +++ b/challenges/extreme-self-casting/solution.py @@ -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) @@ -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 @@ -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 diff --git a/challenges/extreme-self-casting/solution2.py b/challenges/extreme-self-casting/solution2.py index 807a2c8..8bdeeca 100644 --- a/challenges/extreme-self-casting/solution2.py +++ b/challenges/extreme-self-casting/solution2.py @@ -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 @@ -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