Skip to content

Fix: Replace 'assert ValueError' with proper return None#3189

Merged
quaquel merged 2 commits intomesa:mainfrom
codebyNJ:fix/assert-valueerror-bug
Jan 26, 2026
Merged

Fix: Replace 'assert ValueError' with proper return None#3189
quaquel merged 2 commits intomesa:mainfrom
codebyNJ:fix/assert-valueerror-bug

Conversation

@codebyNJ
Copy link
Copy Markdown
Contributor

Summary

Fixed a no-op assertion in the DataCollector test that masked intended behavior for invalid reporter input; ensures the reporter returns None for invalid input instead of silently doing nothing.

Bug / Issue

The MockModel.test_model_calc_comp test used assert ValueError in an else branch (a no-op since is truthy). This meant the failure case (division by zero) was not handled as intended and could hide problems. Expected: the reporter returns None for invalid input; Actual: the code did nothing meaningful.

Implementation

Modified test_datacollector.py (MockModel.test_model_calc_comp)

  • Replaced assert ValueError with a comment.
  • Explicitly return None.

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +2.8% [+1.2%, +4.4%] 🔵 +0.5% [+0.3%, +0.6%]
BoltzmannWealth large 🔵 +2.7% [-7.8%, +14.2%] 🔴 +15.5% [+12.2%, +19.2%]
Schelling small 🔵 +1.5% [+0.7%, +2.2%] 🔵 +2.5% [+1.8%, +3.2%]
Schelling large 🔵 -2.8% [-7.4%, +2.0%] 🔵 -3.2% [-12.0%, +5.2%]
WolfSheep small 🔵 -0.5% [-2.9%, +1.8%] 🔵 +0.4% [-0.2%, +1.0%]
WolfSheep large 🔵 +2.5% [-8.7%, +15.3%] 🔵 -3.1% [-7.3%, +0.7%]
BoidFlockers small 🔵 +0.2% [-0.3%, +0.8%] 🔵 +0.2% [-0.1%, +0.5%]
BoidFlockers large 🔵 -0.8% [-2.0%, +0.4%] 🔵 -0.2% [-0.7%, +0.2%]

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Jan 26, 2026

I am sorry, but I don't understand the test code itself. This changes the return in a MockModel for one of the model reporters. But if a value is invalid, should that not always raise an exception and thus be handled inside the data collector?

To be clear, the current code makes no sense to me. Jus trying to understand the wider context within which this PR is being used.

@codebyNJ
Copy link
Copy Markdown
Contributor Author

Let me clarify:

The original code has a bug: assert ValueError is a no-op. It doesn't raise an exception - it just checks that the ValueError class is truthy (which it always is). The code was always returning None, the assert line did nothing.

The test expects None: Line 165-166 explicitly asserts that model_calc_fail returns None:

for element in data_collector.model_vars["model_calc_fail"]:
    assert element is None

DataCollector doesn't handle exceptions: Looking at datacollection.py:378, there's no try/except around reporter calls. If a reporter raises an exception, it would crash the simulation.

My PR just removes the no-op: I'm not changing the design - just removing the useless assert ValueError and making the intent clear with a comment.

However, you raise a valid design question: Should DataCollector catch reporter exceptions and store None (or some error value) instead? That would be a separate enhancement. My PR just fixes the misleading no-op assertion in the existing test.

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Jan 26, 2026

However, you raise a valid design question: Should DataCollector catch reporter exceptions and store None (or some error value) instead? That would be a separate enhancement. My PR just fixes the misleading no-op assertion in the existing test.

Thanks for the clarification. On this, let's not touch the current data collector and instead focus on the emerging ideas arround dataset and collector. I am going to merge this pr in the meantime.

@quaquel quaquel added the testing Release notes label label Jan 26, 2026
@quaquel quaquel merged commit 87e15aa into mesa:main Jan 26, 2026
12 of 13 checks passed
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.

2 participants