Insert numpy scalars as plain numbers when substituting values #1909
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.40) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 306 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.343
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1909"
general
weight: 0.35
mstimberg
Substituting a name with a numpy scalar fails with numpy 2:
All three substitution mechanisms insert
"(" + repr(value) + ")"into the code, and since numpy 2 the repr of a numpy scalar isnp.float64(0.37)instead of0.37. Brian requires numpy >= 2.3, so this happens for any value that comes out of numpy:np.exp(...),params[0],arr.mean(),rng.uniform(), and so on. ForStatementsthe bad code is only caught later, when the group is created or run. Quantities were not affected becauseQuantity.__repr__already prints a plain number (np.float64(10)*msworks).The change adds a small
value_to_codehelper inbrian2/equations/codestrings.pythat turns numpy scalars into Python scalars with.item()before taking the repr, and uses it inEquations._substitute,Statements._substituteandExpression._substitute. That is the same conversion Brian already does inVariableView.set_itemfor string indices and inCPPStandaloneDevice.fill_with_array. Arrays are left alone, soEquations(..., tau=np.arange(5))still raises aSyntaxErroras tested intest_wrong_replacements.Tests
test_codestrings.py::test_expr_substitutions:np.float64andnp.int64valuestest_statements.py::test_statements_numpy_scalar_substitution:np.float64,np.float32,np.int64andnp.bool_(exact string comparison, since the existinginchecks would also match(np.float64(0.3)))test_equations.py::test_correct_replacements: a numpy scalar, and a quantity built from a numpy scalarWithout the change in
codestrings.pyandequations.py, the new cases fail with the errors above; with it they pass.Local runs on macOS arm64, Python 3.13, numpy 2.5.4, through
brian2.test(["numpy"], test_codegen_independent=True): doctests 57 passed, codegen-independent 457 passed and 4 skipped, numpy target 305 passed and 15 skipped.ruff checkandruff format --check(v0.14.2, as in the pre-commit config) pass on the changed files. The change only touches code strings before code generation, so I did not run the Cython or standalone targets locally.