From 48d5ab41189cd47f4ec12f7c8c71308fd1e850a7 Mon Sep 17 00:00:00 2001 From: phalberg Date: Sun, 4 Oct 2026 12:20:05 +0200 Subject: [PATCH 1/3] docs: clarify typing for yield fixtures --- doc/en/explanation/types.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/doc/en/explanation/types.rst b/doc/en/explanation/types.rst index 827a2bf02b6..d34640e5b27 100644 --- a/doc/en/explanation/types.rst +++ b/doc/en/explanation/types.rst @@ -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]: + 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 ` for more information about +``yield`` fixtures and teardown. Conclusion ---------- From b2fabbc9d5f178570d22b7e7ff33163bf6961884 Mon Sep 17 00:00:00 2001 From: phalberg Date: Sun, 11 Oct 2026 14:13:20 +0200 Subject: [PATCH 2/3] chore: added changelog --- changelog/typing-yield-fixtures.doc.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/typing-yield-fixtures.doc.rst diff --git a/changelog/typing-yield-fixtures.doc.rst b/changelog/typing-yield-fixtures.doc.rst new file mode 100644 index 00000000000..e3559287d36 --- /dev/null +++ b/changelog/typing-yield-fixtures.doc.rst @@ -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. From edcaf22324d479308f6b856ba2dec98479a4c689 Mon Sep 17 00:00:00 2001 From: phalberg Date: Sun, 11 Oct 2026 14:20:33 +0200 Subject: [PATCH 3/3] chore: fixup changelog to be after specified format --- changelog/{typing-yield-fixtures.doc.rst => 15127.doc.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelog/{typing-yield-fixtures.doc.rst => 15127.doc.rst} (100%) diff --git a/changelog/typing-yield-fixtures.doc.rst b/changelog/15127.doc.rst similarity index 100% rename from changelog/typing-yield-fixtures.doc.rst rename to changelog/15127.doc.rst