From 794390b24c415a28f4ea681f7157b7e1d836cadb Mon Sep 17 00:00:00 2001 From: Glyphack Date: Thu, 20 Jun 2024 15:31:24 +0200 Subject: [PATCH] refactor: remove _pytestfixturefunction attribute --- src/_pytest/assertion/rewrite.py | 2 +- src/_pytest/compat.py | 2 +- src/_pytest/fixtures.py | 25 ++++++++++++------------- testing/code/test_source.py | 2 +- testing/test_compat.py | 2 +- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index a2a4b135c..818933b6d 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -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__") diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py index 25e66f65c..bd458d9aa 100644 --- a/src/_pytest/compat.py +++ b/src/_pytest/compat.py @@ -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: diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 500e163be..538a262cf 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -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, diff --git a/testing/code/test_source.py b/testing/code/test_source.py index 9ac673572..1bf88fed4 100644 --- a/testing/code/test_source.py +++ b/testing/code/test_source.py @@ -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)") diff --git a/testing/test_compat.py b/testing/test_compat.py index 9c8a2f9d4..fb820b165 100644 --- a/testing/test_compat.py +++ b/testing/test_compat.py @@ -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: