Skip to content

Commit 79ce682

Browse files
committed
Improve get_return_annotation logic
1 parent ebe8c9c commit 79ce682

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

changelog/13676.improvement.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Added return type annotations in ``fixtures`` and ``fixtures-per-test``.
1+
Return type annotations are now shown in ``--fixtures`` and ``--fixtures-per-test``.

src/_pytest/fixtures.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,8 +2023,12 @@ def get_return_annotation(fixture_func: Callable[..., Any]) -> str:
20232023
try:
20242024
sig = signature(fixture_func)
20252025
annotation = sig.return_annotation
2026-
if annotation is not sig.empty and annotation != inspect._empty:
2027-
return inspect.formatannotation(annotation).replace("'", "")
2026+
if annotation is not sig.empty:
2027+
if isinstance(annotation, str):
2028+
return annotation
2029+
if annotation.__module__ == "typing":
2030+
return str(annotation).replace("typing.", "")
2031+
return annotation.__name__
20282032
except (ValueError, TypeError):
20292033
pass
20302034
return ""

testing/python/fixtures.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pathlib import Path
66
import sys
77
import textwrap
8+
from typing import Any, Callable
89

910
from _pytest.compat import getfuncargnames
1011
from _pytest.config import ExitCode
@@ -5099,14 +5100,31 @@ def two_sixes() -> tuple[int, str]:
50995100

51005101
assert get_return_annotation(two_sixes) == "tuple[int, str]"
51015102

5102-
def no_annot():
5103+
def callable_return() -> Callable[..., Any]:
5104+
return two_sixes
5105+
5106+
assert get_return_annotation(callable_return) == "Callable[..., Any]"
5107+
5108+
def no_annotation():
51035109
return 6
51045110

5105-
assert get_return_annotation(no_annot) == ""
5111+
assert get_return_annotation(no_annotation) == ""
51065112

51075113
def none_return() -> None:
51085114
pass
51095115

51105116
assert get_return_annotation(none_return) == "None"
51115117

5118+
class T:
5119+
pass
5120+
def class_return() -> T:
5121+
return T()
5122+
5123+
assert get_return_annotation(class_return) == "T"
5124+
5125+
def enum_return() -> ExitCode:
5126+
return ExitCode(0)
5127+
5128+
assert get_return_annotation(enum_return) == "ExitCode"
5129+
51125130
assert get_return_annotation(range) == ""

testing/python/show_fixtures_per_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,30 @@ def test_args(arg2, arg3):
160160
)
161161

162162

163+
def test_show_return_annotation(pytester: Pytester) -> None:
164+
p = pytester.makepyfile(
165+
'''
166+
import pytest
167+
@pytest.fixture
168+
def five() -> int:
169+
return 5
170+
def test_five(five):
171+
pass
172+
'''
173+
)
174+
175+
result = pytester.runpytest("--fixtures-per-test", p)
176+
assert result.ret == 0
177+
178+
result.stdout.fnmatch_lines(
179+
[
180+
"*fixtures used by test_five*",
181+
"*(test_show_return_annotation.py:6)*",
182+
"five -> int -- test_show_return_annotation.py:3",
183+
]
184+
)
185+
186+
163187
def test_doctest_items(pytester: Pytester) -> None:
164188
pytester.makepyfile(
165189
'''

0 commit comments

Comments
 (0)