Skip to content

Commit

Permalink
Refine some challenges, support mutitple solutions in testing
Browse files Browse the repository at this point in the history
  • Loading branch information
laike9m committed Dec 11, 2023
1 parent 535c1f2 commit 0ff20c6
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 135 deletions.
38 changes: 0 additions & 38 deletions challenges/advanced-descriptor-basic/question.py

This file was deleted.

32 changes: 32 additions & 0 deletions challenges/advanced-descriptor/question.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
TODO:
Create a descriptor and annotate the __get__ method.
HINT: use typing.overload
"""


class Descriptor:
def __get__(self, instance, owner):
"""you don't need to implement this"""
...


## End of your code ##
class TestClass:
a = Descriptor()


def descriptor_self(x: Descriptor) -> None:
...


def string_value(x: str) -> None:
...


descriptor_self(TestClass.a)
string_value(TestClass().a)
descriptor_self(TestClass().a) # expect-type-error
string_value(TestClass.a) # expect-type-error
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""
TODO:
Define a descriptor, make test case works.
Create a descriptor and annotate the __get__ method.
HINT: use typing.overload
"""

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

def __get__(self, instance: Any, owner: type) -> Self | str:
if instance is None:
return self

return ""
...


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

descriptor_self(TestClass.a)
string_value(TestClass().a)

descriptor_self(TestClass().a) # expect-type-error
string_value(TestClass.a) # expect-type-error
36 changes: 0 additions & 36 deletions challenges/advanced-my-method/question.py

This file was deleted.

50 changes: 0 additions & 50 deletions challenges/advanced-my-method/solution.py

This file was deleted.

File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions challenges/extreme-self-casting/question.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""
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.
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
Expand Down
6 changes: 4 additions & 2 deletions challenges/extreme-self-casting/solution.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""
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.
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, Concatenate, ParamSpec, TypeVar, Generic, Any, assert_type
Expand Down
24 changes: 24 additions & 0 deletions challenges/extreme-self-casting/solution2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import Callable, Concatenate, Any, assert_type


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

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


## End of your code ##
@Fn
def example(a: int, b: str, c: float, *, d: bool = False) -> None:
return


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)
assert_type(b, None)

example.into_callable()(1, "1", 1.0, d=False) # expect-type-error
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

CHALLENGES_DIR = Path(__file__).parent.parent / "challenges"
ALL_QUESTIONS = list(CHALLENGES_DIR.glob("**/question.py"))
ALL_SOLUTIONS = list(CHALLENGES_DIR.glob("**/solution.py"))
ALL_SOLUTIONS = list(CHALLENGES_DIR.glob("**/solution*.py"))


@pytest.fixture
Expand Down

0 comments on commit 0ff20c6

Please sign in to comment.