Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert the example conftest.py to a pytest plugin #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ and the observed types will be written (in JSON form) to the filename
you pass to `dump_stats()`. You can have multiple start/stop pairs
per dump call.

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

Instead of using `start()` and `stop()` you can also use a context
manager:
Expand Down
17 changes: 11 additions & 6 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ gcd.py
Alternative, using pytest
-------------------------

For pytest users, the example_conftest.py file shows how to
automatically configures pytest to collect types when running tests.
The test_gcd.py file contains a simple test to demonstrate this. Copy
the contents of example_conftest.py to your conftest.py file and run
pytest; it will then generate a type_info.json file like the one
above.
For pytest users, pytest can collect types when running tests. The test_gcd.py
file contains a simple test to demonstrate this. You can run the pyannotate
pytest plugin with the argument `-p pyannotate_tools.pytest` or by adding the
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).

For a full example (it will generate output.json like the one above):

```
# omit --type-info to use the default of type_info.json
pytest -p pyannotate_tools.pytest --type-info output.json test_gcd.py
```
20 changes: 16 additions & 4 deletions example/example_conftest.py → pyannotate_tools/pytest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
# Configuration for pytest to automatically collect types.
# Thanks to Guilherme Salgado.

import pytest


def pytest_addoption(parser):
group = parser.getgroup("pyannotate")
group.addoption(
"--type-info",
default="type_info.json",
help="File to write type information to (default: %(default)s).")


def pytest_configure(config):
if config.pluginmanager.hasplugin('xdist'):
if config.getoption('dist') != 'no':
print('Disabling xdist for pyannotate collection.')
config.option.dist = 'no'


def pytest_collection_finish(session):
"""Handle the pytest collection finish hook: configure pyannotate.

Expand All @@ -25,4 +37,4 @@ def collect_types_fixture():

def pytest_sessionfinish(session, exitstatus):
from pyannotate_runtime import collect_types
collect_types.dump_stats("type_info.json")
collect_types.dump_stats(session.config.option.type_info)
9 changes: 8 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
mypy_extensions>=0.3.0
pytest>=3.3.0
pytest>=3.3.0; python_version > '3.5'
# pytest >5.3.0 uses typing.Type from Python 3.5.2
pytest>=3.3.0,<=5.3.0; python_version <= '3.5'
# importlib-metadata is needed for Python 3.5+, but pip does not seem to be
# pinning it to a correct version for Python 3.5 (possibly because it's a
# transitive dependency).
# Python 3.5 support was dropped in importlib-metadata 3.0.0
importlib-metadata>=0.12,<3.0.0; python_version == '3.5'
setuptools>=28.8.0
six>=1.11.0
typing>=3.6.2; python_version < '3.5'