Skip to content

Show return type annotations in --fixtures and --fixtures-per-test - #15162

Open
IslamElsayed wants to merge 1 commit into
pytest-dev:mainfrom
IslamElsayed:fixtures-return-annotation
Open

IslamElsayed wants to merge 1 commit into
pytest-dev:mainfrom
IslamElsayed:fixtures-return-annotation

Conversation

@IslamElsayed

Copy link
Copy Markdown

Closes #13676.

Fixtures with a return annotation now show it in --fixtures and --fixtures-per-test:

answer -> int -- test_x.py:4
    Answer to the Ultimate Question of Life, the Universe, and Everything.

This picks up where #13680 left off (thanks @kianelbo), following what was agreed there:

  • On Python 3.14+ the annotation is read as a string with 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 under TYPE_CHECKING is shown, not raised.
  • Before 3.14 it uses inspect.formatannotation, with no regex post-processing, per @nicoddemus. It passes base_module=func.__module__, so a class from the fixture's own module shows as Answer rather than test_x.Answer. String annotations (from __future__ import annotations) are shown as written.
  • -> None is shown as None, and fixtures without a return annotation are unchanged.

Built-in fixtures now show their annotations too (tmp_path -> Generator[Path]), so the existing TestShowFixtures expectations 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 unresolvable TYPE_CHECKING name 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 --fixtures and --fixtures-per-test.

AI assistance (Claude) was used while preparing this; I've reviewed the change, understand it, and will handle the review myself.

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
@psf-chronographer psf-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.

Show return type annotations in --fixtures and --fixtures-per-test

1 participant