Skip to content

Commit 43e1e4a

Browse files
committed
Migrate existing challenges, update contribution guidance
1 parent 9426302 commit 43e1e4a

File tree

4 files changed

+19
-69
lines changed

4 files changed

+19
-69
lines changed

challenges/advanced-generic-param/question.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,5 @@ def unwrap(self):
1818
## End of your code ##
1919
from typing import assert_type
2020

21-
22-
def should_pass(w: Box[str]):
23-
assert_type(w.unwrap(), str)
24-
assert_type(Box(1).unwrap(), int)
21+
assert_type(w.unwrap(), str)
22+
assert_type(Box(1).unwrap(), int)

challenges/advanced-overload/question.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@ def foo(value=None):
1212
## End of your code ##
1313
from typing import assert_type
1414

15+
assert_type(foo(42), str)
16+
assert_type(foo(), int)
1517

16-
def should_pass():
17-
assert_type(foo(42), str)
18-
assert_type(foo(), int)
19-
20-
21-
def should_fail():
22-
assert_type(foo(42), int) # expect-type-error
23-
assert_type(foo(), str) # expect-type-error
18+
assert_type(foo(42), int) # expect-type-error
19+
assert_type(foo(), str) # expect-type-error

challenges/advanced-self/question.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ class SubclassOfFoo(Foo):
1717
pass
1818

1919

20-
def should_pass():
21-
f: Foo = Foo().return_self()
22-
sf: SubclassOfFoo = SubclassOfFoo().return_self()
20+
f: Foo = Foo().return_self()
21+
sf: SubclassOfFoo = SubclassOfFoo().return_self()
2322

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

docs/Contribute.md

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,27 @@
88

99
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`.
1010

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`:
1212
```python
1313
"""
1414
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.
1716
"""
1817

1918
def foo(x):
2019
pass
2120

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
5424
```
5525

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.
5730

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.
7332

7433
5. Test with [`mypy`](https://mypy.readthedocs.io/) to make sure your new challenge works as expected.
7534

0 commit comments

Comments
 (0)