Skip to content

Commit 536c8d2

Browse files
committed
Add tests for get_return_annotation
1 parent ce68932 commit 536c8d2

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/_pytest/fixtures.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,7 +1915,7 @@ 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)
1918+
ret_annotation = get_return_annotation(fixture_def.func)
19191919
if ret_annotation:
19201920
tw.write(f" -> {ret_annotation}", cyan=True)
19211921
tw.write(f" -- {prettypath}", yellow=True)
@@ -2002,7 +2002,7 @@ def _showfixtures_main(config: Config, session: Session) -> None:
20022002
if verbose <= 0 and argname.startswith("_"):
20032003
continue
20042004
tw.write(f"{argname}", green=True)
2005-
ret_annotation = get_return_annotation(fixturedef)
2005+
ret_annotation = get_return_annotation(fixturedef.func)
20062006
if ret_annotation:
20072007
tw.write(f" -> {ret_annotation}", cyan=True)
20082008
if fixturedef.scope != "function":
@@ -2019,12 +2019,12 @@ def _showfixtures_main(config: Config, session: Session) -> None:
20192019
tw.line()
20202020

20212021

2022-
def get_return_annotation(fixturedef: FixtureDef[object]) -> str:
2022+
def get_return_annotation(fixture_func: Callable) -> str:
20232023
try:
2024-
sig = signature(fixturedef.func)
2024+
sig = signature(fixture_func)
20252025
annotation = sig.return_annotation
20262026
if annotation is not sig.empty and annotation != inspect._empty:
2027-
return inspect.formatannotation(annotation)
2027+
return inspect.formatannotation(annotation).replace("'", "")
20282028
except (ValueError, TypeError):
20292029
pass
20302030
return ""

testing/python/fixtures.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
from pathlib import Path
66
import sys
77
import textwrap
8+
from typing import Tuple
89

910
from _pytest.compat import getfuncargnames
1011
from _pytest.config import ExitCode
1112
from _pytest.fixtures import deduplicate_names
13+
from _pytest.fixtures import get_return_annotation
1214
from _pytest.fixtures import TopRequest
1315
from _pytest.monkeypatch import MonkeyPatch
1416
from _pytest.pytester import get_public_names
@@ -3634,7 +3636,6 @@ def six() -> int:
36343636
result = pytester.runpytest("--fixtures", p)
36353637
result.stdout.fnmatch_lines(
36363638
"""
3637-
*tmp_path* -- *
36383639
*fixtures defined from*
36393640
*six -> int -- test_show_fixtures_return_annotation.py:3*
36403641
"""
@@ -5086,3 +5087,20 @@ def test_method(self, /, fix):
50865087
)
50875088
result = pytester.runpytest()
50885089
result.assert_outcomes(passed=1)
5090+
5091+
def test_get_return_annotation() -> None:
5092+
def six() -> int:
5093+
return 6
5094+
assert get_return_annotation(six) == "int"
5095+
5096+
def two_sixes() -> Tuple[int, str]:
5097+
return (6, "six")
5098+
assert get_return_annotation(two_sixes) == "Tuple[int, str]"
5099+
5100+
def no_annot():
5101+
return 6
5102+
assert get_return_annotation(no_annot) == ""
5103+
5104+
def none_return() -> None:
5105+
pass
5106+
assert get_return_annotation(none_return) == "None"

0 commit comments

Comments
 (0)