Skip to content

Commit d000b4b

Browse files
authored
Document how to use assert_type (#148)
1 parent 1f5c621 commit d000b4b

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
2222
pytest-version: ["~=7.2", "~=8.1"]
2323
# TODO: remove after several new versions of mypy
24-
mypy-version: ["~=1.7", "~=1.9"]
24+
mypy-version: ["~=1.7", "~=1.10"]
2525

2626
steps:
2727
- uses: actions/checkout@v4

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ just execute:
3333
pytest
3434
```
3535

36+
### Asserting types
37+
38+
There are two ways to assert types.
39+
The custom one and regular [`typing.assert_type`](https://docs.python.org/3/library/typing.html#typing.assert_type).
40+
41+
Our custom type assertion uses `reveal_type` helper and custom output matchers:
42+
43+
```yml
44+
- case: using_reveal_type
45+
main: |
46+
instance = 1
47+
reveal_type(instance) # N: Revealed type is 'builtins.int'
48+
```
49+
50+
This method also allows to use `# E:` for matching exact error messages and codes.
51+
52+
But, you can also use regular `assert_type`, examples can be [found here](https://github.com/typeddjango/pytest-mypy-plugins/blob/master/pytest_mypy_plugins/tests/test-assert-type.yml).
53+
3654
### Paths
3755

3856
The `PYTHONPATH` and `MYPYPATH` environment variables, if set, are passed to `mypy` on invocation.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
- case: assert_type
2+
main: |
3+
from typing_extensions import assert_type
4+
5+
def x() -> int:
6+
return 1
7+
8+
assert_type(x(), int)
9+
10+
- case: assert_type_error
11+
mypy_config: |
12+
warn_unused_ignores = true
13+
main: |
14+
from typing_extensions import assert_type
15+
16+
def x() -> int:
17+
return 1
18+
19+
assert_type(x(), str) # type: ignore

0 commit comments

Comments
 (0)