Skip to content

Commit bc6450d

Browse files
committed
Improve get_return_annotation logic
1 parent ec239a4 commit bc6450d

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
@@ -2019,8 +2019,12 @@ def get_return_annotation(fixture_func: Callable[..., Any]) -> str:
20192019
try:
20202020
sig = signature(fixture_func)
20212021
annotation = sig.return_annotation
2022-
if annotation is not sig.empty and annotation != inspect._empty:
2023-
return inspect.formatannotation(annotation).replace("'", "")
2022+
if annotation is not sig.empty:
2023+
if isinstance(annotation, str):
2024+
return annotation
2025+
if annotation.__module__ == "typing":
2026+
return str(annotation).replace("typing.", "")
2027+
return annotation.__name__
20242028
except (ValueError, TypeError):
20252029
pass
20262030
return ""

testing/python/fixtures.py

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

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

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

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

5106-
assert get_return_annotation(no_annot) == ""
5112+
assert get_return_annotation(no_annotation) == ""
51075113

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

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

5119+
class T:
5120+
pass
5121+
def class_return() -> T:
5122+
return T()
5123+
5124+
assert get_return_annotation(class_return) == "T"
5125+
5126+
def enum_return() -> ExitCode:
5127+
return ExitCode(0)
5128+
5129+
assert get_return_annotation(enum_return) == "ExitCode"
5130+
51135131
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)