Skip to content

Commit

Permalink
Black linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche committed Apr 18, 2021
1 parent 5c47b78 commit 22d91b2
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 42 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,)
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ filterwarnings = error
[metadata]
# ensure LICENSE is included in wheel metadata
license_file = LICENSE

[flake8]
ignore = E203, E501, W503
5 changes: 3 additions & 2 deletions tests/async_fixtures/test_async_fixtures_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/async_fixtures/test_async_fixtures_with_finalizer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import functools

import pytest


Expand All @@ -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."""
Expand All @@ -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))
Expand All @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions tests/async_fixtures/test_async_gen_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import unittest.mock

import pytest
Expand All @@ -8,7 +7,7 @@
RETVAL = object()


@pytest.fixture(scope='module')
@pytest.fixture(scope="module")
def mock():
return unittest.mock.Mock(return_value=RETVAL)

Expand Down
4 changes: 3 additions & 1 deletion tests/async_fixtures/test_coroutine_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions tests/async_fixtures/test_nested.py
Original file line number Diff line number Diff line change
@@ -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")
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions tests/markers/test_class_marker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test if pytestmark works when defined on a class."""
import asyncio

import pytest


Expand All @@ -14,6 +15,7 @@ async def inc():
nonlocal counter
counter += 1
await asyncio.sleep(0)

await asyncio.ensure_future(inc())
assert counter == 2

Expand Down
2 changes: 2 additions & 0 deletions tests/markers/test_module_marker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions tests/multiloop/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class CustomSelectorLoop(asyncio.SelectorEventLoop):
"""A subclass with no overrides, just to test for presence."""

pass


Expand Down
43 changes: 19 additions & 24 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -150,12 +146,11 @@ 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")


def test_async_close_loop(event_loop):
event_loop.close()
return 'ok'
return "ok"
11 changes: 6 additions & 5 deletions tests/test_subprocess.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -21,13 +20,15 @@ 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()


@pytest.mark.asyncio(forbid_global_loop=True)
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()
10 changes: 10 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 22d91b2

Please sign in to comment.