Skip to content

Commit

Permalink
remove localized lazy-fixture code; pin pytest<8 in setup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ronpandolfi committed Mar 4, 2024
1 parent e189096 commit 4dd9376
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 88 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@
extras_require={
"dev": ["pyinstaller"],
"docs": ["sphinx", "sphinx-markdown-tables", "numpydoc", "sphinx_copybutton", "myst_parser", "sphinx_rtd_theme", "sphinx_rtd_dark_mode"],
"tests": ["pytest", "coverage", "coveralls", "codecov", "pylint", "pytest-qt", "pytest-cov"],
"tests": ["pytest<8", "coverage", "coveralls", "codecov", "pylint", "pytest-qt", "pytest-cov", "pytest-lazy-fixture"],
},
)
88 changes: 1 addition & 87 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from pathlib import Path
from threading import Thread
import dataclasses
import typing

import pytest
import numpy as np
Expand All @@ -10,7 +8,7 @@
from loguru import logger
from ophyd import Device
from ophyd.sim import SynAxis, SynSignal, Cpt
from pytest import fixture
from pytest import fixture, lazy_fixture
from scipy import ndimage

from tsuchinoko.adaptive.gpCAM_in_process import GPCAMInProcessEngine
Expand All @@ -27,90 +25,6 @@



@dataclasses.dataclass
class LazyFixture:
"""Lazy fixture dataclass."""

name: str


def lazy_fixture(name: str) -> LazyFixture:
"""Mark a fixture as lazy."""
return LazyFixture(name)


def is_lazy_fixture(value: object) -> bool:
"""Check whether a value is a lazy fixture."""
return isinstance(value, LazyFixture)


def pytest_make_parametrize_id(
config: pytest.Config,
val: object,
argname: str,
) -> str | None:
"""Inject lazy fixture parametrized id.
Reference:
- https://bit.ly/48Off6r
Args:
config (pytest.Config): pytest configuration.
value (object): fixture value.
argname (str): automatic parameter name.
Returns:
str: new parameter id.
"""
if is_lazy_fixture(val):
return typing.cast(LazyFixture, val).name
return None


@pytest.hookimpl(tryfirst=True)
def pytest_fixture_setup(
fixturedef: pytest.FixtureDef,
request: pytest.FixtureRequest,
) -> object | None:
"""Lazy fixture setup hook.
This hook will never take over a fixture setup but just simply will
try to resolve recursively any lazy fixture found in request.param.
Reference:
- https://bit.ly/3SyvsXJ
Args:
fixturedef (pytest.FixtureDef): fixture definition object.
request (pytest.FixtureRequest): fixture request object.
Returns:
object | None: fixture value or None otherwise.
"""
if hasattr(request, "param") and request.param:
request.param = _resolve_lazy_fixture(request.param, request)
return None


def _resolve_lazy_fixture(__val: object, request: pytest.FixtureRequest) -> object:
"""Lazy fixture resolver.
Args:
__val (object): fixture value object.
request (pytest.FixtureRequest): pytest fixture request object.
Returns:
object: resolved fixture value.
"""
if isinstance(__val, list | tuple):
return tuple(_resolve_lazy_fixture(v, request) for v in __val)
if isinstance(__val, typing.Mapping):
return {k: _resolve_lazy_fixture(v, request) for k, v in __val.items()}
if not is_lazy_fixture(__val):
return __val
lazy_obj = typing.cast(LazyFixture, __val)
return request.getfixturevalue(lazy_obj.name)


@fixture
def image_data():
Expand Down

0 comments on commit 4dd9376

Please sign in to comment.