Skip to content

Commit

Permalink
Merge pull request #184 from pytest-dev/less_return_value
Browse files Browse the repository at this point in the history
become a full package... and `return` not `defer.returnValue()`
  • Loading branch information
altendky authored Aug 16, 2024
2 parents 23ba220 + cf1bd0c commit dc67544
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 59 deletions.
6 changes: 6 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[flake8]
ignore =
N802

per-file-ignores =
src/pytest_twisted/__init__.py: F401
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from setuptools import setup
import setuptools

with open("README.rst") as f:
long_description = f.read()

setup(
setuptools.setup(
name="pytest-twisted",
version="1.14.2",
description="A twisted plugin for pytest.",
Expand All @@ -12,7 +12,8 @@
author="Ralf Schmitt, Kyle Altendorf, Victor Titor",
author_email="[email protected]",
url="https://github.com/pytest-dev/pytest-twisted",
py_modules=["pytest_twisted"],
packages=setuptools.find_packages('src'),
package_dir={'': 'src'},
python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*',
install_requires=["greenlet", "pytest>=2.3", "decorator"],
extras_require={
Expand Down
60 changes: 8 additions & 52 deletions pytest_twisted.py → src/pytest_twisted/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
from twisted.internet.threads import blockingCallFromThread
from twisted.python import failure

if sys.version_info[0] == 3:
from pytest_twisted.three import (
_async_pytest_fixture_setup,
_async_pytest_pyfunc_call,
)
elif sys.version_info[0] == 2:
from pytest_twisted.two import _async_pytest_pyfunc_call


class WrongReactorAlreadyInstalledError(Exception):
pass
Expand Down Expand Up @@ -308,36 +316,6 @@ def finalizer():
return finalizer


@defer.inlineCallbacks
def _async_pytest_fixture_setup(fixturedef, request, mark):
"""Setup an async or async yield fixture."""
fixture_function = fixturedef.func

kwargs = {
name: request.getfixturevalue(name)
for name in fixturedef.argnames
}

if mark == 'async_fixture':
arg_value = yield defer.ensureDeferred(
fixture_function(**kwargs)
)
elif mark == 'async_yield_fixture':
coroutine = fixture_function(**kwargs)

request.addfinalizer(
_create_async_yield_fixture_finalizer(coroutine=coroutine),
)

arg_value = yield defer.ensureDeferred(coroutine.__anext__())
else:
raise UnrecognizedCoroutineMarkError.from_mark(mark=mark)

fixturedef.cached_result = (arg_value, fixturedef.cache_key(request), None)

defer.returnValue(arg_value)


@defer.inlineCallbacks
def _tear_it_down(deferred):
"""Tear down a specific async yield fixture."""
Expand Down Expand Up @@ -402,28 +380,6 @@ def inner_test(**kwargs):
return result


@defer.inlineCallbacks
def _async_pytest_pyfunc_call(pyfuncitem, f, kwargs):
"""Run test function."""
fixture_kwargs = {
name: value
for name, value in pyfuncitem.funcargs.items()
if name in pyfuncitem._fixtureinfo.argnames
}
kwargs.update(fixture_kwargs)

maybe_mark = _get_mark(f)
if maybe_mark == 'async_test':
result = yield defer.ensureDeferred(f(**kwargs))
elif maybe_mark == 'inline_callbacks_test':
result = yield f(**kwargs)
else:
# TODO: maybe deprecate this
result = yield f(**kwargs)

defer.returnValue(result)


@pytest.fixture(scope="session", autouse=True)
def twisted_greenlet():
"""Provide the twisted greenlet in fixture form."""
Expand Down
60 changes: 60 additions & 0 deletions src/pytest_twisted/three.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from twisted.internet import defer


@defer.inlineCallbacks
def _async_pytest_fixture_setup(fixturedef, request, mark):
"""Setup an async or async yield fixture."""
from pytest_twisted import (
UnrecognizedCoroutineMarkError,
_create_async_yield_fixture_finalizer,
)

fixture_function = fixturedef.func

kwargs = {
name: request.getfixturevalue(name)
for name in fixturedef.argnames
}

if mark == 'async_fixture':
arg_value = yield defer.ensureDeferred(
fixture_function(**kwargs)
)
elif mark == 'async_yield_fixture':
coroutine = fixture_function(**kwargs)

request.addfinalizer(
_create_async_yield_fixture_finalizer(coroutine=coroutine),
)

arg_value = yield defer.ensureDeferred(coroutine.__anext__())
else:
raise UnrecognizedCoroutineMarkError.from_mark(mark=mark)

fixturedef.cached_result = (arg_value, fixturedef.cache_key(request), None)

return arg_value


@defer.inlineCallbacks
def _async_pytest_pyfunc_call(pyfuncitem, f, kwargs):
"""Run test function."""
from pytest_twisted import _get_mark

fixture_kwargs = {
name: value
for name, value in pyfuncitem.funcargs.items()
if name in pyfuncitem._fixtureinfo.argnames
}
kwargs.update(fixture_kwargs)

maybe_mark = _get_mark(f)
if maybe_mark == 'async_test':
result = yield defer.ensureDeferred(f(**kwargs))
elif maybe_mark == 'inline_callbacks_test':
result = yield f(**kwargs)
else:
# TODO: maybe deprecate this
result = yield f(**kwargs)

return result
25 changes: 25 additions & 0 deletions src/pytest_twisted/two.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from twisted.internet import defer


@defer.inlineCallbacks
def _async_pytest_pyfunc_call(pyfuncitem, f, kwargs):
"""Run test function."""
from pytest_twisted import _get_mark

fixture_kwargs = {
name: value
for name, value in pyfuncitem.funcargs.items()
if name in pyfuncitem._fixtureinfo.argnames
}
kwargs.update(fixture_kwargs)

maybe_mark = _get_mark(f)
if maybe_mark == 'async_test':
result = yield defer.ensureDeferred(f(**kwargs))
elif maybe_mark == 'inline_callbacks_test':
result = yield f(**kwargs)
else:
# TODO: maybe deprecate this
result = yield f(**kwargs)

defer.returnValue(result)
5 changes: 1 addition & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,4 @@ download=true

[testenv:linting]
deps=flake8
commands=flake8 setup.py pytest_twisted.py testing

[flake8]
ignore=N802
commands=flake8 setup.py src/pytest_twisted testing

0 comments on commit dc67544

Please sign in to comment.