Repository navigation
Show return type annotations in --fixtures and --fixtures-per-test - #15162
Open
IslamElsayed wants to merge 4 commits into
Open
IslamElsayed wants to merge 4 commits into
IslamElsayed wants to merge 4 commits into
Conversation
A fixture with a return annotation is now listed as `answer -> int -- test_x.py:4`. On Python 3.14+ the annotation is read as a string with annotationlib, so names that only exist when checking types are shown instead of evaluated. On earlier versions it is formatted with inspect.formatannotation, relative to the fixture's module. Closes pytest-dev#13676
RonnyPfannschmidt
requested changes
Oct 19, 2026
RonnyPfannschmidt
left a comment
Member
There was a problem hiding this comment.
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
Thanks, this follows the plan from #13680 well. Three changes before merge:
- Yield fixtures: show the yielded type, not the wrapper.
tmp_path -> Generator[Path]should readtmp_path -> Path. StripGenerator/Iterator/Iterableand their async variants to the first type argument, but only when the fixture function is actually a generator, so a plain fixture that returns anIteratorkeeps its annotation. Please add tests for both cases. - Module prefixes: nested names such as
Optional[test_x.Answer]before 3.14 are fine as they are. No need to strip them. - Odd fixture functions: if reading or formatting the annotation fails for any reason, don't crash or silently drop it. Print a short note under the fixture saying the return type can't be shown and where the fixture is defined. Please add a test for that path.
Generated by Claude Code
…an't be shown A yield fixture annotated Generator[T, ...], Iterator[T] or Iterable[T] (or an async one) is now listed with T, so tmp_path reads tmp_path -> Path. A fixture that is not a generator keeps its annotation as written. If reading or formatting a fixture's return annotation fails, --fixtures and --fixtures-per-test print a short note under the fixture instead of failing.
Author
|
Thanks @RonnyPfannschmidt , all three are done:
Changes are in 21a6d40 and a48e007. The Read the Docs failure is a pluggy cross-reference warning ( |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #13676.
Fixtures with a return annotation now show it in
--fixturesand--fixtures-per-test:This picks up where #13680 left off (thanks @kianelbo), following what was agreed there:
annotationlib.get_annotations(func, format=Format.STRING), as @The-Compiler suggested. Nothing is evaluated, which covers @RonnyPfannschmidt's point about unresolvable annotations: a name imported only underTYPE_CHECKINGis shown, not raised.inspect.formatannotation, with no regex post-processing, per @nicoddemus. It passesbase_module=func.__module__, so a class from the fixture's own module shows asAnswerrather thantest_x.Answer. String annotations (from __future__ import annotations) are shown as written.-> Noneis shown asNone, and fixtures without a return annotation are unchanged.Built-in fixtures now show their annotations too (
tmp_path -> Generator[Path]), so the existingTestShowFixturesexpectations are updated. The new tests use pytester modules without the__future__import, so the object path is covered on older Pythons, plus one test for an unresolvableTYPE_CHECKINGname with the import and one, run on 3.14+ only, without it. I ran the affected tests on 3.10, 3.13 and 3.14.I left the display format as in the issue (
name -> type -- path) and only touched--fixturesand--fixtures-per-test.AI assistance (Claude) was used while preparing this; I've reviewed the change, understand it, and will handle the review myself.