Skip to content

Commit fe0579e

Browse files
committed
convert the example conftest.py to a pytest plugin
1 parent cbc5855 commit fe0579e

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ and the observed types will be written (in JSON form) to the filename
2828
you pass to `dump_stats()`. You can have multiple start/stop pairs
2929
per dump call.
3030

31-
If you'd like to automatically collect types when you run `pytest`,
32-
see `example/example_conftest.py` and `example/README.md`.
31+
If you'd like to automatically collect types when you run `pytest` you can
32+
run pyannotate as a `pytest` plugin with `pytest -p pyannotate_tools.pytest`
33+
which will output `type_info.json` which you can read more about in
34+
`example/README.md`.
3335

3436
Instead of using `start()` and `stop()` you can also use a context
3537
manager:

example/README.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,14 @@ gcd.py
6969
Alternative, using pytest
7070
-------------------------
7171

72-
For pytest users, the example_conftest.py file shows how to
73-
automatically configures pytest to collect types when running tests.
74-
The test_gcd.py file contains a simple test to demonstrate this. Copy
75-
the contents of example_conftest.py to your conftest.py file and run
76-
pytest; it will then generate a type_info.json file like the one
77-
above.
72+
For pytest users, pytest can collect types when running tests. The test_gcd.py
73+
file contains a simple test to demonstrate this. You can run the pyannotate
74+
pytest plugin with the argument `-p pyannotate_tools.pytest` or by adding the
75+
plugin to your `conftest.py` (See [installing and using plugins](https://docs.pytest.org/en/latest/plugins.html#installing-and-using-plugins) for more information about pytest plugins).
76+
77+
For a full example (it will generate output.json like the one above):
78+
79+
```
80+
# omit --type-info to use the default of type_info.json
81+
pytest -p pyannotate_tools.pytest --type-info output.json test_gcd.py
82+
```

example/example_conftest.py renamed to pyannotate_tools/pytest.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
# Configuration for pytest to automatically collect types.
2-
# Thanks to Guilherme Salgado.
3-
41
import pytest
52

63

4+
def pytest_addoption(parser):
5+
group = parser.getgroup("pyannotate")
6+
group.addoption(
7+
"--type-info",
8+
default="type_info.json",
9+
help="File to write type information to (default: %(default)s).")
10+
11+
12+
def pytest_configure(config):
13+
if config.pluginmanager.hasplugin('xdist'):
14+
if config.getoption('dist') != 'no':
15+
print('Disabling xdist for pyannotate collection.')
16+
config.option.dist = 'no'
17+
18+
719
def pytest_collection_finish(session):
820
"""Handle the pytest collection finish hook: configure pyannotate.
921
@@ -25,4 +37,4 @@ def collect_types_fixture():
2537

2638
def pytest_sessionfinish(session, exitstatus):
2739
from pyannotate_runtime import collect_types
28-
collect_types.dump_stats("type_info.json")
40+
collect_types.dump_stats(session.config.option.type_info)

requirements.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
mypy_extensions>=0.3.0
2-
pytest>=3.3.0
2+
pytest>=3.3.0; python_version > '3.5.1'
3+
# pytest >5.3.0 uses typing.Type from Python 3.5.2
4+
pytest>=3.3.0,<=5.3.0; python_version <= '3.5.1'
5+
# importlib-metadata is needed for Python 3.5+, but pip does not seem to be
6+
# pinning it to a correct version (possibly because it's a transitive
7+
# dependency). Python 3.5 support was dropped in importlib-metadata 3.0.0
8+
importlib-metadata>=0.12,<3.0.0; python_version > '3.5.0' and python_version<"3.8"
39
setuptools>=28.8.0
410
six>=1.11.0
511
typing>=3.6.2; python_version < '3.5'

0 commit comments

Comments
 (0)