Skip to content

Commit 37732a0

Browse files
committed
Add tests for get_return_annotation
1 parent b5a86c7 commit 37732a0

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
@@ -1911,7 +1911,7 @@ 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)
1914+
ret_annotation = get_return_annotation(fixture_def.func)
19151915
if ret_annotation:
19161916
tw.write(f" -> {ret_annotation}", cyan=True)
19171917
tw.write(f" -- {prettypath}", yellow=True)
@@ -1998,7 +1998,7 @@ def _showfixtures_main(config: Config, session: Session) -> None:
19981998
if verbose <= 0 and argname.startswith("_"):
19991999
continue
20002000
tw.write(f"{argname}", green=True)
2001-
ret_annotation = get_return_annotation(fixturedef)
2001+
ret_annotation = get_return_annotation(fixturedef.func)
20022002
if ret_annotation:
20032003
tw.write(f" -> {ret_annotation}", cyan=True)
20042004
if fixturedef.scope != "function":
@@ -2015,12 +2015,12 @@ def _showfixtures_main(config: Config, session: Session) -> None:
20152015
tw.line()
20162016

20172017

2018-
def get_return_annotation(fixturedef: FixtureDef[object]) -> str:
2018+
def get_return_annotation(fixture_func: Callable) -> str:
20192019
try:
2020-
sig = signature(fixturedef.func)
2020+
sig = signature(fixture_func)
20212021
annotation = sig.return_annotation
20222022
if annotation is not sig.empty and annotation != inspect._empty:
2023-
return inspect.formatannotation(annotation)
2023+
return inspect.formatannotation(annotation).replace("'", "")
20242024
except (ValueError, TypeError):
20252025
pass
20262026
return ""

testing/python/fixtures.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
from pathlib import Path
77
import sys
88
import textwrap
9+
from typing import Tuple
910

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

0 commit comments

Comments
 (0)