From 22d91b2a6df1b3557cfc0370736d7cad4ba8bef4 Mon Sep 17 00:00:00 2001 From: Tin Tvrtkovic Date: Mon, 19 Apr 2021 00:57:20 +0200 Subject: [PATCH] Black linting --- Makefile | 6 ++- README.rst | 2 + pytest_asyncio/plugin.py | 2 +- setup.cfg | 3 ++ .../test_async_fixtures_scope.py | 5 ++- .../test_async_fixtures_with_finalizer.py | 5 +++ .../async_fixtures/test_async_gen_fixtures.py | 3 +- .../async_fixtures/test_coroutine_fixtures.py | 4 +- tests/async_fixtures/test_nested.py | 11 ++--- tests/conftest.py | 3 +- tests/markers/test_class_marker.py | 2 + tests/markers/test_module_marker.py | 2 + tests/multiloop/conftest.py | 1 + tests/test_simple.py | 43 ++++++++----------- tests/test_subprocess.py | 11 ++--- tox.ini | 10 +++++ 16 files changed, 71 insertions(+), 42 deletions(-) diff --git a/Makefile b/Makefile index e22e0449..425015de 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: clean clean-build clean-pyc clean-test +.PHONY: clean clean-build clean-pyc clean-test lint clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts @@ -19,3 +19,7 @@ clean-test: ## remove test and coverage artifacts rm -fr .tox/ rm -f .coverage rm -fr htmlcov/ + +lint: ## check style with flake8 + flake8 pytest_asyncio tests + black --check --verbose pytest_asyncio tests diff --git a/README.rst b/README.rst index 0862705a..ee5119f0 100644 --- a/README.rst +++ b/README.rst @@ -10,6 +10,8 @@ pytest-asyncio: pytest support for asyncio .. image:: https://img.shields.io/pypi/pyversions/pytest-asyncio.svg :target: https://github.com/pytest-dev/pytest-asyncio :alt: Supported Python versions +.. image:: https://img.shields.io/badge/code%20style-black-000000.svg + :target: https://github.com/ambv/black pytest-asyncio is an Apache2 licensed library, written in Python, for testing asyncio code with pytest. diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 61101e85..80cce291 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -62,7 +62,7 @@ def __init__(self, fixturedef): def add(self, name): """Add fixture name to fixturedef - and record in to_strip list (If not previously included)""" + and record in to_strip list (If not previously included)""" if name in self.fixturedef.argnames: return self.fixturedef.argnames += (name,) diff --git a/setup.cfg b/setup.cfg index 4a0e3005..01610865 100644 --- a/setup.cfg +++ b/setup.cfg @@ -12,3 +12,6 @@ filterwarnings = error [metadata] # ensure LICENSE is included in wheel metadata license_file = LICENSE + +[flake8] +ignore = E203, E501, W503 diff --git a/tests/async_fixtures/test_async_fixtures_scope.py b/tests/async_fixtures/test_async_fixtures_scope.py index 0d8682cc..b150f8a8 100644 --- a/tests/async_fixtures/test_async_fixtures_scope.py +++ b/tests/async_fixtures/test_async_fixtures_scope.py @@ -3,16 +3,17 @@ module-scoped too. """ import asyncio + import pytest -@pytest.fixture(scope='module') +@pytest.fixture(scope="module") def event_loop(): """A module-scoped event loop.""" return asyncio.new_event_loop() -@pytest.fixture(scope='module') +@pytest.fixture(scope="module") async def async_fixture(): await asyncio.sleep(0.1) return 1 diff --git a/tests/async_fixtures/test_async_fixtures_with_finalizer.py b/tests/async_fixtures/test_async_fixtures_with_finalizer.py index 44b5bbe4..c602d908 100644 --- a/tests/async_fixtures/test_async_fixtures_with_finalizer.py +++ b/tests/async_fixtures/test_async_fixtures_with_finalizer.py @@ -1,5 +1,6 @@ import asyncio import functools + import pytest @@ -8,11 +9,13 @@ async def test_module_with_event_loop_finalizer(port_with_event_loop_finalizer): await asyncio.sleep(0.01) assert port_with_event_loop_finalizer + @pytest.mark.asyncio async def test_module_with_get_event_loop_finalizer(port_with_get_event_loop_finalizer): await asyncio.sleep(0.01) assert port_with_get_event_loop_finalizer + @pytest.fixture(scope="module") def event_loop(): """Change event_loop fixture to module level.""" @@ -29,6 +32,7 @@ async def port_afinalizer(): # await task using loop provided by event_loop fixture # RuntimeError is raised if task is created on a different loop await finalizer + event_loop.run_until_complete(port_afinalizer()) worker = asyncio.ensure_future(asyncio.sleep(0.2)) @@ -43,6 +47,7 @@ async def port_afinalizer(): # await task using loop provided by asyncio.get_event_loop() # RuntimeError is raised if task is created on a different loop await finalizer + asyncio.get_event_loop().run_until_complete(port_afinalizer()) worker = asyncio.ensure_future(asyncio.sleep(0.2)) diff --git a/tests/async_fixtures/test_async_gen_fixtures.py b/tests/async_fixtures/test_async_gen_fixtures.py index 81b21949..0bea7458 100644 --- a/tests/async_fixtures/test_async_gen_fixtures.py +++ b/tests/async_fixtures/test_async_gen_fixtures.py @@ -1,4 +1,3 @@ -import asyncio import unittest.mock import pytest @@ -8,7 +7,7 @@ RETVAL = object() -@pytest.fixture(scope='module') +@pytest.fixture(scope="module") def mock(): return unittest.mock.Mock(return_value=RETVAL) diff --git a/tests/async_fixtures/test_coroutine_fixtures.py b/tests/async_fixtures/test_coroutine_fixtures.py index 77f203c9..65dabab5 100644 --- a/tests/async_fixtures/test_coroutine_fixtures.py +++ b/tests/async_fixtures/test_coroutine_fixtures.py @@ -7,7 +7,9 @@ END = object() RETVAL = object() -pytestmark = pytest.mark.skip(reason='@asyncio.coroutine fixtures are not supported yet') +pytestmark = pytest.mark.skip( + reason="@asyncio.coroutine fixtures are not supported yet" +) @pytest.fixture diff --git a/tests/async_fixtures/test_nested.py b/tests/async_fixtures/test_nested.py index 86e45b6b..e81e7824 100644 --- a/tests/async_fixtures/test_nested.py +++ b/tests/async_fixtures/test_nested.py @@ -1,25 +1,26 @@ import asyncio + import pytest @pytest.fixture() async def async_inner_fixture(): await asyncio.sleep(0.01) - print('inner start') + print("inner start") yield True - print('inner stop') + print("inner stop") @pytest.fixture() async def async_fixture_outer(async_inner_fixture, event_loop): await asyncio.sleep(0.01) - print('outer start') + print("outer start") assert async_inner_fixture is True yield True - print('outer stop') + print("outer stop") @pytest.mark.asyncio async def test_async_fixture(async_fixture_outer): assert async_fixture_outer is True - print('test_async_fixture') + print("test_async_fixture") diff --git a/tests/conftest.py b/tests/conftest.py index 9e046123..03fd33f2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -22,8 +22,9 @@ async def just_a_sleep(): assert counter == 2 -@pytest.fixture(scope='session', name='factory_involving_factories') +@pytest.fixture(scope="session", name="factory_involving_factories") def factory_involving_factories_fixture(unused_tcp_port_factory): def factory(): return unused_tcp_port_factory() + return factory diff --git a/tests/markers/test_class_marker.py b/tests/markers/test_class_marker.py index df137e7c..d46c3af7 100644 --- a/tests/markers/test_class_marker.py +++ b/tests/markers/test_class_marker.py @@ -1,5 +1,6 @@ """Test if pytestmark works when defined on a class.""" import asyncio + import pytest @@ -14,6 +15,7 @@ async def inc(): nonlocal counter counter += 1 await asyncio.sleep(0) + await asyncio.ensure_future(inc()) assert counter == 2 diff --git a/tests/markers/test_module_marker.py b/tests/markers/test_module_marker.py index c5ce0b04..2f69dbc9 100644 --- a/tests/markers/test_module_marker.py +++ b/tests/markers/test_module_marker.py @@ -24,10 +24,12 @@ async def inc(): async def test_is_asyncio(event_loop, sample_fixture): assert asyncio.get_event_loop() counter = 1 + async def inc(): nonlocal counter counter += 1 await asyncio.sleep(0) + await asyncio.ensure_future(inc()) assert counter == 2 diff --git a/tests/multiloop/conftest.py b/tests/multiloop/conftest.py index 1b62a1d0..9c74a509 100644 --- a/tests/multiloop/conftest.py +++ b/tests/multiloop/conftest.py @@ -5,6 +5,7 @@ class CustomSelectorLoop(asyncio.SelectorEventLoop): """A subclass with no overrides, just to test for presence.""" + pass diff --git a/tests/test_simple.py b/tests/test_simple.py index 62b4036e..b5b57ed5 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -1,20 +1,20 @@ """Quick'n'dirty unit tests for provided fixtures and markers.""" import asyncio -import pytest +import pytest import pytest_asyncio.plugin async def async_coro(): await asyncio.sleep(0) - return 'ok' + return "ok" def test_event_loop_fixture(event_loop): """Test the injection of the event_loop fixture.""" assert event_loop ret = event_loop.run_until_complete(async_coro()) - assert ret == 'ok' + assert ret == "ok" @pytest.mark.asyncio @@ -23,7 +23,7 @@ async def test_asyncio_marker(): await asyncio.sleep(0) -@pytest.mark.xfail(reason='need a failure', strict=True) +@pytest.mark.xfail(reason="need a failure", strict=True) @pytest.mark.asyncio def test_asyncio_marker_fail(): assert False @@ -42,12 +42,10 @@ async def test_unused_port_fixture(unused_tcp_port, event_loop): async def closer(_, writer): writer.close() - server1 = await asyncio.start_server(closer, host='localhost', - port=unused_tcp_port) + server1 = await asyncio.start_server(closer, host="localhost", port=unused_tcp_port) with pytest.raises(IOError): - await asyncio.start_server(closer, host='localhost', - port=unused_tcp_port) + await asyncio.start_server(closer, host="localhost", port=unused_tcp_port) server1.close() await server1.wait_closed() @@ -60,20 +58,19 @@ async def test_unused_port_factory_fixture(unused_tcp_port_factory, event_loop): async def closer(_, writer): writer.close() - port1, port2, port3 = (unused_tcp_port_factory(), unused_tcp_port_factory(), - unused_tcp_port_factory()) + port1, port2, port3 = ( + unused_tcp_port_factory(), + unused_tcp_port_factory(), + unused_tcp_port_factory(), + ) - server1 = await asyncio.start_server(closer, host='localhost', - port=port1) - server2 = await asyncio.start_server(closer, host='localhost', - port=port2) - server3 = await asyncio.start_server(closer, host='localhost', - port=port3) + server1 = await asyncio.start_server(closer, host="localhost", port=port1) + server2 = await asyncio.start_server(closer, host="localhost", port=port2) + server3 = await asyncio.start_server(closer, host="localhost", port=port3) for port in port1, port2, port3: with pytest.raises(IOError): - await asyncio.start_server(closer, host='localhost', - port=port) + await asyncio.start_server(closer, host="localhost", port=port) server1.close() await server1.wait_closed() @@ -96,8 +93,7 @@ def mock_unused_tcp_port(): else: return 10000 + counter - monkeypatch.setattr(pytest_asyncio.plugin, '_unused_tcp_port', - mock_unused_tcp_port) + monkeypatch.setattr(pytest_asyncio.plugin, "_unused_tcp_port", mock_unused_tcp_port) assert unused_tcp_port_factory() == 10000 assert unused_tcp_port_factory() > 10000 @@ -110,7 +106,7 @@ class Test: async def test_asyncio_marker_method(self, event_loop): """Test the asyncio pytest marker in a Test class.""" ret = await async_coro() - assert ret == 'ok' + assert ret == "ok" class TestUnexistingLoop: @@ -125,7 +121,7 @@ def remove_loop(self): async def test_asyncio_marker_without_loop(self, remove_loop): """Test the asyncio pytest marker in a Test class.""" ret = await async_coro() - assert ret == 'ok' + assert ret == "ok" class TestEventLoopStartedBeforeFixtures: @@ -150,7 +146,6 @@ async def test_event_loop_before_fixture(self, event_loop, loop): assert await loop.run_in_executor(None, self.foo) == 1 - @pytest.mark.asyncio async def test_no_warning_on_skip(): pytest.skip("Test a skip error inside asyncio") @@ -158,4 +153,4 @@ async def test_no_warning_on_skip(): def test_async_close_loop(event_loop): event_loop.close() - return 'ok' + return "ok" diff --git a/tests/test_subprocess.py b/tests/test_subprocess.py index 069c6c22..88ea29ab 100644 --- a/tests/test_subprocess.py +++ b/tests/test_subprocess.py @@ -1,12 +1,11 @@ """Tests for using subprocesses in tests.""" -import sys import asyncio import asyncio.subprocess +import sys import pytest - -if sys.platform == 'win32': +if sys.platform == "win32": # The default asyncio event loop implementation on Windows does not # support subprocesses. Subprocesses are available for Windows if a # ProactorEventLoop is used. @@ -21,7 +20,8 @@ def event_loop(): async def test_subprocess(event_loop): """Starting a subprocess should be possible.""" proc = await asyncio.subprocess.create_subprocess_exec( - sys.executable, '--version', stdout=asyncio.subprocess.PIPE) + sys.executable, "--version", stdout=asyncio.subprocess.PIPE + ) await proc.communicate() @@ -29,5 +29,6 @@ async def test_subprocess(event_loop): async def test_subprocess_forbid(event_loop): """Starting a subprocess should be possible.""" proc = await asyncio.subprocess.create_subprocess_exec( - sys.executable, '--version', stdout=asyncio.subprocess.PIPE) + sys.executable, "--version", stdout=asyncio.subprocess.PIPE + ) await proc.communicate() diff --git a/tox.ini b/tox.ini index c61bd4c0..5b057d75 100644 --- a/tox.ini +++ b/tox.ini @@ -7,6 +7,16 @@ skip_missing_interpreters = true extras = testing commands = coverage run -m pytest {posargs} +[testenv:lint] +skip_install = true +basepython = python3.9 +extras = tests +deps = + flake8 + black +commands = + make lint + [testenv:coverage-report] deps = coverage skip_install = true