Skip to content

Commit 3b6c1e9

Browse files
committed
Use regex approach to retrieve return annotations
1 parent b78fe02 commit 3b6c1e9

File tree

3 files changed

+8
-105
lines changed

3 files changed

+8
-105
lines changed

src/_pytest/fixtures.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import inspect
1919
import os
2020
from pathlib import Path
21+
import re
2122
import sys
2223
import types
2324
from typing import Any
@@ -2016,20 +2017,13 @@ def _showfixtures_main(config: Config, session: Session) -> None:
20162017

20172018

20182019
def get_return_annotation(fixture_func: Callable[..., Any]) -> str:
2019-
try:
2020-
sig = signature(fixture_func)
2021-
annotation = sig.return_annotation
2022-
if annotation is not sig.empty:
2023-
if type(annotation) == type(None):
2024-
return "None"
2025-
if isinstance(annotation, str):
2026-
return annotation
2027-
if annotation.__module__ == "typing":
2028-
return str(annotation).replace("typing.", "")
2029-
return str(annotation.__name__)
2030-
except (ValueError, TypeError):
2031-
pass
2032-
return ""
2020+
pattern = re.compile(r'\b(?:(?:<[^>]+>|[A-Za-z_]\w*)\.)+([A-Za-z_]\w*)\b')
2021+
2022+
sig = str(signature(fixture_func))
2023+
sig_parts = sig.split(" -> ")
2024+
if len(sig_parts) < 2:
2025+
return ""
2026+
return pattern.sub(r'\1', sig_parts[-1])
20332027

20342028

20352029
def write_docstring(tw: TerminalWriter, doc: str, indent: str = " ") -> None:

testing/python/fixtures.py

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
# mypy: allow-untyped-defs
22
from __future__ import annotations
33

4-
from collections.abc import Callable
54
from itertools import zip_longest
65
import os
76
from pathlib import Path
87
import sys
98
import textwrap
10-
from typing import Any
119

1210
from _pytest.compat import getfuncargnames
1311
from _pytest.config import ExitCode
1412
from _pytest.fixtures import deduplicate_names
15-
from _pytest.fixtures import get_return_annotation
1613
from _pytest.fixtures import TopRequest
1714
from _pytest.monkeypatch import MonkeyPatch
1815
from _pytest.pytester import get_public_names
@@ -3626,23 +3623,6 @@ def arg1():
36263623
)
36273624
result.stdout.no_fnmatch_line("*arg0*")
36283625

3629-
def test_show_fixtures_return_annotation(self, pytester: Pytester) -> None:
3630-
p = pytester.makepyfile(
3631-
"""
3632-
import pytest
3633-
@pytest.fixture
3634-
def six() -> int:
3635-
return 6
3636-
"""
3637-
)
3638-
result = pytester.runpytest("--fixtures", p)
3639-
result.stdout.fnmatch_lines(
3640-
"""
3641-
*fixtures defined from*
3642-
*six -> int -- test_show_fixtures_return_annotation.py:3*
3643-
"""
3644-
)
3645-
36463626
@pytest.mark.parametrize("testmod", [True, False])
36473627
def test_show_fixtures_conftest(self, pytester: Pytester, testmod) -> None:
36483628
pytester.makeconftest(
@@ -4584,53 +4564,6 @@ def test_1(self, myfix):
45844564
reprec.assertoutcome(passed=1)
45854565

45864566

4587-
class TestGetReturnAnnotation:
4588-
def test_primitive_return_type(self):
4589-
def six() -> int:
4590-
return 6
4591-
4592-
assert get_return_annotation(six) == "int"
4593-
4594-
def test_compound_return_type(self):
4595-
def two_sixes() -> tuple[int, str]:
4596-
return (6, "six")
4597-
4598-
assert get_return_annotation(two_sixes) == "tuple[int, str]"
4599-
4600-
def test_callable_return_type(self):
4601-
def callable_return() -> Callable[..., Any]:
4602-
return self.test_compound_return_type
4603-
4604-
assert get_return_annotation(callable_return) == "Callable[..., Any]"
4605-
4606-
def test_no_annotation(self):
4607-
def no_annotation():
4608-
return 6
4609-
4610-
assert get_return_annotation(no_annotation) == ""
4611-
4612-
def test_none_return_type(self):
4613-
def none_return() -> None:
4614-
pass
4615-
4616-
assert get_return_annotation(none_return) == "None"
4617-
4618-
def test_custom_class_return_type(self):
4619-
class T:
4620-
pass
4621-
4622-
def class_return() -> T:
4623-
return T()
4624-
4625-
assert get_return_annotation(class_return) == "T"
4626-
4627-
def test_enum_return_type(self):
4628-
def enum_return() -> ExitCode:
4629-
return ExitCode(0)
4630-
4631-
assert get_return_annotation(enum_return) == "ExitCode"
4632-
4633-
46344567
def test_call_fixture_function_error():
46354568
"""Check if an error is raised if a fixture function is called directly (#4545)"""
46364569

testing/python/show_fixtures_per_test.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -160,30 +160,6 @@ 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-
187163
def test_doctest_items(pytester: Pytester) -> None:
188164
pytester.makepyfile(
189165
'''

0 commit comments

Comments
 (0)