Repository navigation
Show return type annotations in --fixtures and --fixtures-per-test - #15162
Open
IslamElsayed wants to merge 1 commit into
Open
IslamElsayed wants to merge 1 commit into
IslamElsayed wants to merge 1 commit 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
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.