Skip to content

Commit

Permalink
fix mypy errors in tests/conftest.py (#568)
Browse files Browse the repository at this point in the history
* fix mypy errors in tests/conftest.py

* remove unused ignore
  • Loading branch information
stevebachmeier authored Jan 10, 2025
1 parent af4830e commit c1d9790
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
**3.2.14 - TBD/TBD/25**

- Type-hinting: Fix mypy errors in tests/conftest.py

**3.2.13 - 12/27/24**

- Type-hinting: Fix mypy errors in vivarium/examples/boids/
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ exclude = [
'src/vivarium/examples/disease_model/risk.py',
'src/vivarium/interface/cli.py',
'src/vivarium/testing_utilities.py',
'tests/conftest.py',
'tests/examples/test_disease_model.py',
'tests/framework/artifact/test_artifact.py',
'tests/framework/artifact/test_hdf.py',
Expand Down Expand Up @@ -90,5 +89,6 @@ module = [
"ipywidgets.*",
"Ipython.*",
"dill",
"tables",
]
ignore_missing_imports = true
2 changes: 1 addition & 1 deletion src/vivarium/framework/artifact/hdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
from typing import Any, Literal

import pandas as pd
import tables # type: ignore [import-untyped]
import tables
from tables.nodes import filenode # type: ignore [import-untyped]

####################
Expand Down
2 changes: 1 addition & 1 deletion src/vivarium/testing_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,5 @@ def reset_mocks(mocks):
mock.reset_mock()


def metadata(file_path, layer="override"):
def metadata(file_path: str, layer: str = "override") -> dict[str, str]:
return {"layer": layer, "source": str(Path(file_path).resolve())}
33 changes: 19 additions & 14 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from __future__ import annotations

from collections.abc import Generator
from pathlib import Path

import pytest
import pytest_mock
import tables
import yaml
from _pytest.logging import LogCaptureFixture
from layered_config_tree import LayeredConfigTree
from loguru import logger
from vivarium_testing_utils import FuzzyChecker

Expand All @@ -16,15 +19,15 @@
from vivarium.testing_utilities import metadata


def pytest_addoption(parser):
def pytest_addoption(parser: pytest.Parser) -> None:
parser.addoption("--runslow", action="store_true", default=False, help="run slow tests")


def pytest_configure(config):
def pytest_configure(config: pytest.Config) -> None:
config.addinivalue_line("markers", "slow: mark test as slow to run")


def pytest_collection_modifyitems(config, items):
def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item]) -> None:
if config.getoption("--runslow"):
# --runslow given in cli: do not skip slow tests
return
Expand All @@ -35,19 +38,19 @@ def pytest_collection_modifyitems(config, items):


@pytest.fixture(scope="session")
def fuzzy_checker():
def fuzzy_checker() -> FuzzyChecker:
return FuzzyChecker()


@pytest.fixture
def caplog(caplog: LogCaptureFixture):
def caplog(caplog: LogCaptureFixture) -> Generator[LogCaptureFixture, None, None]:
handler_id = logger.add(caplog.handler, format="{message}")
yield caplog
logger.remove(handler_id)


@pytest.fixture
def base_config():
def base_config() -> LayeredConfigTree:
config = build_simulation_configuration()
config.update(
{
Expand All @@ -66,31 +69,33 @@ def base_config():


@pytest.fixture
def test_data_dir():
def test_data_dir() -> Path:
data_dir = Path(__file__).resolve().parent / "test_data"
assert data_dir.exists(), "Test directory structure is broken"
return data_dir


@pytest.fixture(params=[".yaml", ".yml"])
def test_spec(request, test_data_dir):
def test_spec(request: pytest.FixtureRequest, test_data_dir: Path) -> Path:
return test_data_dir / f"mock_model_specification{request.param}"


@pytest.fixture(params=[".yaml", ".yml"])
def test_user_config(request, test_data_dir):
def test_user_config(request: pytest.FixtureRequest, test_data_dir: Path) -> Path:
return test_data_dir / f"mock_user_config{request.param}"


@pytest.fixture
def model_specification(mocker, test_spec, test_user_config):
def model_specification(
mocker: pytest_mock.MockFixture, test_spec: Path, test_user_config: Path
) -> LayeredConfigTree:
expand_user_mock = mocker.patch("vivarium.framework.configuration.Path.expanduser")
expand_user_mock.return_value = test_user_config
return build_model_specification(test_spec)


@pytest.fixture
def disease_model_spec(tmp_path):
def disease_model_spec(tmp_path: Path) -> Path:
model_spec_path = (
Path(__file__).resolve().parent.parent
/ "src/vivarium/examples/disease_model/disease_model.yaml"
Expand All @@ -115,7 +120,7 @@ def disease_model_spec(tmp_path):


@pytest.fixture
def hdf_file_path(tmpdir, test_data_dir):
def hdf_file_path(tmp_path: Path, test_data_dir: Path) -> Path:
"""This file contains the following:
Object Tree:
/ (RootGroup) ''
Expand All @@ -135,13 +140,13 @@ def hdf_file_path(tmpdir, test_data_dir):
/cause/all_causes/restrictions (EArray(166,)) ''
"""
# Make temporary copy of file for test.
p = tmpdir.join("artifact.hdf")
p = tmp_path / "artifact.hdf"
with tables.open_file(str(test_data_dir / "artifact.hdf")) as file:
file.copy_file(str(p), overwrite=True)
return p


@pytest.fixture
def hdf_file(hdf_file_path):
def hdf_file(hdf_file_path: Path) -> Generator[tables.file.File, None, None]:
with tables.open_file(str(hdf_file_path)) as file:
yield file

0 comments on commit c1d9790

Please # to comment.