Skip to content

Commit 3687117

Browse files
committed
Show return type annotations in fixtures
1 parent 3d58e8d commit 3687117

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
@@ -1915,6 +1915,9 @@ def write_fixture(fixture_def: FixtureDef[object]) -> None:
19151915
return
19161916
prettypath = _pretty_fixture_path(invocation_dir, fixture_def.func)
19171917
tw.write(f"{argname}", green=True)
1918+
ret_annotation = get_return_annotation(fixture_def)
1919+
if ret_annotation:
1920+
tw.write(f" -> {ret_annotation}", cyan=True)
19181921
tw.write(f" -- {prettypath}", yellow=True)
19191922
tw.write("\n")
19201923
fixture_doc = inspect.getdoc(fixture_def.func)
@@ -1999,6 +2002,9 @@ def _showfixtures_main(config: Config, session: Session) -> None:
19992002
if verbose <= 0 and argname.startswith("_"):
20002003
continue
20012004
tw.write(f"{argname}", green=True)
2005+
ret_annotation = get_return_annotation(fixturedef)
2006+
if ret_annotation:
2007+
tw.write(f" -> {ret_annotation}", cyan=True)
20022008
if fixturedef.scope != "function":
20032009
tw.write(f" [{fixturedef.scope} scope]", cyan=True)
20042010
tw.write(f" -- {prettypath}", yellow=True)
@@ -2013,6 +2019,17 @@ def _showfixtures_main(config: Config, session: Session) -> None:
20132019
tw.line()
20142020

20152021

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

testing/python/fixtures.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3581,9 +3581,9 @@ def test_show_fixtures(self, pytester: Pytester) -> None:
35813581
result = pytester.runpytest("--fixtures")
35823582
result.stdout.fnmatch_lines(
35833583
[
3584-
"tmp_path_factory [[]session scope[]] -- .../_pytest/tmpdir.py:*",
3584+
"tmp_path_factory* [[]session scope[]] -- .../_pytest/tmpdir.py:*",
35853585
"*for the test session*",
3586-
"tmp_path -- .../_pytest/tmpdir.py:*",
3586+
"tmp_path* -- .../_pytest/tmpdir.py:*",
35873587
"*temporary directory*",
35883588
]
35893589
)
@@ -3592,9 +3592,9 @@ def test_show_fixtures_verbose(self, pytester: Pytester) -> None:
35923592
result = pytester.runpytest("--fixtures", "-v")
35933593
result.stdout.fnmatch_lines(
35943594
[
3595-
"tmp_path_factory [[]session scope[]] -- .../_pytest/tmpdir.py:*",
3595+
"tmp_path_factory* [[]session scope[]] -- .../_pytest/tmpdir.py:*",
35963596
"*for the test session*",
3597-
"tmp_path -- .../_pytest/tmpdir.py:*",
3597+
"tmp_path* -- .../_pytest/tmpdir.py:*",
35983598
"*temporary directory*",
35993599
]
36003600
)
@@ -3614,14 +3614,32 @@ def arg1():
36143614
result = pytester.runpytest("--fixtures", p)
36153615
result.stdout.fnmatch_lines(
36163616
"""
3617-
*tmp_path -- *
3617+
*tmp_path* -- *
36183618
*fixtures defined from*
36193619
*arg1 -- test_show_fixtures_testmodule.py:6*
36203620
*hello world*
36213621
"""
36223622
)
36233623
result.stdout.no_fnmatch_line("*arg0*")
36243624

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

0 commit comments

Comments
 (0)