From 48d5ab41189cd47f4ec12f7c8c71308fd1e850a7 Mon Sep 17 00:00:00 2001 From: phalberg Date: Sun, 4 Oct 2026 12:20:05 +0200 Subject: [PATCH 1/6] 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/6] 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/6] 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 From b108d8cc9eedd8f0fca5bc15ba8e67d53dfdaa2c Mon Sep 17 00:00:00 2001 From: Philipp Stahlberg <131917964+phalberg@users.noreply.github.com> Date: Mon, 26 Oct 2026 08:27:31 +0100 Subject: [PATCH 4/6] Update doc/en/explanation/types.rst use collections instead of typing Co-authored-by: Ran Benita --- doc/en/explanation/types.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/en/explanation/types.rst b/doc/en/explanation/types.rst index d34640e5b27..ef20172b43e 100644 --- a/doc/en/explanation/types.rst +++ b/doc/en/explanation/types.rst @@ -87,7 +87,7 @@ with the type of the value yielded: .. code-block:: python - from typing import Generator + from collections.abc import Generator import pytest From dff9bdc52d6f431576e7f3cb0657aed139042e53 Mon Sep 17 00:00:00 2001 From: Philipp Stahlberg <131917964+phalberg@users.noreply.github.com> Date: Mon, 26 Oct 2026 08:28:42 +0100 Subject: [PATCH 5/6] Update doc/en/explanation/types.rst use generator default value's and adjust naming Co-authored-by: Ran Benita --- doc/en/explanation/types.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/en/explanation/types.rst b/doc/en/explanation/types.rst index ef20172b43e..7a25ea88d43 100644 --- a/doc/en/explanation/types.rst +++ b/doc/en/explanation/types.rst @@ -93,7 +93,7 @@ with the type of the value yielded: @pytest.fixture - def sample_fixture_yield() -> Generator[int, None, None]: + def sample_yield_fixture() -> Generator[int]: yield 38 From a6cd592ba9d7a9779ae8579a28f08944a31b694f Mon Sep 17 00:00:00 2001 From: Philipp Stahlberg <131917964+phalberg@users.noreply.github.com> Date: Mon, 26 Oct 2026 08:30:42 +0100 Subject: [PATCH 6/6] Update doc/en/explanation/types.rst update naming of test Co-authored-by: Ran Benita --- doc/en/explanation/types.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/en/explanation/types.rst b/doc/en/explanation/types.rst index 7a25ea88d43..954fca22fc3 100644 --- a/doc/en/explanation/types.rst +++ b/doc/en/explanation/types.rst @@ -97,8 +97,8 @@ with the type of the value yielded: yield 38 - def test_sample_fixture_yield(sample_fixture_yield: int) -> None: - assert sample_fixture_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 ` for more information about ``yield`` fixtures and teardown.