refactor: remove _pytestfixturefunction attribute

This commit is contained in:
Glyphack 2024-06-20 15:31:24 +02:00
parent 1f4d9ab2cb
commit 794390b24c
5 changed files with 16 additions and 17 deletions

View File

@ -462,7 +462,7 @@ def _format_assertmsg(obj: object) -> str:
def _should_repr_global_name(obj: object) -> bool:
if callable(obj):
return hasattr(obj, "_pytestfixturefunction")
return hasattr(obj, "_fixture_function_marker")
try:
return not hasattr(obj, "__name__")

View File

@ -217,7 +217,7 @@ def get_real_func(obj):
start_obj = obj
for _ in range(100):
if isinstance(obj, FixtureFunctionDefinition):
obj = obj.get_real_func()
obj = obj._get_wrapped_function()
break
new_obj = getattr(obj, "__wrapped__", None)
if new_obj is None:

View File

@ -155,7 +155,7 @@ def getfixturemarker(obj: object) -> Optional["FixtureFunctionMarker"]:
exceptions."""
return cast(
Optional[FixtureFunctionMarker],
safe_getattr(obj, "_pytestfixturefunction", None),
safe_getattr(obj, "_fixture_function_marker", None),
)
@ -1193,7 +1193,7 @@ class FixtureFunctionDefinition:
def __init__(
self,
function: Callable[..., object],
function: Callable[..., Any],
fixture_function_marker: FixtureFunctionMarker,
instance: Optional[type] = None,
):
@ -1202,17 +1202,16 @@ class FixtureFunctionDefinition:
# This attribute is only used to check if an arbitrary python object is a fixture.
# Using isinstance on every object in code might execute code that is not intended to be executed.
# Like lazy loaded classes.
self._pytestfixturefunction = fixture_function_marker
self.fixture_function_marker = fixture_function_marker
self.fixture_function = function
self.instance = instance
self._fixture_function_marker = fixture_function_marker
self._fixture_function = function
self._instance = instance
def __repr__(self) -> str:
return f"pytest_fixture({self.fixture_function})"
return f"pytest_fixture({self._fixture_function})"
def __get__(self, instance, owner=None):
return FixtureFunctionDefinition(
self.fixture_function, self.fixture_function_marker, instance
self._fixture_function, self._fixture_function_marker, instance
)
def __call__(self, *args: Any, **kwds: Any) -> Any:
@ -1224,10 +1223,10 @@ class FixtureFunctionDefinition:
)
fail(message, pytrace=False)
def get_real_func(self):
if self.instance is not None:
return self.fixture_function.__get__(self.instance)
return self.fixture_function
def _get_wrapped_function(self):
if self._instance is not None:
return self._fixture_function.__get__(self._instance)
return self._fixture_function
@overload
@ -1769,7 +1768,7 @@ class FixtureManager:
if isinstance(obj, FixtureFunctionDefinition):
if marker.name:
name = marker.name
func = obj.get_real_func()
func = obj._get_wrapped_function()
self._register_fixture(
name=name,
nodeid=nodeid,

View File

@ -481,7 +481,7 @@ def test_source_with_decorator() -> None:
# Since deco_fixture is now an instance of FixtureFunctionDef the getsource function will not work on it.
with pytest.raises(Exception):
inspect.getsource(deco_fixture)
src = inspect.getsource(deco_fixture.get_real_func())
src = inspect.getsource(deco_fixture._get_wrapped_function())
assert src == " @pytest.fixture\n def deco_fixture():\n assert False\n"
# Make sure the decorator is not a wrapped function
assert not str(Source(deco_fixture)).startswith("@functools.wraps(function)")

View File

@ -83,7 +83,7 @@ def test_get_real_func() -> None:
pass
wrapped_func4 = decorator(wrapped_func3)
assert get_real_func(wrapped_func4) is wrapped_func3.get_real_func()
assert get_real_func(wrapped_func4) is wrapped_func3._get_wrapped_function()
def test_get_real_func_partial() -> None: