Skip to content

Commit 3a3900f

Browse files
committed
docstrings and... :[
1 parent 150764c commit 3a3900f

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

pytest_twisted.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,22 @@ def decorator(f):
163163

164164

165165
def pytest_fixture_setup(fixturedef, request):
166+
"""Interface pytest to async setup for async and async yield fixtures."""
167+
# TODO: what about inlineCallbacks fixtures?
166168
maybe_mark = getattr(fixturedef.func, _mark_attribute_name, None)
167169
if maybe_mark is None:
168170
return None
169171

170172
mark = maybe_mark
171173

172-
run_inline_callbacks(_pytest_fixture_setup, fixturedef, request, mark)
174+
run_inline_callbacks(_async_pytest_fixture_setup, fixturedef, request, mark)
173175

174176
return True
175177

176178

177179
@defer.inlineCallbacks
178-
def _pytest_fixture_setup(fixturedef, request, mark):
180+
def _async_pytest_fixture_setup(fixturedef, request, mark):
181+
"""Setup async and async yield fixtures."""
179182
fixture_function = fixturedef.func
180183

181184
kwargs = {
@@ -204,6 +207,7 @@ def _pytest_fixture_setup(fixturedef, request, mark):
204207

205208
# TODO: but don't we want to do the finalizer? not wait until post it?
206209
def pytest_fixture_post_finalizer(fixturedef, request):
210+
"""Collect async yield fixture teardown requests for later handling."""
207211
maybe_coroutine = _tracking.async_yield_fixture_cache.pop(
208212
request.param_index,
209213
None,
@@ -221,6 +225,7 @@ def pytest_fixture_post_finalizer(fixturedef, request):
221225

222226
@defer.inlineCallbacks
223227
def tear_it_down(deferred):
228+
"""Tear down a specific async yield fixture."""
224229
try:
225230
yield deferred
226231
except StopAsyncIteration:
@@ -230,6 +235,7 @@ def tear_it_down(deferred):
230235
else:
231236
e = None
232237

238+
# TODO: six.raise_from()
233239
raise AsyncGeneratorFixtureDidNotStopError.from_generator(
234240
generator=deferred,
235241
)
@@ -240,6 +246,7 @@ def tear_it_down(deferred):
240246

241247

242248
def run_inline_callbacks(f, *args):
249+
"""Interface into Twisted greenlet to run and wait for deferred."""
243250
if _instances.gr_twisted is not None:
244251
if _instances.gr_twisted.dead:
245252
raise RuntimeError("twisted reactor has stopped")
@@ -258,15 +265,24 @@ def in_reactor(d, f, *args):
258265

259266
@pytest.hookimpl(hookwrapper=True)
260267
def pytest_runtest_teardown(item):
268+
"""Tear down collected async yield fixtures."""
261269
yield
262270

263271
while len(_tracking.to_be_torn_down) > 0:
264272
deferred = _tracking.to_be_torn_down.pop(0)
265273
run_inline_callbacks(tear_it_down, deferred)
266274

267275

276+
def pytest_pyfunc_call(pyfuncitem):
277+
"""Interface to async test call handler."""
278+
# TODO: only handle 'our' tests? what is the point of handling others?
279+
run_inline_callbacks(_async_pytest_pyfunc_call, pyfuncitem)
280+
return True
281+
282+
268283
@defer.inlineCallbacks
269-
def _pytest_pyfunc_call(pyfuncitem):
284+
def _async_pytest_pyfunc_call(pyfuncitem):
285+
"""Run test function."""
270286
kwargs = {
271287
name: value
272288
for name, value in pyfuncitem.funcargs.items()
@@ -276,17 +292,13 @@ def _pytest_pyfunc_call(pyfuncitem):
276292
defer.returnValue(result)
277293

278294

279-
def pytest_pyfunc_call(pyfuncitem):
280-
run_inline_callbacks(_pytest_pyfunc_call, pyfuncitem)
281-
return True
282-
283-
284295
@pytest.fixture(scope="session", autouse=True)
285296
def twisted_greenlet():
286297
return _instances.gr_twisted
287298

288299

289300
def init_default_reactor():
301+
"""Install the default Twisted reactor."""
290302
import twisted.internet.default
291303

292304
module = inspect.getmodule(twisted.internet.default.install)
@@ -302,6 +314,7 @@ def init_default_reactor():
302314

303315

304316
def init_qt5_reactor():
317+
"""Install the qt5reactor... reactor."""
305318
import qt5reactor
306319

307320
_install_reactor(
@@ -310,6 +323,7 @@ def init_qt5_reactor():
310323

311324

312325
def init_asyncio_reactor():
326+
"""Install the Twisted reactor for asyncio."""
313327
from twisted.internet import asyncioreactor
314328

315329
_install_reactor(
@@ -326,6 +340,7 @@ def init_asyncio_reactor():
326340

327341

328342
def _install_reactor(reactor_installer, reactor_type):
343+
"""Install the specified reactor and create the greenlet."""
329344
try:
330345
reactor_installer()
331346
except error.ReactorAlreadyInstalledError:
@@ -345,6 +360,7 @@ def _install_reactor(reactor_installer, reactor_type):
345360

346361

347362
def pytest_addoption(parser):
363+
"""Add options into the pytest CLI."""
348364
group = parser.getgroup("twisted")
349365
group.addoption(
350366
"--reactor",
@@ -354,6 +370,7 @@ def pytest_addoption(parser):
354370

355371

356372
def pytest_configure(config):
373+
"""Identify and install chosen reactor."""
357374
pytest.inlineCallbacks = _deprecate(
358375
deprecated='pytest.inlineCallbacks',
359376
recommended='pytest_twisted.inlineCallbacks',
@@ -367,10 +384,12 @@ def pytest_configure(config):
367384

368385

369386
def pytest_unconfigure(config):
387+
"""Stop the reactor greenlet."""
370388
stop_twisted_greenlet()
371389

372390

373391
def _use_asyncio_selector_if_required(config):
392+
"""Set asyncio selector event loop policy if needed."""
374393
# https://twistedmatrix.com/trac/ticket/9766
375394
# https://github.com/pytest-dev/pytest-twisted/issues/80
376395

0 commit comments

Comments
 (0)