Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add helper function for type error assertion #351

Merged
merged 8 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions model/src/pyrenew/latent/infection_initialization_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pyrenew.latent.infection_initialization_method import (
InfectionInitializationMethod,
)
from pyrenew.metaclass import RandomVariable, SampledValue
from pyrenew.metaclass import RandomVariable, SampledValue, _assert_type


class InfectionInitializationProcess(RandomVariable):
Expand Down Expand Up @@ -74,18 +74,12 @@ def validate(
-------
None
"""
if not isinstance(I_pre_init_rv, RandomVariable):
raise TypeError(
"I_pre_init_rv must be an instance of RandomVariable"
f"Got {type(I_pre_init_rv)}"
)
if not isinstance(
infection_init_method, InfectionInitializationMethod
):
raise TypeError(
"infection_init_method must be an instance of InfectionInitializationMethod"
f"Got {type(infection_init_method)}"
)
_assert_type("I_pre_init_rv", I_pre_init_rv, RandomVariable)
_assert_type(
"infection_init_method",
infection_init_method,
InfectionInitializationMethod,
)

def sample(self) -> tuple:
"""Sample the Infection Initialization Process.
Expand Down
30 changes: 30 additions & 0 deletions model/src/pyrenew/metaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,36 @@
from pyrenew.transformation import Transform


def _assert_type(arg_name: str, value, expected_type) -> None:
"""
Matches TypeError arising during validation

Parameters
----------
arg_name : str
Name of the argument
value : object
The object to be validated
expected_type : type
The expected object type

Raises
-------
TypeError
If `value` is not an instance of `expected_type`.

Returns
-------
None
"""

if not isinstance(value, expected_type):
raise TypeError(
f"{arg_name} must be an instance of {expected_type}. "
f"Got {type(value)}"
)


def _assert_sample_and_rtype(
rp: "RandomVariable", skip_if_none: bool = True
) -> None:
Expand Down
37 changes: 37 additions & 0 deletions model/src/test/test_assert_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# numpydoc ignore=GL08

import numpyro.distributions as dist
import pytest
from pyrenew.metaclass import DistributionalRV, RandomVariable, _assert_type


def test_valid_assertion_types():
"""
Test valid assertion types in _assert_type.
"""

values = [
5,
"Hello",
(1,),
DistributionalRV(name="rv", dist=dist.Beta(1, 1)),
]
arg_names = ["input_int", "input_string", "input_tuple", "input_rv"]
input_types = [int, str, tuple, RandomVariable]

for arg, value, input in zip(arg_names, values, input_types):
_assert_type(arg, value, input)


def test_invalid_assertion_types():
"""
Test invalid assertion types in _assert_type.
"""

values = [None] * 4
arg_names = ["input_int", "input_string", "input_tuple", "input_rv"]
input_types = [int, str, tuple, RandomVariable]

for arg, value, input in zip(arg_names, values, input_types):
with pytest.raises(TypeError):
_assert_type(arg, value, input)