Skip to content

Commit 3df0ad2

Browse files
Merge branch 'main' into fix-blocked-conftest
2 parents 642336d + 087ac04 commit 3df0ad2

File tree

5 files changed

+250
-202
lines changed

5 files changed

+250
-202
lines changed

CONTRIBUTING.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Look through the `GitHub issues for bugs <https://github.com/pytest-dev/pytest/l
4949
See also the `"good first issue" issues <https://github.com/pytest-dev/pytest/labels/good%20first%20issue>`_
5050
that are friendly to new contributors.
5151

52-
:ref:`Talk <contact>` to developers to find out how you can fix specific bugs. To indicate that you are going
52+
`Talk to developers <https://docs.pytest.org/en/stable/contact.html>`_ to find out how you can fix specific bugs. To indicate that you are going
5353
to work on a particular issue, add a comment to that effect on the specific issue.
5454

5555
Don't forget to check the issue trackers of your favourite plugins, too!
@@ -61,7 +61,7 @@ Implement features
6161

6262
Look through the `GitHub issues for enhancements <https://github.com/pytest-dev/pytest/labels/type:%20enhancement>`_.
6363

64-
:ref:`Talk <contact>` to developers to find out how you can implement specific
64+
`Talk to developers <https://docs.pytest.org/en/stable/contact.html>`_ to find out how you can implement specific
6565
features.
6666

6767
Write documentation

doc/en/deprecations.rst

Lines changed: 72 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -148,76 +148,6 @@ Simply remove the ``__init__.py`` file entirely.
148148
Python 3.3+ natively supports namespace packages without ``__init__.py``.
149149

150150

151-
.. _sync-test-async-fixture:
152-
153-
sync test depending on async fixture
154-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
155-
156-
.. deprecated:: 8.4
157-
158-
Pytest has for a long time given an error when encountering an asynchronous test function, prompting the user to install
159-
a plugin that can handle it. It has not given any errors if you have an asynchronous fixture that's depended on by a
160-
synchronous test. If the fixture was an async function you did get an "unawaited coroutine" warning, but for async yield fixtures you didn't even get that.
161-
This is a problem even if you do have a plugin installed for handling async tests, as they may require
162-
special decorators for async fixtures to be handled, and some may not robustly handle if a user accidentally requests an
163-
async fixture from their sync tests. Fixture values being cached can make this even more unintuitive, where everything will
164-
"work" if the fixture is first requested by an async test, and then requested by a synchronous test.
165-
166-
Unfortunately there is no 100% reliable method of identifying when a user has made a mistake, versus when they expect an
167-
unawaited object from their fixture that they will handle on their own. To suppress this warning
168-
when you in fact did intend to handle this you can wrap your async fixture in a synchronous fixture:
169-
170-
.. code-block:: python
171-
172-
import asyncio
173-
import pytest
174-
175-
176-
@pytest.fixture
177-
async def unawaited_fixture():
178-
return 1
179-
180-
181-
def test_foo(unawaited_fixture):
182-
assert 1 == asyncio.run(unawaited_fixture)
183-
184-
should be changed to
185-
186-
187-
.. code-block:: python
188-
189-
import asyncio
190-
import pytest
191-
192-
193-
@pytest.fixture
194-
def unawaited_fixture():
195-
async def inner_fixture():
196-
return 1
197-
198-
return inner_fixture()
199-
200-
201-
def test_foo(unawaited_fixture):
202-
assert 1 == asyncio.run(unawaited_fixture)
203-
204-
205-
You can also make use of `pytest_fixture_setup` to handle the coroutine/asyncgen before pytest sees it - this is the way current async pytest plugins handle it.
206-
207-
If a user has an async fixture with ``autouse=True`` in their ``conftest.py``, or in a file
208-
containing both synchronous tests and the fixture, they will receive this warning.
209-
Unless you're using a plugin that specifically handles async fixtures
210-
with synchronous tests, we strongly recommend against this practice.
211-
It can lead to unpredictable behavior (with larger scopes, it may appear to "work" if an async
212-
test is the first to request the fixture, due to value caching) and will generate
213-
unawaited-coroutine runtime warnings (but only for non-yield fixtures).
214-
Additionally, it creates ambiguity for other developers about whether the fixture is intended to perform
215-
setup for synchronous tests.
216-
217-
The `anyio pytest plugin <https://anyio.readthedocs.io/en/stable/testing.html>`_ supports
218-
synchronous tests with async fixtures, though certain limitations apply.
219-
220-
221151
.. _import-or-skip-import-error:
222152

223153
``pytest.importorskip`` default behavior regarding :class:`ImportError`
@@ -423,6 +353,78 @@ an appropriate period of deprecation has passed.
423353

424354
Some breaking changes which could not be deprecated are also listed.
425355

356+
.. _sync-test-async-fixture:
357+
358+
sync test depending on async fixture
359+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
360+
361+
.. deprecated:: 8.4
362+
.. versionremoved:: 9.0
363+
364+
Pytest has for a long time given an error when encountering an asynchronous test function, prompting the user to install
365+
a plugin that can handle it. It has not given any errors if you have an asynchronous fixture that's depended on by a
366+
synchronous test. If the fixture was an async function you did get an "unawaited coroutine" warning, but for async yield fixtures you didn't even get that.
367+
This is a problem even if you do have a plugin installed for handling async tests, as they may require
368+
special decorators for async fixtures to be handled, and some may not robustly handle if a user accidentally requests an
369+
async fixture from their sync tests. Fixture values being cached can make this even more unintuitive, where everything will
370+
"work" if the fixture is first requested by an async test, and then requested by a synchronous test.
371+
372+
Unfortunately there is no 100% reliable method of identifying when a user has made a mistake, versus when they expect an
373+
unawaited object from their fixture that they will handle on their own. To suppress this warning
374+
when you in fact did intend to handle this you can wrap your async fixture in a synchronous fixture:
375+
376+
.. code-block:: python
377+
378+
import asyncio
379+
import pytest
380+
381+
382+
@pytest.fixture
383+
async def unawaited_fixture():
384+
return 1
385+
386+
387+
def test_foo(unawaited_fixture):
388+
assert 1 == asyncio.run(unawaited_fixture)
389+
390+
should be changed to
391+
392+
393+
.. code-block:: python
394+
395+
import asyncio
396+
import pytest
397+
398+
399+
@pytest.fixture
400+
def unawaited_fixture():
401+
async def inner_fixture():
402+
return 1
403+
404+
return inner_fixture()
405+
406+
407+
def test_foo(unawaited_fixture):
408+
assert 1 == asyncio.run(unawaited_fixture)
409+
410+
411+
You can also make use of `pytest_fixture_setup` to handle the coroutine/asyncgen before pytest sees it - this is the way current async pytest plugins handle it.
412+
413+
If a user has an async fixture with ``autouse=True`` in their ``conftest.py``, or in a file
414+
containing both synchronous tests and the fixture, they will receive this warning.
415+
Unless you're using a plugin that specifically handles async fixtures
416+
with synchronous tests, we strongly recommend against this practice.
417+
It can lead to unpredictable behavior (with larger scopes, it may appear to "work" if an async
418+
test is the first to request the fixture, due to value caching) and will generate
419+
unawaited-coroutine runtime warnings (but only for non-yield fixtures).
420+
Additionally, it creates ambiguity for other developers about whether the fixture is intended to perform
421+
setup for synchronous tests.
422+
423+
The `anyio pytest plugin <https://anyio.readthedocs.io/en/stable/testing.html>`_ supports
424+
synchronous tests with async fixtures, though certain limitations apply.
425+
426+
427+
426428
Applying a mark to a fixture function
427429
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
428430

0 commit comments

Comments
 (0)