Skip to content

docs: clarify typing for yield fixtures - #15127

Open
phalberg wants to merge 4 commits into
pytest-dev:mainfrom
phalberg:docs/typing-yield-fixtures
Open

phalberg wants to merge 4 commits into
pytest-dev:mainfrom
phalberg:docs/typing-yield-fixtures

Conversation

@phalberg

@phalberg phalberg commented Oct 4, 2026

Copy link
Copy Markdown

Description

While working with adding types to a codebase of mine and using mypy it was not so trivial (for me at least) that fixtures that are of Generator types, are not to be type annotated like Generators but rather their return types when used in other fixtures. Based on: https://docs.pytest.org/en/stable/how-to/fixtures.html#teardown-cleanup-aka-fixture-finalization, it became more obvious why this is the case, but it was not so easy to find this documentation, and I feel that the typing docs can also have a note for it.

Changes

Added a typing section about usage of yield based fixtures for other fixtures.

Relevant issue

No relevant issue, as I see this as small docs "enhancement".



@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

@phalberg
phalberg force-pushed the docs/typing-yield-fixtures branch from 0e4e81d to b2fabbc Compare October 11, 2026 12:16
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 11, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant