Skip to content

Commit 9426302

Browse files
committed
Migrate old challenges and solutions to the new layout laike9m#10
1 parent 68a2c89 commit 9426302

File tree

7 files changed

+93
-71
lines changed

7 files changed

+93
-71
lines changed

challenges/advanced-overload-generic/question.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@
55
"""
66
from typing import TypeVar, Generic
77

8-
T = TypeVar('T')
8+
T = TypeVar("T")
9+
910

1011
class Foo(Generic[T]):
1112
a: T
1213

14+
1315
def foo(value: Foo):
1416
...
1517

1618

17-
def should_pass():
18-
foo(Foo[int]()).bit_length()
19-
foo(Foo[str]()).upper()
20-
foo(Foo[list]()).a.append(1)
19+
## End of your code ##
20+
21+
22+
foo(Foo[int]()).bit_length()
23+
foo(Foo[str]()).upper()
24+
foo(Foo[list]()).a.append(1)
2125

2226

23-
def should_fail():
24-
foo(Foo[int]()).upper()
25-
foo(Foo[str]()).bit_length()
26-
foo(Foo[list]()).bit_length()
27+
foo(Foo[int]()).upper() # expect-type-error
28+
foo(Foo[str]()).bit_length() # expect-type-error
29+
foo(Foo[list]()).bit_length() # expect-type-error

challenges/advanced-overload-generic/solution.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,38 @@
55
"""
66
from typing import Any, TypeVar, Generic, overload
77

8-
T = TypeVar('T')
8+
T = TypeVar("T")
9+
910

1011
class Foo(Generic[T]):
1112
a: T
1213

14+
1315
@overload
1416
def foo(value: Foo[int]) -> int:
1517
...
1618

19+
1720
@overload
1821
def foo(value: Foo[str]) -> str:
1922
...
2023

24+
2125
@overload
2226
def foo(value: Foo[T]) -> Foo[T]:
2327
...
2428

29+
2530
def foo(value: Foo) -> Any:
2631
...
2732

2833

29-
def should_pass():
30-
foo(Foo[int]()).bit_length()
31-
foo(Foo[str]()).upper()
32-
foo(Foo[list]()).a.append(1)
34+
## End of your code ##
3335

36+
foo(Foo[int]()).bit_length()
37+
foo(Foo[str]()).upper()
38+
foo(Foo[list]()).a.append(1)
3439

35-
def should_fail():
36-
foo(Foo[int]()).upper()
37-
foo(Foo[str]()).bit_length()
38-
foo(Foo[list]()).bit_length()
40+
foo(Foo[int]()).upper() # expect-type-error
41+
foo(Foo[str]()).bit_length() # expect-type-error
42+
foo(Foo[list]()).bit_length() # expect-type-error

challenges/advanced-overload-literal/question.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ def foo(value, flag):
99
...
1010

1111

12-
def should_pass():
13-
foo("42", 1).bit_length()
14-
foo(42, 2).upper()
15-
foo(True, 3).append(1)
16-
foo({}, "4").keys()
12+
## End of your code ##
1713

14+
foo("42", 1).bit_length()
15+
foo(42, 2).upper()
16+
foo(True, 3).append(1)
17+
foo({}, "4").keys()
1818

19-
def should_fail():
20-
foo("42", 1).upper()
21-
foo(42, 2).append(1)
22-
foo(True, 3).bit_length()
23-
foo({}, "4").upper()
19+
foo("42", 1).upper() # expect-type-error
20+
foo(42, 2).append(1) # expect-type-error
21+
foo(True, 3).bit_length() # expect-type-error
22+
foo({}, "4").upper() # expect-type-error

challenges/advanced-overload-literal/solution.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@
55
"""
66
from typing import Any, Literal, overload, TypeVar
77

8-
T = TypeVar('T')
8+
T = TypeVar("T")
9+
910

1011
@overload
1112
def foo(value: Any, flag: Literal[1]) -> int:
1213
...
1314

15+
1416
@overload
1517
def foo(value: Any, flag: Literal[2]) -> str:
1618
...
1719

20+
1821
@overload
1922
def foo(value: Any, flag: Literal[3]) -> list:
2023
...
2124

25+
2226
@overload
2327
def foo(value: T, flag: Any) -> T:
2428
...
@@ -28,15 +32,15 @@ def foo(value, flag) -> Any:
2832
...
2933

3034

31-
def should_pass():
32-
foo("42", 1).bit_length()
33-
foo(42, 2).upper()
34-
foo(True, 3).append(1)
35-
foo({}, "4").keys()
35+
## End of your code ##
36+
37+
foo("42", 1).bit_length()
38+
foo(42, 2).upper()
39+
foo(True, 3).append(1)
40+
foo({}, "4").keys()
3641

3742

38-
def should_fail():
39-
foo("42", 1).upper()
40-
foo(42, 2).append(1)
41-
foo(True, 3).bit_length()
42-
foo({}, "4").upper()
43+
foo("42", 1).upper() # expect-type-error
44+
foo(42, 2).append(1) # expect-type-error
45+
foo(True, 3).bit_length() # expect-type-error
46+
foo({}, "4").upper() # expect-type-error

challenges/extreme-constructor-parameter/question.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,40 @@
22
TODO:
33
44
Define a decorator `constructor_parameter` that accepts the type of Foo.
5-
and return a wrapper function with the same signature as the constructor of Foo,
5+
and return a wrapper function with the same signature as the constructor of Foo,
66
and function decorated by `constructor_parameter` can be called with an instance of Foo.
77
"""
88

9+
910
class Foo:
1011
a: int
1112
b: str
1213

1314
def __init__(self, a: int, b: str) -> None:
1415
...
1516

17+
1618
def constructor_parameter():
1719
...
1820

1921

20-
def should_pass():
21-
@constructor_parameter(Foo)
22-
def func(foo: Foo) -> list[Foo]:
23-
...
22+
## End of your code ##
2423

25-
res = func(1, "2")
26-
res[0].a.bit_length()
27-
res[0].b.upper()
2824

25+
@constructor_parameter(Foo)
26+
def func_pass(foo: Foo) -> list[Foo]:
27+
...
28+
29+
30+
res = func_pass(1, "2")
31+
res[0].a.bit_length()
32+
res[0].b.upper()
33+
34+
35+
@constructor_parameter(Foo)
36+
def func_fail(foo: Foo) -> list:
37+
...
2938

30-
def should_fail():
31-
@constructor_parameter(Foo)
32-
def func(foo: Foo) -> list:
33-
...
3439

35-
func("1", "2")
36-
func([1, 2, 3])
40+
func_fail("1", "2") # expect-type-error
41+
func_fail([1, 2, 3]) # expect-type-error

challenges/extreme-constructor-parameter/solution.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,47 @@
22
TODO:
33
44
Define a decorator `constructor_parameter` that accepts the type of Foo.
5-
and return a wrapper function with the same signature as the constructor of Foo,
5+
and return a wrapper function with the same signature as the constructor of Foo,
66
and function decorated by `constructor_parameter` can be called with an instance of Foo.
77
"""
88
from typing import TypeVar, Callable
99
from typing_extensions import ParamSpec, Concatenate
1010

11+
1112
class Foo:
1213
a: int
1314
b: str
1415

1516
def __init__(self, a: int, b: str) -> None:
1617
...
1718

18-
T = TypeVar('T')
19-
P = ParamSpec('P')
20-
R = TypeVar('R')
19+
20+
T = TypeVar("T")
21+
P = ParamSpec("P")
22+
R = TypeVar("R")
2123

2224

23-
def constructor_parameter(cls: Callable[P, T]) -> Callable[[Callable[[T], R]], Callable[P, R]]:
25+
def constructor_parameter(
26+
cls: Callable[P, T]
27+
) -> Callable[[Callable[[T], R]], Callable[P, R]]:
2428
...
2529

2630

27-
def should_pass():
28-
@constructor_parameter(Foo)
29-
def func(foo: Foo) -> list[Foo]:
30-
...
31+
## End of your code ##
32+
@constructor_parameter(Foo)
33+
def func_pass(foo: Foo) -> list[Foo]:
34+
...
3135

32-
res = func(1, "2")
33-
res[0].a.bit_length()
34-
res[0].b.upper()
3536

37+
res = func_pass(1, "2")
38+
res[0].a.bit_length()
39+
res[0].b.upper()
40+
41+
42+
@constructor_parameter(Foo)
43+
def func_fail(foo: Foo) -> list:
44+
...
3645

37-
def should_fail():
38-
@constructor_parameter(Foo)
39-
def func(foo: Foo) -> list:
40-
...
4146

42-
func("1", "2")
43-
func([1, 2, 3])
47+
func_fail("1", "2") # expect-type-error
48+
func_fail([1, 2, 3]) # expect-type-error

views/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,13 @@ def _load_challenges() -> dict[ChallengeName, Challenge]:
9090
@classmethod
9191
def _type_check_with_mypy(cls, code: str) -> TypeCheckResult:
9292
buffer = io.StringIO(code)
93+
9394
# This produces a stream of TokenInfos, example:
9495
# TokenInfo(type=4 (NEWLINE), string='\n', start=(4, 3), end=(4, 4), line='"""\n'),
9596
# TokenInfo(type=62 (NL), string='\n', start=(5, 0), end=(5, 1), line='\n')
9697
# See https://docs.python.org/3/library/tokenize.html#tokenize.tokenize for more details
9798
tokens = list(tokenize.generate_tokens(buffer.readline))
99+
98100
# Find all lines that are followed by a comment # expect-type-error
99101
expect_error_line_numbers = [
100102
token.start[0]

0 commit comments

Comments
 (0)