Skip to content

Commit 6f23f6c

Browse files
committed
new challenge
1 parent a58098c commit 6f23f6c

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
TODO:
3+
4+
foo is a function that returns an interger when second argument is True, otherwise it returns a string.
5+
"""
6+
7+
8+
def foo(value, flag=False):
9+
...
10+
11+
12+
def should_pass():
13+
foo("42").upper()
14+
foo("42", True).bit_length()
15+
16+
17+
def should_fail():
18+
foo("42").bit_length()
19+
foo("42", True).upper()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
TODO:
3+
4+
foo is a function that returns an interger when called specified Foo[int], returns a string when called specified Foo[str], otherwise it returns a Foo[T].
5+
"""
6+
from typing import TypeVar, Generic
7+
8+
class Foo(Generic[T]): ...
9+
a: T
10+
11+
def foo(value: Foo):
12+
...
13+
14+
15+
def should_pass():
16+
foo(Foo[int]()).bit_length()
17+
foo(Foo[str]()).upper()
18+
foo(Foo[list]()).a.append(1)
19+
20+
21+
def should_fail():
22+
foo(Foo[int]()).upper()
23+
foo(Foo[str]()).bit_length()
24+
foo(Foo[list]()).bit_length()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
TODO:
3+
4+
Define a decorator `constructor_parameter` that input target class
5+
and return a wrapper function with the same signature as the constructor of the target class,
6+
and function which decorated by `constructor_parameter` can be called with the instance of the target class.
7+
"""
8+
9+
class Foo:
10+
def __init__(self, a: int, b: str) -> None:
11+
...
12+
13+
def constructor_parameter():
14+
...
15+
16+
17+
def should_pass():
18+
@constructor_parameter(Foo)
19+
def func(foo: Foo) -> None:
20+
...
21+
22+
func(1, "2")
23+
24+
25+
def should_fail():
26+
@constructor_parameter(Foo)
27+
def func(foo: Foo) -> None:
28+
...
29+
30+
func("1", "2")
31+
func([1, 2, 3])

0 commit comments

Comments
 (0)