Skip to content

Commit 4c7da65

Browse files
authored
Setup pre-commit hooks and reformat code (#245)
1 parent 5d4954a commit 4c7da65

19 files changed

+86
-36
lines changed

.pre-commit-config.yaml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: 'v4.1.0'
4+
hooks:
5+
- id: check-merge-conflict
6+
exclude: "rst$"
7+
- repo: https://github.com/asottile/yesqa
8+
rev: v1.3.0
9+
hooks:
10+
- id: yesqa
11+
- repo: https://github.com/Zac-HD/shed
12+
rev: 0.6.0 # 0.7 does not support Python 3.7
13+
hooks:
14+
- id: shed
15+
args:
16+
- --refactor
17+
- --py37-plus
18+
types_or:
19+
- python
20+
- markdown
21+
- rst
22+
- repo: https://github.com/pre-commit/pre-commit-hooks
23+
rev: v4.1.0
24+
hooks:
25+
- id: trailing-whitespace
26+
- id: end-of-file-fixer
27+
- id: fix-encoding-pragma
28+
args: [--remove]
29+
- id: check-yaml
30+
- id: debug-statements
31+
- repo: https://gitlab.com/pycqa/flake8
32+
rev: 3.9.2
33+
hooks:
34+
- id: flake8
35+
language_version: python3
36+
- repo: https://github.com/pre-commit/pygrep-hooks
37+
rev: v1.9.0
38+
hooks:
39+
- id: python-use-type-annotations

LICENSE

-1
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,3 @@ Apache License
199199
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200200
See the License for the specific language governing permissions and
201201
limitations under the License.
202-

Makefile

+11-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,17 @@ clean-test: ## remove test and coverage artifacts
2020
rm -f .coverage
2121
rm -fr htmlcov/
2222

23-
lint: ## check style with flake8
24-
flake8 pytest_asyncio tests
25-
black --check --verbose pytest_asyncio tests
23+
lint:
24+
# CI env-var is set by GitHub actions
25+
ifdef CI
26+
pre-commit run --all-files --show-diff-on-failure
27+
else
28+
pre-commit run --all-files
29+
endif
2630

2731
test:
2832
pytest tests
33+
34+
install:
35+
pip install -U pre-commit
36+
pre-commit install

README.rst

+9-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ provides useful fixtures and markers to make testing easier.
2525
@pytest.mark.asyncio
2626
async def test_some_asyncio_code():
2727
res = await library.do_something()
28-
assert b'expected result' == res
28+
assert b"expected result" == res
2929
3030
pytest-asyncio has been strongly influenced by pytest-tornado_.
3131

@@ -139,9 +139,9 @@ Use ``pytest.mark.asyncio`` for this purpose.
139139
.. code-block:: python
140140
141141
def test_http_client(event_loop):
142-
url = 'http://httpbin.org/get'
142+
url = "http://httpbin.org/get"
143143
resp = event_loop.run_until_complete(http_client(url))
144-
assert b'HTTP/1.1 200 OK' in resp
144+
assert b"HTTP/1.1 200 OK" in resp
145145
146146
This fixture can be easily overridden in any of the standard pytest locations
147147
(e.g. directly in the test file, or in ``conftest.py``) to use a non-default
@@ -189,12 +189,14 @@ Asynchronous fixtures are defined just like ordinary pytest fixtures, except the
189189
190190
import pytest_asyncio
191191
192+
192193
@pytest_asyncio.fixture
193194
async def async_gen_fixture():
194195
await asyncio.sleep(0.1)
195-
yield 'a value'
196+
yield "a value"
197+
196198
197-
@pytest_asyncio.fixture(scope='module')
199+
@pytest_asyncio.fixture(scope="module")
198200
async def async_fixture():
199201
return await asyncio.sleep(0.1)
200202
@@ -227,11 +229,13 @@ Only test coroutines will be affected (by default, coroutines prefixed by
227229
.. code-block:: python
228230
229231
import asyncio
232+
230233
import pytest
231234
232235
# All test coroutines will be treated as marked.
233236
pytestmark = pytest.mark.asyncio
234237
238+
235239
async def test_example(event_loop):
236240
"""No marker!"""
237241
await asyncio.sleep(0, loop=event_loop)

pytest_asyncio/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33

44
from .plugin import fixture
55

6-
76
__all__ = ("fixture",)

pytest_asyncio/plugin.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ def pytest_fixture_post_finalizer(fixturedef, request):
159159
"""Called after fixture teardown"""
160160
if fixturedef.argname == "event_loop":
161161
policy = asyncio.get_event_loop_policy()
162-
policy.get_event_loop().close() # Clean up existing loop to avoid ResourceWarnings
162+
# Clean up existing loop to avoid ResourceWarnings
163+
policy.get_event_loop().close()
163164
new_loop = policy.new_event_loop() # Replace existing event loop
164165
# Ensure subsequent calls to get_event_loop() succeed
165166
policy.set_event_loop(new_loop)
@@ -282,7 +283,8 @@ def pytest_pyfunc_call(pyfuncitem):
282283
"""
283284
Pytest hook called before a test case is run.
284285
285-
Wraps marked tests in a synchronous function where the wrapped test coroutine is executed in an event loop.
286+
Wraps marked tests in a synchronous function
287+
where the wrapped test coroutine is executed in an event loop.
286288
"""
287289
if "asyncio" in pyfuncitem.keywords:
288290
if getattr(pyfuncitem.obj, "is_hypothesis_test", False):

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ filterwarnings = error
1515
license_file = LICENSE
1616

1717
[flake8]
18-
ignore = E203, E501, W503
18+
max-line-length = 88

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22
from pathlib import Path
33

4-
from setuptools import setup, find_packages
4+
from setuptools import find_packages, setup
55

66

77
def find_version():

tests/async_fixtures/test_async_fixtures_with_finalizer.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def port_finalizer(finalizer):
4646
async def port_afinalizer():
4747
# await task using current loop retrieved from the event loop policy
4848
# RuntimeError is raised if task is created on a different loop.
49-
# This can happen when pytest_fixture_setup does not set up the loop correctly,
49+
# This can happen when pytest_fixture_setup
50+
# does not set up the loop correctly,
5051
# for example when policy.set_event_loop() is called with a wrong argument
5152
await finalizer
5253

tests/hypothesis/test_base.py

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import asyncio
55

66
import pytest
7-
87
from hypothesis import given, strategies as st
98

109

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import hypothesis.strategies as st
2-
from hypothesis import given
32
import pytest
3+
from hypothesis import given
44

55

66
class BaseClass:
77
@pytest.mark.asyncio
88
@given(value=st.integers())
99
async def test_hypothesis(self, value: int) -> None:
10-
assert True
10+
pass
1111

1212

1313
class TestOne(BaseClass):
14-
"""During the first execution the Hypothesis test is wrapped in a synchronous function."""
15-
16-
pass
14+
"""During the first execution the Hypothesis test
15+
is wrapped in a synchronous function."""
1716

1817

1918
class TestTwo(BaseClass):
20-
"""Execute the test a second time to ensure that the test receives a fresh event loop."""
21-
22-
pass
19+
"""Execute the test a second time to ensure that
20+
the test receives a fresh event loop."""

tests/multiloop/conftest.py

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
class CustomSelectorLoop(asyncio.SelectorEventLoop):
77
"""A subclass with no overrides, just to test for presence."""
88

9-
pass
10-
119

1210
@pytest.fixture
1311
def event_loop():

tests/respect_event_loop_policy/test_respects_event_loop_policy.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
@pytest.mark.asyncio
88
async def test_uses_loop_provided_by_custom_policy():
9-
"""Asserts that test cases use the event loop provided by the custom event loop policy"""
9+
"""Asserts that test cases use the event loop
10+
provided by the custom event loop policy"""
1011
assert type(asyncio.get_event_loop()).__name__ == "TestEventLoop"
1112

1213

tests/sessionloop/conftest.py

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
class CustomSelectorLoopSession(asyncio.SelectorEventLoop):
77
"""A subclass with no overrides, just to test for presence."""
88

9-
pass
10-
119

1210
loop = CustomSelectorLoopSession()
1311

tests/test_asyncio_fixture.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import asyncio
2-
import pytest_asyncio
2+
33
import pytest
44

5+
import pytest_asyncio
6+
57

68
@pytest_asyncio.fixture
79
async def fixture_bare():

tests/test_dependent_fixtures.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
23
import pytest
34

45

tests/test_simple.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import asyncio
33

44
import pytest
5+
56
import pytest_asyncio.plugin
67

78

@@ -26,7 +27,7 @@ async def test_asyncio_marker():
2627
@pytest.mark.xfail(reason="need a failure", strict=True)
2728
@pytest.mark.asyncio
2829
def test_asyncio_marker_fail():
29-
assert False
30+
raise AssertionError
3031

3132

3233
@pytest.mark.asyncio
@@ -196,13 +197,15 @@ class TestMarkerInClassBasedTests:
196197

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

203205
@pytest.mark.asyncio
204206
async def test_asyncio_marker_with_implicit_loop_fixture(self):
205-
"""Test the "asyncio" marker works on a method in a class-based test with implicit loop fixture."""
207+
"""Test the "asyncio" marker works on a method in
208+
a class-based test with implicit loop fixture."""
206209
ret = await async_coro()
207210
assert ret == "ok"
208211

tests/test_subprocess.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""Tests for using subprocesses in tests."""
2-
import asyncio
32
import asyncio.subprocess
43
import sys
54

tox.ini

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ skip_install = true
1212
basepython = python3.9
1313
extras = tests
1414
deps =
15-
flake8
16-
black
15+
pre-commit
1716
commands =
1817
make lint
1918

0 commit comments

Comments
 (0)