From a2481a2c6df052ca5a35b39563fc9301681b8ec4 Mon Sep 17 00:00:00 2001 From: Steve Bachmeier <23350991+stevebachmeier@users.noreply.github.com> Date: Thu, 31 Oct 2024 17:22:22 -0600 Subject: [PATCH] fix type handling for clocktime and step_size (#522) --- CHANGELOG.rst | 3 ++- src/vivarium/framework/event.py | 19 ++++++++++++++++--- src/vivarium/types.py | 6 +++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 63eaf70b4..bb448297e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,9 +1,10 @@ -**3.0.16 - 10/31/24** +**3.0.16 - 10/30/24** - Bugfix to prevent a LookupTable from changing order of the value columns - Fix mypy errors in vivarium/framework/lookup/table.py - Typing changes in vivarium/framework/lookup/interpolation.py - Fix broken build from LayeredConfigTree typing + - Fix type handling for clock and step size in vivarium/framework/event.py **3.0.15 - 10/24/24** diff --git a/src/vivarium/framework/event.py b/src/vivarium/framework/event.py index d5805970b..075f2d816 100644 --- a/src/vivarium/framework/event.py +++ b/src/vivarium/framework/event.py @@ -30,7 +30,7 @@ from __future__ import annotations from collections.abc import Callable -from datetime import datetime +from datetime import datetime, timedelta from typing import TYPE_CHECKING, Any, NamedTuple import pandas as pd @@ -115,11 +115,24 @@ def emit(self, index: pd.Index[int], user_data: dict[str, Any] | None = None) -> """ if not user_data: user_data = {} + + clock = self.manager.clock() + step_size = self.manager.step_size() + event_time: ClockTime + if isinstance(clock, int) and isinstance(step_size, int): + event_time = clock + step_size + elif isinstance(clock, datetime) and isinstance(step_size, timedelta): + event_time = clock + step_size + else: + raise ValueError( + f"Clock ({type(clock)}) and step size ({type(step_size)}) are not compatible." + ) + e = Event( index, user_data, - self.manager.clock() + self.manager.step_size(), # type: ignore[operator, arg-type] - self.manager.step_size(), + event_time, + step_size, ) for priority_bucket in self.listeners: diff --git a/src/vivarium/types.py b/src/vivarium/types.py index cc9024553..5d813e312 100644 --- a/src/vivarium/types.py +++ b/src/vivarium/types.py @@ -19,8 +19,8 @@ float, int, ] -# todo need to use TypeVars here +# TODO: [MIC-5481] need to use TypeVars here Time = Union[pd.Timestamp, datetime] Timedelta = Union[pd.Timedelta, timedelta] -ClockTime = Union[Time, Number] -ClockStepSize = Union[Timedelta, Number] +ClockTime = Union[Time, int] +ClockStepSize = Union[Timedelta, int]