Skip to content

fix: handle deprecated space_kwargs gracefully in SpaceRenderer.rende…#3269

Merged
quaquel merged 3 commits intomesa:mainfrom
DipayanDasgupta:fix-spacerenderer-crash-3216
Feb 11, 2026
Merged

fix: handle deprecated space_kwargs gracefully in SpaceRenderer.rende…#3269
quaquel merged 3 commits intomesa:mainfrom
DipayanDasgupta:fix-spacerenderer-crash-3216

Conversation

@DipayanDasgupta
Copy link
Copy Markdown
Contributor

Summary

This PR adds a safeguard to SpaceRenderer.render to prevent a hard AttributeError crash when the deprecated space_kwargs argument is passed.

Bug / Issue

Closes #3216

Context:
Currently, if a user (or a legacy script/tutorial) passes space_kwargs to renderer.render(), the method blindly merges it into kwargs and passes it to the backend drawer.
What happened: Matplotlib raises AttributeError: Line2D.set() got an unexpected keyword argument 'space_kwargs' because the dictionary key wasn't unpacked.
What was expected: The renderer should handle the deprecated argument gracefully (by unpacking its contents) or at least not crash the entire application.

Implementation

Modified mesa/visualization/space_renderer.py inside the render method:

  1. Checked if space_kwargs exists in kwargs.
  2. Extracted the dictionary value and merged it into self.draw_space_kwargs (which is the correct destination for these parameters).
  3. Removed space_kwargs from kwargs to prevent it from being passed downstream to the backend (Matplotlib/Altair) where it causes the crash.

Testing

I created a local reproduction script to verify the fix:

Reproduction Code:

renderer.render(space_kwargs={"color": "black", "alpha": 0.1})

Results:

  • Before: Crashed with AttributeError: Line2D.set() got an unexpected keyword argument 'space_kwargs'.
  • After: Renders successfully without error.

Additional Notes

While PR #3223 and #3231 updated the tutorials to use the new API, the underlying library code remained fragile. This fix ensures that users who copy-paste old code or haven't updated their scripts yet will encounter a warning rather than a hard crash, improving the DevEx.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 9, 2026

Performance benchmarks:

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

@Sahil-Chhoker
Copy link
Copy Markdown
Collaborator

It can also contain agent_kwargs(check the documentation for exact name), can you add it's extraction as well?

@DipayanDasgupta DipayanDasgupta force-pushed the fix-spacerenderer-crash-3216 branch from 107eaf6 to 14abde7 Compare February 10, 2026 12:25
@DipayanDasgupta
Copy link
Copy Markdown
Contributor Author

@Sahil-Chhoker Added it as you suggested.

@Sahil-Chhoker
Copy link
Copy Markdown
Collaborator

@DipayanDasgupta I'm seeing indentation problems in your code, fix that please.

@quaquel quaquel added the enhancement Release notes label label Feb 10, 2026
@souro26
Copy link
Copy Markdown
Contributor

souro26 commented Feb 10, 2026

A small suggestion i had is since we’re now handling multiple deprecated kwargs (space_kwargs, agent_kwargs), it might be cleaner to extract them in a single pass before updating draw_space_kwargs. , something like

deprecated = {
    "space_kwargs": self.draw_space_kwargs,
    "agent_kwargs": self.draw_agent_kwargs,
}
for key, target in deprecated.items():
    if key in kwargs:
        val = kwargs.pop(key)
        if isinstance(val, dict):
            target.update(val)

This keeps the logic simple and avoids adding more conditional blocks as new legacy kwargs come up.

@DipayanDasgupta DipayanDasgupta force-pushed the fix-spacerenderer-crash-3216 branch from 14abde7 to 69cc2f9 Compare February 10, 2026 17:50
@DipayanDasgupta
Copy link
Copy Markdown
Contributor Author

@Sahil-Chhoker @souro26 Thank you both for the constructive feedback! I've updated the PR to address all the points raised:

  1. Completeness: Added handling for agent_kwargs alongside space_kwargs as requested.
  2. Refactor: Adopted the dictionary loop approach suggested by @souro26 to handle these deprecated arguments cleanly and scalably.
  3. Style & Linting: Corrected the indentation issues and resolved a ruff docstring error (missing self description) that was blocking the pre-commit hook.

The implementation is now much cleaner and fully robust against legacy kwargs. Please let me know if there are any other improvements needed before merging!

Copy link
Copy Markdown
Collaborator

@Sahil-Chhoker Sahil-Chhoker left a comment

Choose a reason for hiding this comment

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

Thanks @DipayanDasgupta for the PR and @souro26 for helping in the logic, just a couple comments after that this PR is good to go from my side.

"""Render the complete space with structure, agents, and property layers.

Args:
self: The SpaceRenderer instance.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Remove this comment, self is automatically passed.

@DipayanDasgupta DipayanDasgupta force-pushed the fix-spacerenderer-crash-3216 branch from 1358976 to 672e4ad Compare February 11, 2026 06:46
@DipayanDasgupta DipayanDasgupta force-pushed the fix-spacerenderer-crash-3216 branch from 672e4ad to 618db48 Compare February 11, 2026 07:09
@DipayanDasgupta
Copy link
Copy Markdown
Contributor Author

@quaquel @Sahil-Chhoker I've made the recommended changes , if there's anything else to be done before its good to merge, please do let me know.

@quaquel quaquel merged commit f5fef2f into mesa:main Feb 11, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Property Layer Visualization Example AttributeError: 'space_kwargs'

4 participants