Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 typing import Generator

import pytest


@pytest.fixture
def sample_fixture_yield() -> Generator[int, None, None]:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could also include an example of iterator for this too? If this is not already clear from this perhaps... or at least just mention it maybe

yield 38


def test_sample_fixture_yield(sample_fixture_yield: int) -> None:
assert sample_fixture_yield == 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