Skip to content
1 change: 1 addition & 0 deletions changelog/15127.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added guidance on typing yield fixtures, including annotating fixture functions with their generator type and fixture consumers with the yielded value type.
21 changes: 21 additions & 0 deletions doc/en/explanation/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ The same logic applies when typing fixture functions which receive other fixture
def mock_env_user(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("USER", "TestingUser")
For a fixture that uses ``yield``, annotate the fixture function with the
type of its generator. Annotate a test or another fixture that requests it
with the type of the value yielded:

.. code-block:: python
from collections.abc import Generator
import pytest
@pytest.fixture
def sample_yield_fixture() -> Generator[int]:
yield 38
def test_sample_yield_fixture(sample_yield_fixture: int) -> None:
assert sample_yield_fixture == 38
The test receives the yielded ``int``, not the generator object. See :ref:`How to use fixtures <how-to-fixtures>` for more information about
``yield`` fixtures and teardown.

Conclusion
----------
Expand Down
Loading