|
8 | 8 |
|
9 | 9 | For example, say you want to add a new challenge about [Protocols](https://mypy.readthedocs.io/en/stable/protocols.html). Since this is an advanced topic, you may name the directory `advanced-protocol`.
|
10 | 10 |
|
11 |
| -4. Put a `question.py` and a `solution.py` in the new directory. Here's an example: |
| 11 | +4. Put a `question.py` and a `solution.py` in the new directory. Here's an example `question.py`: |
12 | 12 | ```python
|
13 | 13 | """
|
14 | 14 | TODO:
|
15 |
| -
|
16 |
| - foo should accept a dict argument, both keys and values are string. |
| 15 | + foo should accept a dict argument, both keys and values are string. |
17 | 16 | """
|
18 | 17 |
|
19 | 18 | def foo(x):
|
20 | 19 | pass
|
21 | 20 |
|
22 |
| - def should_pass(): |
23 |
| - foo({"foo": "bar"}) |
24 |
| - |
25 |
| - def should_fail(): |
26 |
| - foo({"foo": 1}) |
27 |
| - ``` |
28 |
| - |
29 |
| - You want to include several things in `question.py` |
30 |
| - |
31 |
| - - Describe the challenge, make sure people understand what they need to accomplish (the `TODO:` part) |
32 |
| - - Add two functions `should_pass` and `should_fail` as test cases. The **function names are mandatory**. With correct code, `should_pass` should pass type check, and `should_fail` should fail type check. |
33 |
| - |
34 |
| - Using the above challenge as an example, the correct answer is `def foo(x: dict[str, str]): pass`. |
35 |
| - |
36 |
| - This will pass type check: |
37 |
| - |
38 |
| - ```python |
39 |
| - def foo(x: dict[str, str]): |
40 |
| - pass |
41 |
| - |
42 |
| - def should_pass(): |
43 |
| - foo({"foo": "bar"}) |
44 |
| - ``` |
45 |
| - |
46 |
| - This will fail type check |
47 |
| - |
48 |
| - ```python |
49 |
| - def foo(x: dict[str, str]): |
50 |
| - pass |
51 |
| - |
52 |
| - def should_fail(): |
53 |
| - foo({"foo": 1}) |
| 21 | + ## End of your code ## |
| 22 | + foo({"foo": "bar"}) |
| 23 | + foo({"foo": 1}) # expect-type-error |
54 | 24 | ```
|
55 | 25 |
|
56 |
| - And the `solution.py` shall look like the same as `question.py`, except that it has a solution provided. |
| 26 | + You want to include several things in `question.py`: |
| 27 | + - Describe the challenge, make sure people understand what they need to accomplish (i.e. the `TODO:` part) |
| 28 | + - A comment `## End of your code ##`. This is mandatory, just copy and paste it. |
| 29 | + - Several test cases. Add a comment `# expect-type-error` after the lines where type errors should be thrown. |
57 | 30 |
|
58 |
| - ```python |
59 |
| - """ |
60 |
| - TODO: |
61 |
| -
|
62 |
| - foo should accept a dict argument, both keys and values are string. |
63 |
| - """ |
64 |
| - |
65 |
| - def foo(x: dict[str, str]): pass |
66 |
| - |
67 |
| - def should_pass(): |
68 |
| - foo({"foo": "bar"}) |
69 |
| - |
70 |
| - def should_fail(): |
71 |
| - foo({"foo": 1}) |
72 |
| - ``` |
| 31 | + `solution.py` contains the right solution, with everything else unchanged. |
73 | 32 |
|
74 | 33 | 5. Test with [`mypy`](https://mypy.readthedocs.io/) to make sure your new challenge works as expected.
|
75 | 34 |
|
|
0 commit comments