@@ -20,29 +20,47 @@ a few of them.
2020 For simplicity, we will assume that type-checking is done with ``mypy ``.
2121 Many of these strategies can be applied to other type-checkers as well.
2222
23- Testing Using ``mypy --warn-unused-ignores ``
24- --------------------------------------------
23+ Testing Using ``assert_type `` and `` --warn-unused-ignores ``
24+ -----------------------------------------------------------
2525
26- Clever use of `` --warn-unused-ignores `` can be used to check that certain
27- expressions are or are not well-typed .
26+ The idea is to write normal Python files, set aside in a dedicated directory like `` typing_tests/ ``, which assert certain properties
27+ of the type annotations .
2828
29- The idea is to write normal python files which contain valid expressions along
29+ ``assert_type `` (``mypy `` 0.950 and above) can ensure that the type annotation produces the expected type.
30+
31+ If the following file is under test:
32+
33+ .. code-block :: python
34+
35+ # foo.py
36+ def bar (x : int ) -> str :
37+ return str (x)
38+
39+ then the following file tests ``foo.py ``:
40+
41+ .. code-block :: python
42+
43+ from typing_extensions import assert_type
44+
45+ assert_type(bar(42 ), str )
46+
47+ Clever use of ``mypy --warn-unused-ignores `` can be used to check that certain
48+ expressions are or are not well-typed. The idea is to have valid expressions along
3049with invalid expressions annotated with ``type: ignore `` comments. When
3150``mypy --warn-unused-ignores `` is run on these files, it should pass.
32- A directory of test files, ``typing_tests/ ``, can be maintained.
3351
3452This strategy does not offer strong guarantees about the types under test, but
3553it requires no additional tooling.
3654
37- If the following file is under test
55+ If the following file is under test:
3856
3957.. code-block :: python
4058
4159 # foo.py
4260 def bar (x : int ) -> str :
4361 return str (x)
4462
45- Then the following file tests ``foo.py ``:
63+ then the following file tests ``foo.py ``:
4664
4765.. code-block :: python
4866
0 commit comments