Skip to content

Commit 147ed14

Browse files
authored
feat: improve the layout and judgement of challenges, to make it more strict (laike9m#10)
1 parent ea8f540 commit 147ed14

File tree

31 files changed

+312
-332
lines changed

31 files changed

+312
-332
lines changed

.github/workflows/test.yaml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Unit Test
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Set up PDM
14+
uses: pdm-project/setup-pdm@v3
15+
with:
16+
python-version: 3.12
17+
cache: true
18+
- name: Install dependencies
19+
run: pdm install
20+
21+
- name: Run tests
22+
run: pdm run pytest
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
TODO:
3+
4+
Define a callable type that accepts a string parameter called `name` and returns None.
5+
6+
HINT: Use Protocol
7+
"""
8+
9+
10+
class SingleStringInput:
11+
...
12+
13+
14+
## End of your code ##
15+
def accept_single_string_input(func: SingleStringInput) -> None:
16+
func(name="name")
17+
18+
19+
def string_name(name: str) -> None:
20+
...
21+
22+
23+
def string_value(value: str) -> None:
24+
...
25+
26+
27+
def return_string(name: str) -> str:
28+
return name
29+
30+
31+
accept_single_string_input(string_name)
32+
accept_single_string_input(string_value) # expect-type-error
33+
accept_single_string_input(return_string) # expect-type-error

challenges/advanced-callable/question.py

-35
This file was deleted.

challenges/advanced-generic-param/question.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ def unwrap(self):
1515
return self.value
1616

1717

18-
def should_pass(w: Box[str]):
19-
a: str = w.unwrap()
20-
b: int = Box(1).unwrap()
18+
## End of your code ##
19+
from typing import assert_type
2120

2221

23-
def should_fail(w: Box[str]):
24-
a: int = w.unwrap()
25-
b: str = Box(1).unwrap()
22+
def should_pass(w: Box[str]):
23+
assert_type(w.unwrap(), str)
24+
assert_type(Box(1).unwrap(), int)

challenges/advanced-overload/question.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ def foo(value=None):
99
...
1010

1111

12+
## End of your code ##
13+
from typing import assert_type
14+
15+
1216
def should_pass():
13-
foo(42).upper()
14-
foo().bit_length()
17+
assert_type(foo(42), str)
18+
assert_type(foo(), int)
1519

1620

1721
def should_fail():
18-
foo(42).bit_length()
19-
foo().upper()
22+
assert_type(foo(42), int) # expect-type-error
23+
assert_type(foo(), str) # expect-type-error

challenges/advanced-protocol/question.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class SupportsQuack:
1010
...
1111

12-
12+
## End of your code ##
1313
class Duck:
1414
def quack(self) -> None:
1515
print("quack!")
@@ -20,9 +20,5 @@ def bark(self) -> None:
2020
print("bark!")
2121

2222

23-
def should_pass():
24-
duck: SupportsQuack = Duck()
25-
26-
27-
def should_fail():
28-
dog: SupportsQuack = Dog()
23+
duck: SupportsQuack = Duck()
24+
dog: SupportsQuack = Dog() # expect-type-error

challenges/advanced-recursive/question.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
Define a `Tree` type. `Tree` is a dictionary, whose keys are string, values are also `Tree`.
55
"""
66

7+
Tree = ...
78

9+
10+
## End of your code ##
811
def f(t: Tree):
912
pass
1013

1114

12-
def should_pass():
13-
f({})
14-
f({"foo": {}})
15-
f({"foo": {"bar": {}}})
16-
f({"foo": {"bar": {"baz": {}}}})
15+
f({})
16+
f({"foo": {}})
17+
f({"foo": {"bar": {}}})
18+
f({"foo": {"bar": {"baz": {}}}})
1719

1820

19-
def should_fail():
20-
f(1)
21-
f({"foo": []})
22-
f({"foo": {1: {}}})
21+
f(1) # expect-type-error
22+
f({"foo": []}) # expect-type-error
23+
f({"foo": {1: {}}}) # expect-type-error

challenges/advanced-self/question.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def return_self(self):
1212
return self
1313

1414

15+
## End of your code ##
1516
class SubclassOfFoo(Foo):
1617
pass
1718

@@ -22,4 +23,4 @@ def should_pass():
2223

2324

2425
def should_fail():
25-
sf: SubclassOfFoo = Foo().return_self()
26+
sf: SubclassOfFoo = Foo().return_self() # expect-type-error

challenges/advanced-typeguard/question.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ def is_string(value: Any):
1313
...
1414

1515

16-
def should_pass():
17-
a: str | None = "hello"
18-
if is_string(a):
19-
a.strip()
16+
## End of your code ##
17+
from typing import assert_type
2018

21-
22-
def should_fail():
23-
a: str | None = "hello"
24-
if not is_string(a):
25-
a.strip()
19+
a: str | None = "hello"
20+
if is_string(a):
21+
assert_type(a, str)
22+
else:
23+
assert_type(a, type(None))

challenges/basic-any/question.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
foo should accept an argument of arbitrary type.
55
"""
66

7-
import typing
8-
97

108
def foo(x):
119
pass
1210

1311

14-
def should_pass():
15-
foo(1)
16-
foo("10")
17-
18-
19-
def should_fail():
20-
foo(1, 2)
12+
## End of your code ##
13+
foo(1)
14+
foo("10")
15+
foo(1, 2) # expect-type-error

challenges/basic-callable/question.py

+17-18
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,28 @@
88
SingleStringInput = ...
99

1010

11-
def should_pass():
12-
def accept_single_string_input(func: SingleStringInput) -> None:
13-
...
11+
## End of your code ##
12+
def accept_single_string_input(func: SingleStringInput) -> None:
13+
...
1414

15-
def string_name(name: str) -> None:
16-
...
1715

18-
def string_value(value: str) -> None:
19-
...
16+
def string_name(name: str) -> None:
17+
...
2018

21-
accept_single_string_input(string_name)
22-
accept_single_string_input(string_value)
2319

20+
def string_value(value: str) -> None:
21+
...
2422

25-
def should_fail():
26-
def accept_single_string_input(func: SingleStringInput) -> None:
27-
...
2823

29-
def int_value(value: int) -> None:
30-
...
24+
def int_value(value: int) -> None:
25+
...
3126

32-
def new_name(name: str) -> str:
33-
return name
3427

35-
accept_single_string_input(int_value)
36-
accept_single_string_input(new_name)
28+
def new_name(name: str) -> str:
29+
return name
30+
31+
32+
accept_single_string_input(string_name)
33+
accept_single_string_input(string_value)
34+
accept_single_string_input(int_value) # expect-type-error
35+
accept_single_string_input(new_name) # expect-type-error

challenges/basic-dict/question.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ def foo(x):
99
pass
1010

1111

12-
def should_pass():
13-
foo({"foo": "bar"})
14-
15-
16-
def should_fail():
17-
foo({"foo": 1})
12+
## End of your code ##
13+
foo({"foo": "bar"})
14+
foo({"foo": 1}) # expect-type-error

challenges/basic-list/question.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ def foo(x):
99
pass
1010

1111

12-
def should_pass():
13-
foo(["foo", "bar"])
14-
15-
16-
def should_fail():
17-
foo(["foo", 1])
12+
## End of your code ##
13+
foo(["foo", "bar"])
14+
foo(["foo", 1]) # expect-type-error

challenges/basic-optional/question.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ def foo(x=0):
99
pass
1010

1111

12-
def should_pass():
13-
foo(10)
14-
foo()
12+
## End of your code ##
13+
foo(10)
14+
foo()
1515

16-
17-
def should_fail():
18-
foo("10")
16+
foo("10") # expect-type-error

challenges/basic-parameter/question.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ def foo(x):
99
pass
1010

1111

12-
def should_pass():
13-
foo(10)
12+
## End of your code ##
13+
foo(10)
1414

15-
16-
def should_fail():
17-
foo("10")
15+
foo("10") # expect-type-error

challenges/basic-return/question.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ def foo():
99
return 1
1010

1111

12-
def should_pass():
13-
i: int = foo()
12+
## End of your code ##
13+
from typing import assert_type
1414

15-
16-
def should_fail():
17-
i: str = foo()
15+
assert_type(foo(), int)
16+
assert_type(foo(), str) # expect-type-error

challenges/basic-tuple/question.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ def foo(x):
99
pass
1010

1111

12-
def should_pass():
13-
foo(("foo", 1))
12+
## End of your code ##
13+
foo(("foo", 1))
1414

15-
16-
def should_fail():
17-
foo((1, 2))
18-
foo(("foo", "bar"))
19-
foo((1, "foo"))
15+
foo((1, 2)) # expect-type-error
16+
foo(("foo", "bar")) # expect-type-error
17+
foo((1, "foo")) # expect-type-error

challenges/basic-variable/question.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@
33
44
`a` should be an integer.
55
"""
6+
from typing import Any
67

8+
a: Any
79

8-
a
910

10-
11-
def should_pass():
12-
global a
13-
a = 2
14-
15-
16-
def should_fail():
17-
global a
18-
a = "1"
11+
## End of your code ##
12+
a = 2
13+
a = "1" # expect-type-error

0 commit comments

Comments
 (0)