Skip to content

Commit fd267bf

Browse files
committed
Update call_fixture() to work with pytest 8.4.
The implementation retains compatibility with older versions.
1 parent 4ae6499 commit fd267bf

File tree

1 file changed

+21
-7
lines changed
  • src/databricks/labs/pytester/fixtures

1 file changed

+21
-7
lines changed

src/databricks/labs/pytester/fixtures/unwrap.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from typing import TypeVar
66
from unittest.mock import MagicMock, create_autospec
77

8+
import pytest
9+
810
from databricks.labs.lsql.backends import MockBackend
911
from databricks.sdk import WorkspaceClient
1012
from databricks.sdk.service import iam
@@ -14,13 +16,25 @@
1416
T = TypeVar('T')
1517

1618

17-
def call_fixture(fixture_fn: Callable[..., T], *args, **kwargs) -> T:
18-
if not hasattr(fixture_fn, '__pytest_wrapped__'):
19-
raise ValueError(f'{fixture_fn} is not a pytest fixture')
20-
wrapped = getattr(fixture_fn, '__pytest_wrapped__')
21-
if not hasattr(wrapped, 'obj'):
22-
raise ValueError(f'{fixture_fn} is not a pytest fixture')
23-
return wrapped.obj(*args, **kwargs)
19+
# Pytest fixtures are not supposed to be called directly, but historically we hack through the countermeasures.
20+
# TODO: Investigate and fix this if possible, to avoid breakage in future pytest versions.
21+
# Potential solution: use `pytest.FixtureRequest` & `request.getfixturevalue()` to access fixtures.
22+
if pytest.version_tuple >= (8, 4):
23+
def call_fixture(fixture_fn: Callable[..., T], *args, **kwargs) -> T:
24+
if not hasattr(fixture_fn, "_get_wrapped_function"):
25+
raise ValueError(f'{fixture_fn} is not a pytest fixture')
26+
accessor = getattr(fixture_fn, "_get_wrapped_function")
27+
wrapped = accessor()
28+
return wrapped(*args, **kwargs)
29+
else:
30+
# Older versions of pytest use a different mechanism to wrap fixtures.
31+
def call_fixture(fixture_fn: Callable[..., T], *args, **kwargs) -> T:
32+
if not hasattr(fixture_fn, '__pytest_wrapped__'):
33+
raise ValueError(f'{fixture_fn} is not a pytest fixture')
34+
wrapped = getattr(fixture_fn, '__pytest_wrapped__')
35+
if not hasattr(wrapped, 'obj'):
36+
raise ValueError(f'{fixture_fn} is not a pytest fixture')
37+
return wrapped.obj(*args, **kwargs)
2438

2539

2640
class CallContext:

0 commit comments

Comments
 (0)