Callable Schedule interval can return 0 in EventGenerator, bypassing validation
Describe the bug
Schedule validates fixed numeric intervals with interval <= 0 (must be strictly > 0), but EventGenerator._get_interval() only rejects callable interval results when < 0.
This means a callable interval returning 0 is accepted, which is inconsistent with fixed-interval validation and can schedule events at the same simulation time repeatedly.
Expected behavior
Callable and non-callable intervals should follow the same rule: interval must be strictly greater than 0.
If a callable returns 0 (or negative), a ValueError should be raised.
To Reproduce
- Create a model.
- Create a
Schedulewith a callable interval that returns0. - Start an
EventGeneratorusing that schedule. - Observe that no
ValueErroris raised for interval0.
Minimal repro:
from mesa.time.events import EventGenerator, EventList, Schedule
class DummyModel:
def __init__(self):
self.time = 0.0
self._event_list = EventList()
self._event_generators = set()
def noop():
pass
# Case 1: fixed interval=0 is correctly rejected.
try:
Schedule(interval=0)
print("fixed interval=0 -> NOT rejected (unexpected)")
except ValueError as exc:
print(f"fixed interval=0 -> rejected (expected): {exc}")
# Case 2: callable interval returning 0 currently passes validation.
schedule = Schedule(interval=lambda _m: 0)
generator = EventGenerator(DummyModel(), noop, schedule)
interval = generator._get_interval()
print(f"callable interval returns: {interval}")
if interval == 0:
print("BUG VERIFIED: callable interval can return 0")
else:
print("No bug reproduced")
Current code:
def _get_interval(self) -> float | int:
"""Get the next interval value."""
if callable(self.schedule.interval):
interval = self.schedule.interval(self.model)
if interval < 0:
raise ValueError(f"Interval must be > 0, got {interval}")
return interval
return self.schedule.interval
proposed solution :
def _get_interval(self) -> float | int:
"""Get the next interval value."""
if callable(self.schedule.interval):
interval = self.schedule.interval(self.model)
if interval <= 0: #changed here
raise ValueError(f"Interval must be strictly > 0, got {interval}")
return interval
return self.schedule.interval
I’ll be opening a PR shortly; the implementation is ready, and any feedback or suggestions are welcome
I disagree. In event scheduling, when drawing time between arrivals, having a 0 can be perfectly acceptable. An example is a negative binomial arrival process.