File tree Expand file tree Collapse file tree 3 files changed +15
-31
lines changed Expand file tree Collapse file tree 3 files changed +15
-31
lines changed Original file line number Diff line number Diff line change 3
3
4
4
from __future__ import annotations
5
5
6
- import dataclasses
7
6
import enum
8
7
import functools
9
8
import inspect
@@ -210,30 +209,15 @@ def ascii_escaped(val: bytes | str) -> str:
210
209
return ret .translate (_non_printable_ascii_translate_table )
211
210
212
211
213
- # TODO: remove and replace with FixtureFunctionDefinition
214
- @dataclasses .dataclass
215
- class _PytestWrapper :
216
- """Dummy wrapper around a function object for internal use only.
217
-
218
- Used to correctly unwrap the underlying function object when we are
219
- creating fixtures, because we wrap the function object ourselves with a
220
- decorator to issue warnings when the fixture function is called directly.
221
- """
222
-
223
- obj : Any
224
-
225
-
226
212
def get_real_func (obj ):
227
213
"""Get the real function object of the (possibly) wrapped object by
228
- functools.wraps or functools.partial."""
214
+ functools.wraps or functools.partial or pytest.fixture"""
215
+ from _pytest .fixtures import FixtureFunctionDefinition
216
+
229
217
start_obj = obj
230
- for i in range (100 ):
231
- # __pytest_wrapped__ is set by @pytest.fixture when wrapping the fixture function
232
- # to trigger a warning if it gets called directly instead of by pytest: we don't
233
- # want to unwrap further than this otherwise we lose useful wrappings like @mock.patch (#3774)
234
- new_obj = getattr (obj , "__pytest_wrapped__" , None )
235
- if isinstance (new_obj , _PytestWrapper ):
236
- obj = new_obj .obj
218
+ for _ in range (100 ):
219
+ if isinstance (obj , FixtureFunctionDefinition ):
220
+ obj = obj .get_real_func ()
237
221
break
238
222
new_obj = getattr (obj , "__wrapped__" , None )
239
223
if new_obj is None :
Original file line number Diff line number Diff line change 42
42
from _pytest ._code .code import FormattedExcinfo
43
43
from _pytest ._code .code import TerminalRepr
44
44
from _pytest ._io import TerminalWriter
45
- from _pytest .compat import _PytestWrapper
46
45
from _pytest .compat import assert_never
47
46
from _pytest .compat import get_real_func
48
47
from _pytest .compat import getfuncargnames
@@ -1204,7 +1203,6 @@ def __init__(
1204
1203
# Using isinstance on every object in code might execute code that is not intended to be executed.
1205
1204
# Like lazy loaded classes.
1206
1205
self ._pytestfixturefunction = fixture_function_marker
1207
- self .__pytest_wrapped__ = _PytestWrapper (function )
1208
1206
self .fixture_function_marker = fixture_function_marker
1209
1207
self .fixture_function = function
1210
1208
self .instance = instance
Original file line number Diff line number Diff line change 7
7
from typing import TYPE_CHECKING
8
8
from typing import Union
9
9
10
- from _pytest .compat import _PytestWrapper
11
10
from _pytest .compat import assert_never
12
11
from _pytest .compat import get_real_func
13
12
from _pytest .compat import is_generator
@@ -52,8 +51,8 @@ def __getattr__(self, attr):
52
51
with pytest .raises (
53
52
ValueError ,
54
53
match = (
55
- "could not find real function of <Evil left=800 >\n "
56
- "stopped at <Evil left=800 >"
54
+ "could not find real function of <Evil left=900 >\n "
55
+ "stopped at <Evil left=900 >"
57
56
),
58
57
):
59
58
get_real_func (evil )
@@ -78,10 +77,13 @@ def func():
78
77
wrapped_func2 = decorator (decorator (wrapped_func ))
79
78
assert get_real_func (wrapped_func2 ) is func
80
79
81
- # special case for __pytest_wrapped__ attribute: used to obtain the function up until the point
82
- # a function was wrapped by pytest itself
83
- wrapped_func2 .__pytest_wrapped__ = _PytestWrapper (wrapped_func )
84
- assert get_real_func (wrapped_func2 ) is wrapped_func
80
+ # obtain the function up until the point a function was wrapped by pytest itself
81
+ @pytest .fixture
82
+ def wrapped_func3 ():
83
+ pass
84
+
85
+ wrapped_func4 = decorator (wrapped_func3 )
86
+ assert get_real_func (wrapped_func4 ) is wrapped_func3 .get_real_func ()
85
87
86
88
87
89
def test_get_real_func_partial () -> None :
You can’t perform that action at this time.
0 commit comments