Skip to content

Commit da25c20

Browse files
committed
Show return type annotations in fixtures
1 parent 184f5f1 commit da25c20

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/_pytest/fixtures.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,6 +1911,9 @@ def write_fixture(fixture_def: FixtureDef[object]) -> None:
19111911
return
19121912
prettypath = _pretty_fixture_path(invocation_dir, fixture_def.func)
19131913
tw.write(f"{argname}", green=True)
1914+
ret_annotation = get_return_annotation(fixture_def)
1915+
if ret_annotation:
1916+
tw.write(f" -> {ret_annotation}", cyan=True)
19141917
tw.write(f" -- {prettypath}", yellow=True)
19151918
tw.write("\n")
19161919
fixture_doc = inspect.getdoc(fixture_def.func)
@@ -1995,6 +1998,9 @@ def _showfixtures_main(config: Config, session: Session) -> None:
19951998
if verbose <= 0 and argname.startswith("_"):
19961999
continue
19972000
tw.write(f"{argname}", green=True)
2001+
ret_annotation = get_return_annotation(fixturedef)
2002+
if ret_annotation:
2003+
tw.write(f" -> {ret_annotation}", cyan=True)
19982004
if fixturedef.scope != "function":
19992005
tw.write(f" [{fixturedef.scope} scope]", cyan=True)
20002006
tw.write(f" -- {prettypath}", yellow=True)
@@ -2009,6 +2015,17 @@ def _showfixtures_main(config: Config, session: Session) -> None:
20092015
tw.line()
20102016

20112017

2018+
def get_return_annotation(fixturedef: FixtureDef[object]) -> str:
2019+
try:
2020+
sig = signature(fixturedef.func)
2021+
annotation = sig.return_annotation
2022+
if annotation is not sig.empty and annotation != inspect._empty:
2023+
return inspect.formatannotation(annotation)
2024+
except (ValueError, TypeError):
2025+
pass
2026+
return ""
2027+
2028+
20122029
def write_docstring(tw: TerminalWriter, doc: str, indent: str = " ") -> None:
20132030
for line in doc.split("\n"):
20142031
tw.line(indent + line)

testing/python/fixtures.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3582,9 +3582,9 @@ def test_show_fixtures(self, pytester: Pytester) -> None:
35823582
result = pytester.runpytest("--fixtures")
35833583
result.stdout.fnmatch_lines(
35843584
[
3585-
"tmp_path_factory [[]session scope[]] -- .../_pytest/tmpdir.py:*",
3585+
"tmp_path_factory* [[]session scope[]] -- .../_pytest/tmpdir.py:*",
35863586
"*for the test session*",
3587-
"tmp_path -- .../_pytest/tmpdir.py:*",
3587+
"tmp_path* -- .../_pytest/tmpdir.py:*",
35883588
"*temporary directory*",
35893589
]
35903590
)
@@ -3593,9 +3593,9 @@ def test_show_fixtures_verbose(self, pytester: Pytester) -> None:
35933593
result = pytester.runpytest("--fixtures", "-v")
35943594
result.stdout.fnmatch_lines(
35953595
[
3596-
"tmp_path_factory [[]session scope[]] -- .../_pytest/tmpdir.py:*",
3596+
"tmp_path_factory* [[]session scope[]] -- .../_pytest/tmpdir.py:*",
35973597
"*for the test session*",
3598-
"tmp_path -- .../_pytest/tmpdir.py:*",
3598+
"tmp_path* -- .../_pytest/tmpdir.py:*",
35993599
"*temporary directory*",
36003600
]
36013601
)
@@ -3615,14 +3615,32 @@ def arg1():
36153615
result = pytester.runpytest("--fixtures", p)
36163616
result.stdout.fnmatch_lines(
36173617
"""
3618-
*tmp_path -- *
3618+
*tmp_path* -- *
36193619
*fixtures defined from*
36203620
*arg1 -- test_show_fixtures_testmodule.py:6*
36213621
*hello world*
36223622
"""
36233623
)
36243624
result.stdout.no_fnmatch_line("*arg0*")
36253625

3626+
def test_show_fixtures_return_annotation(self, pytester: Pytester) -> None:
3627+
p = pytester.makepyfile(
3628+
'''
3629+
import pytest
3630+
@pytest.fixture
3631+
def six() -> int:
3632+
return 6
3633+
'''
3634+
)
3635+
result = pytester.runpytest("--fixtures", p)
3636+
result.stdout.fnmatch_lines(
3637+
"""
3638+
*tmp_path* -- *
3639+
*fixtures defined from*
3640+
*six -> int -- test_show_fixtures_return_annotation.py:3*
3641+
"""
3642+
)
3643+
36263644
@pytest.mark.parametrize("testmod", [True, False])
36273645
def test_show_fixtures_conftest(self, pytester: Pytester, testmod) -> None:
36283646
pytester.makeconftest(

0 commit comments

Comments
 (0)