Skip to content

Commit 0ff20c6

Browse files
committed
Refine some challenges, support mutitple solutions in testing
1 parent 535c1f2 commit 0ff20c6

File tree

11 files changed

+69
-135
lines changed

11 files changed

+69
-135
lines changed

challenges/advanced-descriptor-basic/question.py

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
TODO:
3+
4+
Create a descriptor and annotate the __get__ method.
5+
6+
HINT: use typing.overload
7+
"""
8+
9+
10+
class Descriptor:
11+
def __get__(self, instance, owner):
12+
"""you don't need to implement this"""
13+
...
14+
15+
16+
## End of your code ##
17+
class TestClass:
18+
a = Descriptor()
19+
20+
21+
def descriptor_self(x: Descriptor) -> None:
22+
...
23+
24+
25+
def string_value(x: str) -> None:
26+
...
27+
28+
29+
descriptor_self(TestClass.a)
30+
string_value(TestClass().a)
31+
descriptor_self(TestClass().a) # expect-type-error
32+
string_value(TestClass.a) # expect-type-error

challenges/advanced-descriptor-basic/solution.py renamed to challenges/advanced-descriptor/solution.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""
22
TODO:
33
4-
Define a descriptor, make test case works.
4+
Create a descriptor and annotate the __get__ method.
5+
6+
HINT: use typing.overload
57
"""
68

79
from typing import overload, Self, Any
@@ -17,10 +19,7 @@ def __get__(self, instance: Any, owner: type) -> str:
1719
...
1820

1921
def __get__(self, instance: Any, owner: type) -> Self | str:
20-
if instance is None:
21-
return self
22-
23-
return ""
22+
...
2423

2524

2625
## End of your code ##
@@ -38,6 +37,5 @@ def string_value(x: str) -> None:
3837

3938
descriptor_self(TestClass.a)
4039
string_value(TestClass().a)
41-
4240
descriptor_self(TestClass().a) # expect-type-error
4341
string_value(TestClass.a) # expect-type-error

challenges/advanced-my-method/question.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

challenges/advanced-my-method/solution.py

Lines changed: 0 additions & 50 deletions
This file was deleted.

challenges/extreme-self-casting/question.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""
22
TODO:
33
4-
Enhance the Fn[VnCallable].into_callable method to return a Callable with an additional Any parameter at the beginning (using Concatenate).
5-
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.
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.
68
"""
79

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

challenges/extreme-self-casting/solution.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""
22
TODO:
33
4-
Enhance the Fn[VnCallable].into_callable method to return a Callable with an additional Any parameter at the beginning (using Concatenate).
5-
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.
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.
68
"""
79

810
from typing import Callable, Concatenate, ParamSpec, TypeVar, Generic, Any, assert_type
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import Callable, Concatenate, Any, assert_type
2+
3+
4+
class Fn[R, **P]():
5+
def __init__(self, f: Callable[P, R]) -> None:
6+
self.f = f
7+
8+
def into_callable(self) -> Callable[Concatenate[Any, P], R]:
9+
...
10+
11+
12+
## End of your code ##
13+
@Fn
14+
def example(a: int, b: str, c: float, *, d: bool = False) -> None:
15+
return
16+
17+
18+
assert_type(example.f(1, "1", 1.0, d=False), None)
19+
20+
a: Any = 11111111
21+
b = example.into_callable()(a, 1, "1", 1.0, d=False)
22+
assert_type(b, None)
23+
24+
example.into_callable()(1, "1", 1.0, d=False) # expect-type-error

0 commit comments

Comments
 (0)