Skip to content

fix(model): preserve float type for model.time in _advance_time - #4057

Closed
sundeep8967 wants to merge 2 commits into
mesa:mainfrom
sundeep8967:fix/model-time-float-type-4051
Closed

sundeep8967 wants to merge 2 commits into
mesa:mainfrom
sundeep8967:fix/model-time-float-type-4051

Conversation

@sundeep8967

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

[x] This PR is a bug fix, not a new feature or enhancement.

Summary

Ensure model.time remains a float when time is advanced with integer values in run_until() or when events are scheduled at integer timestamps.

Bug / Issue

Fixes #4051

In Model._advance_time():

  • When events execute, self.time = event.time was assigned without casting.
  • Upon loop termination, self.time = until assigned until directly.
  • When an integer was passed to run_until(5) or scheduled at an integer time, self.time became an int, clobbering the annotated float type.
  • Because _wrapped_step() advances time via self.time + 1, subsequent steps remained int (5 + 1 = 6), causing mixed int/float types in outputs such as JSONDataRecorder.

Implementation

  • Cast until = float(until) and self.time = float(event.time) in mesa/model.py.
  • Added unit tests in tests/test_schedule_run.py covering run_until with integer times and scheduled events with integer timestamps, verifying that model.time retains float type and continues to advance as float.

Testing

  • Ran uv run pytest tests/test_schedule_run.py: 25 passed.
  • Ran uv run --with pre-commit pre-commit run --files mesa/model.py tests/test_schedule_run.py: all hooks passed (ruff check, ruff format, pyupgrade, trim trailing whitespace, codespell).

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.1% [-0.2%, +0.5%] 🔵 -2.0% [-2.2%, -1.9%]
BoltzmannWealth large 🔵 +0.5% [+0.1%, +0.9%] 🔵 +1.0% [+0.4%, +1.6%]
Schelling small 🔵 +0.4% [+0.1%, +0.8%] 🔵 +0.2% [-0.4%, +0.9%]
Schelling large 🔵 +0.3% [-0.3%, +0.9%] 🔴 +4.4% [+3.1%, +5.8%]
WolfSheep small 🔵 +0.4% [+0.0%, +0.8%] 🔵 -2.2% [-6.2%, +2.1%]
WolfSheep large 🔵 -0.7% [-1.2%, -0.2%] 🔵 -1.8% [-2.7%, -1.0%]
SugarscapeG1mt small 🔵 -0.7% [-1.1%, -0.4%] 🔵 -1.5% [-3.5%, +0.4%]
SugarscapeG1mt large 🔵 -1.6% [-2.9%, -0.3%] 🔵 -1.7% [-3.4%, -0.2%]
BoidFlockers small 🔵 -1.0% [-1.5%, -0.3%] 🔵 -0.8% [-1.1%, -0.4%]
BoidFlockers large 🔵 -2.7% [-3.4%, -1.9%] 🔵 -0.7% [-1.0%, -0.5%]
MultiLevelAllianceModel small 🔵 -2.4% [-3.0%, -1.8%] 🔵 -0.3% [-0.6%, -0.1%]
MultiLevelAllianceModel large 🔵 -1.0% [-2.6%, +0.6%] 🔵 -0.2% [-0.6%, +0.2%]

@arzaanxeng

Copy link
Copy Markdown
Contributor

Thanks for the clear explanation of the bug and for adding tests for both cases !

One question: the fix casts when model.time is assigned. Would it be better to also convert to a float when the event is created? In schedule_event, at is passed straight into Event(...), so event.time can still be an int (for example at=2). Any other code that reads event.time would still see an int.

@sundeep8967
sundeep8967 force-pushed the fix/model-time-float-type-4051 branch from dd190c4 to 20428a2 Compare November 9, 2026 21:32
@sundeep8967

Copy link
Copy Markdown
Contributor Author

Good catch @arzaanxeng! That makes complete sense. I have updated Event.__init__ in mesa/time/events.py to cast self.time = float(time) so that event.time is guaranteed to be a float from creation. Re-verified against all 100 event tests and schedule tests.

@github-actions github-actions Bot added the testing Release notes label label Nov 9, 2026
@mesa-triage-bot

Copy link
Copy Markdown

#4052 also fixes #4051, with conversion only when assigning model.time; this PR additionally normalizes Event.time at construction. @quaquel, should event timestamps also be normalized, and which PR should carry the fix?


Automated triage note, written by an AI model for the Mesa maintainers. It can be wrong, and it doesn't make decisions: maintainers do. If it missed the point, say so here.

Fixes mesa#4051

Ensure model.time remains a float when time is advanced with integer
values in run_until() or when events are scheduled at integer timestamps.
Previously, _advance_time directly assigned until and event.time without
casting, clobbering the annotated float type and propagating integer time
to subsequent step() calls and data recorders.

Signed-off-by: sundeep8967 <[email protected]>
@sundeep8967
sundeep8967 force-pushed the fix/model-time-float-type-4051 branch from 20428a2 to 082bee1 Compare November 10, 2026 07:11
@quaquel

quaquel commented Nov 10, 2026

Copy link
Copy Markdown
Member

With #4052 merged, I fail to see the relevance of this.

@quaquel quaquel closed this Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

model.time becomes an int after run_until or events scheduled at int times

3 participants