Skip to content

Setup pre-commit hooks and reformat code #245

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

Merged
merged 2 commits into from
Jan 9, 2022
Merged
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
39 changes: 39 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: 'v4.1.0'
hooks:
- id: check-merge-conflict
exclude: "rst$"
- repo: https://github.com/asottile/yesqa
rev: v1.3.0
hooks:
- id: yesqa
- repo: https://github.com/Zac-HD/shed
rev: 0.6.0 # 0.7 does not support Python 3.7
hooks:
- id: shed
args:
- --refactor
- --py37-plus
types_or:
- python
- markdown
- rst
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: fix-encoding-pragma
args: [--remove]
- id: check-yaml
- id: debug-statements
- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
language_version: python3
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.9.0
hooks:
- id: python-use-type-annotations
1 change: 0 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,3 @@ Apache License
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ clean-test: ## remove test and coverage artifacts
rm -f .coverage
rm -fr htmlcov/

lint: ## check style with flake8
flake8 pytest_asyncio tests
black --check --verbose pytest_asyncio tests
lint:
# CI env-var is set by GitHub actions
ifdef CI
pre-commit run --all-files --show-diff-on-failure
else
pre-commit run --all-files
endif

test:
pytest tests

install:
pip install -U pre-commit
pre-commit install
14 changes: 9 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ provides useful fixtures and markers to make testing easier.
@pytest.mark.asyncio
async def test_some_asyncio_code():
res = await library.do_something()
assert b'expected result' == res
assert b"expected result" == res

pytest-asyncio has been strongly influenced by pytest-tornado_.

Expand Down Expand Up @@ -139,9 +139,9 @@ Use ``pytest.mark.asyncio`` for this purpose.
.. code-block:: python

def test_http_client(event_loop):
url = 'http://httpbin.org/get'
url = "http://httpbin.org/get"
resp = event_loop.run_until_complete(http_client(url))
assert b'HTTP/1.1 200 OK' in resp
assert b"HTTP/1.1 200 OK" in resp

This fixture can be easily overridden in any of the standard pytest locations
(e.g. directly in the test file, or in ``conftest.py``) to use a non-default
Expand Down Expand Up @@ -189,12 +189,14 @@ Asynchronous fixtures are defined just like ordinary pytest fixtures, except the

import pytest_asyncio


@pytest_asyncio.fixture
async def async_gen_fixture():
await asyncio.sleep(0.1)
yield 'a value'
yield "a value"


@pytest_asyncio.fixture(scope='module')
@pytest_asyncio.fixture(scope="module")
async def async_fixture():
return await asyncio.sleep(0.1)

Expand Down Expand Up @@ -227,11 +229,13 @@ Only test coroutines will be affected (by default, coroutines prefixed by
.. code-block:: python

import asyncio

import pytest

# All test coroutines will be treated as marked.
pytestmark = pytest.mark.asyncio


async def test_example(event_loop):
"""No marker!"""
await asyncio.sleep(0, loop=event_loop)
Expand Down
1 change: 0 additions & 1 deletion pytest_asyncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@

from .plugin import fixture


__all__ = ("fixture",)
6 changes: 4 additions & 2 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ def pytest_fixture_post_finalizer(fixturedef, request):
"""Called after fixture teardown"""
if fixturedef.argname == "event_loop":
policy = asyncio.get_event_loop_policy()
policy.get_event_loop().close() # Clean up existing loop to avoid ResourceWarnings
# Clean up existing loop to avoid ResourceWarnings
policy.get_event_loop().close()
new_loop = policy.new_event_loop() # Replace existing event loop
# Ensure subsequent calls to get_event_loop() succeed
policy.set_event_loop(new_loop)
Expand Down Expand Up @@ -297,7 +298,8 @@ def pytest_pyfunc_call(pyfuncitem):
"""
Pytest hook called before a test case is run.

Wraps marked tests in a synchronous function where the wrapped test coroutine is executed in an event loop.
Wraps marked tests in a synchronous function
where the wrapped test coroutine is executed in an event loop.
"""
if "asyncio" in pyfuncitem.keywords:
if getattr(pyfuncitem.obj, "is_hypothesis_test", False):
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ filterwarnings = error
license_file = LICENSE

[flake8]
ignore = E203, E501, W503
max-line-length = 88
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
from pathlib import Path

from setuptools import setup, find_packages
from setuptools import find_packages, setup


def find_version():
Expand Down
3 changes: 2 additions & 1 deletion tests/async_fixtures/test_async_fixtures_with_finalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def port_finalizer(finalizer):
async def port_afinalizer():
# await task using current loop retrieved from the event loop policy
# RuntimeError is raised if task is created on a different loop.
# This can happen when pytest_fixture_setup does not set up the loop correctly,
# This can happen when pytest_fixture_setup
# does not set up the loop correctly,
# for example when policy.set_event_loop() is called with a wrong argument
await finalizer

Expand Down
1 change: 0 additions & 1 deletion tests/hypothesis/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import asyncio

import pytest

from hypothesis import given, strategies as st


Expand Down
14 changes: 6 additions & 8 deletions tests/hypothesis/test_inherited_test.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import hypothesis.strategies as st
from hypothesis import given
import pytest
from hypothesis import given


class BaseClass:
@pytest.mark.asyncio
@given(value=st.integers())
async def test_hypothesis(self, value: int) -> None:
assert True
pass


class TestOne(BaseClass):
"""During the first execution the Hypothesis test is wrapped in a synchronous function."""

pass
"""During the first execution the Hypothesis test
is wrapped in a synchronous function."""


class TestTwo(BaseClass):
"""Execute the test a second time to ensure that the test receives a fresh event loop."""

pass
"""Execute the test a second time to ensure that
the test receives a fresh event loop."""
2 changes: 0 additions & 2 deletions tests/multiloop/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
class CustomSelectorLoop(asyncio.SelectorEventLoop):
"""A subclass with no overrides, just to test for presence."""

pass


@pytest.fixture
def event_loop():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

@pytest.mark.asyncio
async def test_uses_loop_provided_by_custom_policy():
"""Asserts that test cases use the event loop provided by the custom event loop policy"""
"""Asserts that test cases use the event loop
provided by the custom event loop policy"""
assert type(asyncio.get_event_loop()).__name__ == "TestEventLoop"


Expand Down
2 changes: 0 additions & 2 deletions tests/sessionloop/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
class CustomSelectorLoopSession(asyncio.SelectorEventLoop):
"""A subclass with no overrides, just to test for presence."""

pass


loop = CustomSelectorLoopSession()

Expand Down
4 changes: 3 additions & 1 deletion tests/test_asyncio_fixture.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import asyncio
import pytest_asyncio

import pytest

import pytest_asyncio


@pytest_asyncio.fixture
async def fixture_bare():
Expand Down
1 change: 1 addition & 0 deletions tests/test_dependent_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio

import pytest


Expand Down
9 changes: 6 additions & 3 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import asyncio

import pytest

import pytest_asyncio.plugin


Expand All @@ -26,7 +27,7 @@ async def test_asyncio_marker():
@pytest.mark.xfail(reason="need a failure", strict=True)
@pytest.mark.asyncio
def test_asyncio_marker_fail():
assert False
raise AssertionError


@pytest.mark.asyncio
Expand Down Expand Up @@ -196,13 +197,15 @@ class TestMarkerInClassBasedTests:

@pytest.mark.asyncio
async def test_asyncio_marker_with_explicit_loop_fixture(self, event_loop):
"""Test the "asyncio" marker works on a method in a class-based test with explicit loop fixture."""
"""Test the "asyncio" marker works on a method in
a class-based test with explicit loop fixture."""
ret = await async_coro()
assert ret == "ok"

@pytest.mark.asyncio
async def test_asyncio_marker_with_implicit_loop_fixture(self):
"""Test the "asyncio" marker works on a method in a class-based test with implicit loop fixture."""
"""Test the "asyncio" marker works on a method in
a class-based test with implicit loop fixture."""
ret = await async_coro()
assert ret == "ok"

Expand Down
1 change: 0 additions & 1 deletion tests/test_subprocess.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Tests for using subprocesses in tests."""
import asyncio
import asyncio.subprocess
import sys

Expand Down
3 changes: 1 addition & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ skip_install = true
basepython = python3.9
extras = tests
deps =
flake8
black
pre-commit
commands =
make lint

Expand Down