Skip to content

Commit 5155ed3

Browse files
Improve documentation (#30)
1 parent b12d3e0 commit 5155ed3

File tree

1 file changed

+62
-11
lines changed

1 file changed

+62
-11
lines changed

README.md

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,57 @@ pip install pytest-mypy-plugins
1414

1515
## Usage
1616

17-
Examples of a test case:
17+
### Running
18+
19+
Plugin, after installation, is automatically picked up by `pytest` therefore it is sufficient to
20+
just execute:
21+
22+
```bash
23+
pytest
24+
```
25+
26+
### What is a test case?
27+
28+
In general each test case is just an element in an array written in a properly formatted `YAML` file.
29+
On top of that, each case must comply to following types:
30+
31+
| Property | Type | Description |
32+
| --------------- | ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------- |
33+
| `case` | `str` | Name of the test case, complies to `[a-zA-Z0-9]` pattern |
34+
| `main` | `str` | Portion of the code as if written in `.py` file |
35+
| `files` | `Optional[List[File]]=[]`\* | List of extra files to simulate imports if needed |
36+
| `disable_cache` | `Optional[bool]=False` | Set to `true` disables `mypy` caching |
37+
| `mypy_config` | `Optional[Dict[str, Union[str, int, bool, float]]]={}` | Inline `mypy` configuration, passed directly to `mypy` as `--config-file` option |
38+
| `env` | `Optional[Dict[str, str]]={}` | Environmental variables to be provided inside of test run |
39+
| `parametrized` | `Optional[List[Parameter]]=[]`\* | List of parameters, similar to [`@pytest.mark.parametrize`](https://docs.pytest.org/en/stable/parametrize.html) |
40+
| `skip` | `str` | Expression evaluated with following globals set: `sys`, `os`, `pytest` and `platform` |
41+
42+
Appendix to **pseudo** types used above:
43+
44+
```python
45+
class File:
46+
path: str
47+
content: Optional[str] = None
48+
Parameter = Mapping[str, Any]
49+
```
50+
51+
Implementation notes:
52+
53+
- `main` must be non-empty string that evaluates to valid **Python** code,
54+
- `content` of each of extra files must evaluate to valid **Python** code,
55+
- `parametrized` entries must all be the objects of the same _type_. It simply means that each
56+
entry must have **exact** same set of keys,
57+
- `skip` - an expression set in `skip` is passed directly into
58+
[`eval`](https://docs.python.org/3/library/functions.html#eval). It is advised to take a peek and
59+
learn about how `eval` works.
60+
61+
### Example
62+
63+
#### 1. Inline type expectations
1864

1965
```yaml
2066
# typesafety/test_request.yml
2167
- case: request_object_has_user_of_type_auth_user_model
22-
disable_cache: true
2368
main: |
2469
from django.http.request import HttpRequest
2570
reveal_type(HttpRequest().user) # N: Revealed type is 'myapp.models.MyUser'
@@ -32,23 +77,31 @@ Examples of a test case:
3277
from django.db import models
3378
class MyUser(models.Model):
3479
pass
80+
```
81+
82+
#### 2. `@parametrized`
83+
84+
```yaml
3585
- case: with_params
3686
parametrized:
3787
- val: 1
3888
rt: builtins.int
3989
- val: 1.0
4090
rt: builtins.float
4191
main: |
42-
reveal_type({[ val }}) # N: Reveal type is '{{ rt }}'
92+
reveal_type({[ val }}) # N: Revealed type is '{{ rt }}'
4393
```
4494

45-
Running:
95+
#### 3. Longer type expectations
4696

47-
```bash
48-
pytest
97+
```yaml
98+
- case: with_out
99+
main: |
100+
reveal_type('str')
101+
out: |
102+
main:1: note: Revealed type is 'builtins.str'
49103
```
50104

51-
52105
## Options
53106

54107
```
@@ -57,18 +110,16 @@ mypy-tests:
57110
Base directory for tests to use
58111
--mypy-ini-file=MYPY_INI_FILE
59112
Which .ini file to use as a default config for tests
60-
--mypy-same-process
61-
Now, to help with various issues in django-stubs, it runs every single test in the subprocess mypy call.
113+
--mypy-same-process
114+
Now, to help with various issues in django-stubs, it runs every single test in the subprocess mypy call.
62115
Some debuggers cannot attach to subprocess, so enable this flag to make mypy check happen in the same process.
63116
(Could cause cache issues)
64117
```
65118
66-
67119
## Further reading
68120
69121
- [Testing mypy stubs, plugins, and types](https://sobolevn.me/2019/08/testing-mypy-types)
70122
71-
72123
## License
73124
74125
[MIT](https://github.com/typeddjango/pytest-mypy-plugins/blob/master/LICENSE)

0 commit comments

Comments
 (0)