Skip to content

Improve type annotations in mesa.time.event#3419

Merged
quaquel merged 3 commits intomesa:mainfrom
Tushar1733:improve-event-typing-clean
Mar 3, 2026
Merged

Improve type annotations in mesa.time.event#3419
quaquel merged 3 commits intomesa:mainfrom
Tushar1733:improve-event-typing-clean

Conversation

@Tushar1733
Copy link
Copy Markdown
Contributor

This PR refines type annotations in the mesa.time.event module.

Changes include:

  • Adding explicit return types for getstate , setstate and other methods
  • Refining Callable annotations to Callable[..., Any]
  • Ensuring consistency across Event and EventGenerator

No functional changes were made.

This replaces #3412 with a clean branch containing only the intended changes.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 3, 2026

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.4% [-0.8%, +0.1%] 🔵 -2.7% [-3.2%, -2.1%]
BoltzmannWealth large 🔵 +0.2% [-0.8%, +1.0%] 🔵 -1.5% [-3.9%, +0.6%]
Schelling small 🔵 +0.3% [+0.0%, +0.7%] 🔵 -0.2% [-0.3%, -0.0%]
Schelling large 🔵 -0.8% [-1.6%, -0.1%] 🔵 -1.8% [-2.7%, -0.6%]
WolfSheep small 🔵 -0.2% [-0.5%, +0.1%] 🔵 -0.4% [-0.6%, -0.2%]
WolfSheep large 🔵 +0.6% [-0.4%, +1.6%] 🔵 +0.9% [+0.4%, +1.4%]
BoidFlockers small 🔵 -1.3% [-1.7%, -0.9%] 🔵 -2.2% [-2.3%, -2.0%]
BoidFlockers large 🔵 -0.9% [-1.3%, -0.5%] 🔵 -1.2% [-1.5%, -0.9%]

# Filter out canceled events and get n smallest in correct chronological order
return nsmallest(n, (e for e in self._events if not e.CANCELED))
valid_events = [e for e in self._events if not e.CANCELED]
return nsmallest(n, valid_events)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change reintroduces the intermediate list in peek_ahead. In #3413 we switched to a generator expression to avoid the extra allocation and improve performance. We should keep that version here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out — you're right.
I’ll restore the generator expression to preserve the optimization from #3413.

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Mar 3, 2026

This replaces #3412 with a clean branch containing only the intended changes.

We typically use squash and merge, so there is no need to create a clean PR.

self,
time: int | float,
function: Callable,
function: Callable[..., Any],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return of the callable is ignored, so there in principle should not be a return from the callable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, since the return value is ignored, using Callable[..., None] is more appropriate. I've updated the annotation accordingly.

@Tushar1733
Copy link
Copy Markdown
Contributor Author

This replaces #3412 with a clean branch containing only the intended changes.

We typically use squash and merge, so there is no need to create a clean PR.

Understood, thanks for clarifying.

@quaquel quaquel added the docs Release notes label label Mar 3, 2026
@quaquel quaquel merged commit 8931c85 into mesa:main Mar 3, 2026
15 checks passed
@Tushar1733 Tushar1733 deleted the improve-event-typing-clean branch March 5, 2026 03:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants