Skip to content

Commit 776560e

Browse files
committed
Move get_return_annotation tests into a separate class
1 parent eb16715 commit 776560e

File tree

2 files changed

+49
-43
lines changed

2 files changed

+49
-43
lines changed

src/_pytest/fixtures.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2024,11 +2024,13 @@ def get_return_annotation(fixture_func: Callable[..., Any]) -> str:
20242024
sig = signature(fixture_func)
20252025
annotation = sig.return_annotation
20262026
if annotation is not sig.empty:
2027+
if type(annotation) == type(None):
2028+
return "None"
20272029
if isinstance(annotation, str):
20282030
return annotation
20292031
if annotation.__module__ == "typing":
20302032
return str(annotation).replace("typing.", "")
2031-
return annotation.__name__
2033+
return str(annotation.__name__)
20322034
except (ValueError, TypeError):
20332035
pass
20342036
return ""

testing/python/fixtures.py

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4583,6 +4583,52 @@ def test_1(self, myfix):
45834583
reprec.assertoutcome(passed=1)
45844584

45854585

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

@@ -5088,45 +5134,3 @@ def test_method(self, /, fix):
50885134
)
50895135
result = pytester.runpytest()
50905136
result.assert_outcomes(passed=1)
5091-
5092-
5093-
def test_get_return_annotation() -> None:
5094-
def six() -> int:
5095-
return 6
5096-
5097-
assert get_return_annotation(six) == "int"
5098-
5099-
def two_sixes() -> tuple[int, str]:
5100-
return (6, "six")
5101-
5102-
assert get_return_annotation(two_sixes) == "tuple[int, str]"
5103-
5104-
def callable_return() -> Callable[..., Any]:
5105-
return two_sixes
5106-
5107-
assert get_return_annotation(callable_return) == "Callable[..., Any]"
5108-
5109-
def no_annotation():
5110-
return 6
5111-
5112-
assert get_return_annotation(no_annotation) == ""
5113-
5114-
def none_return() -> None:
5115-
pass
5116-
5117-
assert get_return_annotation(none_return) == "None"
5118-
5119-
class T:
5120-
pass
5121-
5122-
def class_return() -> T:
5123-
return T()
5124-
5125-
assert get_return_annotation(class_return) == "T"
5126-
5127-
def enum_return() -> ExitCode:
5128-
return ExitCode(0)
5129-
5130-
assert get_return_annotation(enum_return) == "ExitCode"
5131-
5132-
assert get_return_annotation(range) == ""

0 commit comments

Comments
 (0)