Fix PropertyLayer default value type mismatch warnings#2963
Merged
Fix PropertyLayer default value type mismatch warnings#2963
Conversation
Replace isinstance() check with NumPy-aware dtype compatibility checking. The previous implementation incorrectly warned when default_value was compatible with the dtype (e.g., float 10.0 with dtype=float or np.float64). The fix normalizes dtypes using np.dtype() to handle both Python types (int, float) and NumPy types (np.int64, np.float64), and tests actual conversion instead of type checking. Warnings are now only raised for actual incompatibilities or precision loss. Fixes false warnings in: - tests/test_space_renderer.py - tests/test_solara_viz.py - tests/test_solara_viz_updated.py - tests/test_components_matplotlib.py
|
Performance benchmarks:
|
Replace isinstance() check with NumPy-aware dtype compatibility checking. The previous implementation incorrectly warned when default_value was compatible with the dtype (e.g., float 10.0 with dtype=float or np.float64). The fix normalizes dtypes using np.dtype() to handle both Python types (int, float) and NumPy types (np.int64, np.float64), and tests actual conversion instead of type checking. Warnings are now only raised for actual incompatibilities or precision loss. Fixes false warnings in: - tests/test_space_renderer.py:46 - tests/test_solara_viz.py:121 - tests/test_solara_viz_updated.py:136 - tests/test_components_matplotlib.py:240 Updated test_discrete_space.py::test_property_layer_errors to test a legitimate warning case (precision loss) instead of a false positive.
quaquel
reviewed
Dec 18, 2025
quaquel
reviewed
Dec 18, 2025
quaquel
reviewed
Dec 18, 2025
Member
quaquel
left a comment
There was a problem hiding this comment.
I have 2 questions, but looks fine to me otherwise
f5ddfad to
1936cf8
Compare
Member
|
Can you also check why builds are failing now? |
Contributor
Author
I need to update tests, working on it. |
Replace isinstance() check with NumPy-aware dtype compatibility checking. The previous implementation incorrectly warned when default_value was compatible with the dtype (e.g., float 10.0 with dtype=float or np.float64). The fix normalizes dtypes using np.dtype() to handle both Python types (int, float) and NumPy types (np.int64, np.float64), and tests actual conversion instead of type checking. Warnings are now only raised for actual incompatibilities or precision loss. Fixes false warnings in: - tests/test_space_renderer.py - tests/test_solara_viz.py - tests/test_solara_viz_updated.py - tests/test_components_matplotlib.py
11ba09e to
bf08639
Compare
12 tasks
Refactored the dtype validation logic to be more concise and readable: - Reduced code from ~20 lines to ~8 lines per location - Removed intermediate np.dtype() wrapper and array creation - Simplified precision loss detection using direct type conversion - Maintained all functionality: incompatible types raise TypeError, precision loss triggers UserWarning The new implementation uses dtype(default_value) directly and compares the result to detect any conversion issues, making the logic clearer and eliminating code duplication between property_layer.py and space.py.
EwoutH
approved these changes
Dec 21, 2025
Member
EwoutH
left a comment
There was a problem hiding this comment.
Made a few modifications to reduce the complexity.
The current dtype system is not ideal, we might want to handle that differently (automatically) in the future.
Thanks!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixed false warnings in
PropertyLayerwhen default_value is compatible with the specified dtype. The previous implementation usedisinstance()checks that incorrectly flagged compatible values (e.g.,float 10.0withdtype=floatornp.float64) as type mismatches. This fix eliminates ~4 false warnings in test files while maintaining proper warnings for actual incompatibilities and precision loss cases.Bug / Issue
The
PropertyLayerclass was generating falseUserWarningmessages about dtype mismatches when the default_value was actually compatible with the specified dtype. This occurred because:Root Cause: The code used
isinstance(default_value, dtype)which doesn't work correctly with NumPy dtypes. For example:isinstance(10.0, float)returnsTrue(correct)isinstance(10.0, np.float64)returnsFalse(incorrect, even though 10.0 is compatible)Impact: This caused false warnings in multiple test files:
tests/test_space_renderer.py:46-PropertyLayer("test", [2, 2], default_value=0, dtype=int)tests/test_solara_viz.py:121-PropertyLayer(..., default_value=10.0, dtype=float)tests/test_solara_viz_updated.py:136-PropertyLayer(..., default_value=10.0, dtype=float)tests/test_components_matplotlib.py:240-PropertyLayer(..., default_value=0, dtype=int)Expected Behavior: Warnings should only appear for actual incompatibilities (e.g., string -> int) or precision loss (e.g.,
10.5->int).Implementation
Replaced the
isinstance()-based type checking with NumPy-aware dtype compatibility checking in bothPropertyLayerimplementations:Files Modified:
mesa/discrete_space/property_layer.pymesa/space.pytests/test_discrete_space.pyKey Changes:
Dtype Normalization: Use
np.dtype(dtype)to normalize both Python types (int,float) and NumPy types (np.int64,np.float64) to NumPy dtype objects.Conversion Testing: Instead of type checking, attempt actual conversion using
np.array([default_value], dtype=dtype_obj)to verify compatibility.Precision Loss Detection: Added specific check for precision loss when converting float -> int with decimal values (e.g.,
10.5->int).Error Handling: Only warn when conversion fails (
ValueError,TypeError,OverflowError) or when precision would be lost.Testing
Manual Testing:
Created and ran test cases to verify the fix works correctly:
default_value=0, dtype=int-> No warning (compatible)default_value=10.0, dtype=float-> No warning (compatible)default_value=0, dtype=float-> No warning (compatible)default_value=10.0, dtype=np.float64-> No warning (compatible)default_value=0, dtype=np.int64-> No warning (compatible)default_value=10.5, dtype=int-> Warns correctly (precision loss)Affected Test Files:
The following test files should no longer generate false warnings:
tests/test_space_renderer.py:46tests/test_solara_viz.py:121tests/test_solara_viz_updated.py:136tests/test_components_matplotlib.py:240Verification:
PropertyLayerimplementations fixed consistentlyAdditional Notes
PropertyLayer.PropertyLayerimplementations (mesa.space.PropertyLayerandmesa.discrete_space.PropertyLayer) were updated to use the same logic for consistency.