Repository navigation
Accept numpy integer and float scalars in AgentSet.select_random - #3906
Merged
Merged
Conversation
select_random validated n with isinstance(n, (int, float)). NumPy integer scalars are not int subclasses and np.float32 is not a float subclass, so a sample size computed with model.rng or numpy arithmetic raised TypeError, while select(at_most=np.int64(...)) worked. Validate against numbers.Integral / numbers.Real instead and normalize integral n to a Python int. bool and np.bool_ are still rejected. Fixes mesa#3905
|
Performance benchmarks:
|
quaquel
approved these changes
Oct 6, 2026
1 task done
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.
Pre-PR Checklist
Summary
AgentSet.select_random(n)raisedTypeErrorfor numpy integer scalars (np.int64,np.int32,np.uint8, ...) and fornp.float32fractions.Bug / Issue
Fixes #3905
The type check used
isinstance(n, (int, float)). NumPy integers aren'tintsubclasses, andnp.float32isn't afloatsubclass, so sample sizes coming frommodel.rng.integers(...)or numpy arithmetic were rejected.select(at_most=np.int64(...))already accepts them.Implementation
numbers.Realand branch onnumbers.Integral. NumPy registers its scalar types with both ABCs.nto a Pythonintbefore use.np.bool_alongsidebool, so the existing "booleans are not sizes" rule still holds for numpy booleans.Error messages and every other validation path are unchanged.
Testing
test_select_random_accepts_numpy_numbersis parametrized overnp.int64,np.int32,np.uint8,np.float64andnp.float32.np.float32cases fail before the change withTypeError.np.float64already passed because it subclassesfloat.test_select_random_rejects_numpy_boolchecks thatnp.True_still raisesTypeError.tests/test_agentset.py,tests/test_agent.pyandtests/test_model.py: 73 passed.ruff check/ruff format --checkclean.