Skip to content

Commit 41d12cd

Browse files
authored
docs: clarify typing for yield fixtures
1 parent b9022ba commit 41d12cd

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

‎changelog/15127.doc.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added guidance on typing yield fixtures, including annotating fixture functions with their generator type and fixture consumers with the yielded value type.

‎doc/en/explanation/types.rst‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ The same logic applies when typing fixture functions which receive other fixture
8181
def mock_env_user(monkeypatch: pytest.MonkeyPatch) -> None:
8282
monkeypatch.setenv("USER", "TestingUser")
8383
84+
For a fixture that uses ``yield``, annotate the fixture function with the
85+
type of its generator. Annotate a test or another fixture that requests it
86+
with the type of the value yielded:
87+
88+
.. code-block:: python
89+
90+
from collections.abc import Generator
91+
92+
import pytest
93+
94+
95+
@pytest.fixture
96+
def sample_yield_fixture() -> Generator[int]:
97+
yield 38
98+
99+
100+
def test_sample_yield_fixture(sample_yield_fixture: int) -> None:
101+
assert sample_yield_fixture == 38
102+
103+
The test receives the yielded ``int``, not the generator object. See :ref:`How to use fixtures <how-to-fixtures>` for more information about
104+
``yield`` fixtures and teardown.
84105

85106
Conclusion
86107
----------

0 commit comments

Comments
 (0)