Skip to content

Commit 14dae78

Browse files
test_parser typing (#584)
typing for tests/framework/components/test_parser.py
1 parent a816095 commit 14dae78

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
**3.3.2 - 02/12/25**
2+
3+
- Type-hinting: Fix mypy errors in tests/framework/components/test_parser.py
4+
15
**3.3.1 - 02/07/25**
26

37
- Type-hinting: Fix mypy errors in tests/framework/lookup/test_interpolation.py

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ exclude = [
4343
'src/vivarium/testing_utilities.py',
4444
'tests/examples/test_disease_model.py',
4545
'tests/framework/components/test_manager.py',
46-
'tests/framework/components/test_parser.py',
4746
'tests/framework/lookup/test_lookup.py',
4847
'tests/framework/population/test_manager.py',
4948
'tests/framework/population/test_population_view.py',

tests/framework/components/test_parser.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
from unittest.mock import call
1+
from abc import ABCMeta
2+
from typing import Any
3+
from unittest.mock import Mock, call
24

35
import pytest
46
import yaml
7+
from _pytest.monkeypatch import MonkeyPatch
58
from layered_config_tree.main import LayeredConfigTree
9+
from pytest_mock import MockerFixture
610

711
from tests.helpers import MockComponentA, MockComponentB
812
from vivarium.framework.components.parser import ComponentConfigurationParser, ParsingError
@@ -75,40 +79,34 @@
7579
]
7680

7781

78-
def mock_importer(path):
82+
def mock_importer(path: str) -> ABCMeta:
7983
return {
8084
"test_components.MockComponentA": MockComponentA,
8185
"test_components.MockComponentB": MockComponentB,
8286
}[path]
8387

8488

8589
@pytest.fixture()
86-
def parser(mocker) -> ComponentConfigurationParser:
90+
def parser(mocker: MockerFixture) -> ComponentConfigurationParser:
8791
parser = ComponentConfigurationParser()
88-
parser.import_and_instantiate_component = mocker.Mock()
92+
parser.import_and_instantiate_component = mocker.Mock() # type: ignore [method-assign]
8993
return parser
9094

9195

9296
@pytest.fixture(params=[TEST_COMPONENTS_NESTED, TEST_COMPONENTS_FLAT])
93-
def components(request):
97+
def components(request: pytest.FixtureRequest) -> str:
98+
assert isinstance(request.param, str)
9499
return request.param
95100

96101

97-
@pytest.fixture
98-
def import_and_instantiate_mock(mocker):
99-
return mocker.patch(
100-
"vivarium.framework.components.parser.import_and_instantiate_component"
101-
)
102-
103-
104-
def test_prep_component(parser):
102+
def test_prep_component(parser: ComponentConfigurationParser) -> None:
105103
desc = 'cave_system.monsters.Rabbit("timid", "squeak")'
106104
component, args = parser.prep_component(desc)
107105
assert component == "cave_system.monsters.Rabbit"
108106
assert set(args) == {"timid", "squeak"}
109107

110108

111-
def test_prep_component_syntax_error(parser):
109+
def test_prep_component_syntax_error(parser: ComponentConfigurationParser) -> None:
112110
desc = 'cave_system.monsters.Rabbit("timid", 0.01)'
113111
with pytest.raises(ParsingError):
114112
parser.prep_component(desc)
@@ -122,15 +120,15 @@ def test_prep_component_syntax_error(parser):
122120
parser.prep_component(desc)
123121

124122

125-
def test_parse_and_prep_components(parser):
123+
def test_parse_and_prep_components(parser: ComponentConfigurationParser) -> None:
126124
prepped_components = [
127125
parser.prep_component(component) for component in TEST_COMPONENTS_PARSED
128126
]
129127

130128
assert set(TEST_COMPONENTS_PREPPED) == set(prepped_components)
131129

132130

133-
def test_import_and_instantiate_components(monkeypatch):
131+
def test_import_and_instantiate_components(monkeypatch: MonkeyPatch) -> None:
134132
monkeypatch.setattr("vivarium.framework.components.parser.import_by_path", mock_importer)
135133

136134
component_descriptions = [
@@ -151,14 +149,15 @@ def test_import_and_instantiate_components(monkeypatch):
151149
assert component_list[1].args == ("Ethel the Aardvark goes Quantity Surveying",)
152150

153151

154-
def test_get_components(parser, components):
152+
def test_get_components(parser: ComponentConfigurationParser, components: str) -> None:
155153
config = build_simulation_configuration()
156154
config.update(components)
157155

158156
parser.get_components(config.components)
159157

160158
calls = [call(path, args) for path, args in TEST_COMPONENTS_PREPPED]
161-
parser.import_and_instantiate_component.assert_has_calls(calls)
159+
# mypy is interpreting assert_has_calls as an attribute
160+
parser.import_and_instantiate_component.assert_has_calls(calls) # type: ignore[attr-defined]
162161

163162

164163
@pytest.mark.parametrize(
@@ -168,7 +167,9 @@ def test_get_components(parser, components):
168167
(TEST_COMPONENTS_NON_STRING, "should be a string, list, or dictionary"),
169168
],
170169
)
171-
def test_components_invalid_config(parser, config, error_message):
170+
def test_components_invalid_config(
171+
parser: ComponentConfigurationParser, config: str, error_message: str
172+
) -> None:
172173
bad_config = LayeredConfigTree(yaml.full_load(config))["components"]
173174
with pytest.raises(ParsingError, match=error_message):
174175
parser.parse_component_config(bad_config)

0 commit comments

Comments
 (0)