Skip to content

Reject non-positive callable Schedule intervals in EventGenerator - #3729

Merged
quaquel merged 1 commit into
mesa:mainfrom
AshNicolus:fix-callable-interval-validation
Jun 8, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
AshNicolus:fix-callable-interval-validation

Conversation

@AshNicolus

Copy link
Copy Markdown
Contributor

What

Schedule(interval=0) is rejected at construction (interval must be > 0), but
a callable interval that returns 0 was accepted at runtime. The check in
EventGenerator._get_interval used interval < 0, while its error message says
must be > 0, so 0 passed through.

An interval of 0 schedules the next recurring event at the current time, which
can cause repeated same-timestamp rescheduling, so it should be rejected just like
a fixed interval of 0.

Reproduction

from mesa import Model
from mesa.time import Schedule, EventGenerator

def cb():
    pass

Schedule(interval=0)            # ValueError: interval must be > 0   (correct)

m = Model(rng=1)
gen = EventGenerator(m, cb, Schedule(interval=lambda model: 0))
gen._get_interval()             # returns 0  (inconsistent: should raise)

Fix

Changed the guard in _get_interval from interval < 0 to interval <= 0, so a
callable interval follows the same > 0 rule as a fixed interval. The existing
error message is already correct.

Test

Added test_rejects_nonpositive_callable_interval: a callable returning 0 or a
negative value raises ValueError, and a positive value is returned unchanged.
Full tests/time/ suite passes under -Werror; ruff clean.

@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.5% [-0.8%, -0.2%] 🔵 -0.2% [-0.4%, +0.1%]
BoltzmannWealth large 🔵 +0.0% [-0.4%, +0.4%] 🔵 -0.1% [-0.4%, +0.2%]
Schelling small 🔵 +0.1% [-0.4%, +0.6%] 🔵 +0.7% [-0.3%, +1.6%]
Schelling large 🔵 +0.3% [-0.0%, +0.5%] 🔵 +0.7% [-0.5%, +1.9%]
WolfSheep small 🔵 +0.3% [-0.1%, +0.6%] 🔵 -1.2% [-5.4%, +3.0%]
WolfSheep large 🔵 -0.5% [-1.3%, +0.2%] 🔵 +0.3% [-0.5%, +0.9%]
SugarscapeG1mt small 🔵 -0.5% [-0.7%, -0.4%] 🔵 -1.5% [-3.2%, +0.4%]
SugarscapeG1mt large 🔵 -0.4% [-0.9%, +0.1%] 🔵 +1.2% [-1.6%, +4.3%]
BoidFlockers small 🔵 +0.0% [-0.3%, +0.3%] 🔵 -2.0% [-2.4%, -1.5%]
BoidFlockers large 🔵 +0.3% [+0.0%, +0.6%] 🔵 -1.2% [-1.4%, -0.9%]

@quaquel quaquel added the bug Release notes label label Jun 4, 2026
@quaquel
quaquel merged commit 4385fba into mesa:main Jun 8, 2026
13 checks passed
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants