Raise an error when assigning floating point values to integer variables (fixes #1914) #1950
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 · 290 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.339
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1950"
general
weight: 0.35
mstimberg
Fixes #1914.
As discussed in the issue, assigning a floating point value to an
integervariable now raises aTypeErroron all targets, instead of truncating onnumpyand C++ standalone, failing with+=onnumpy, and failing to compile with Cython.run_regularly,on_pre, reset, ...):check_units_statementsnow also uses the Brian AST to get the type of the right-hand side, and raises if a float is assigned to an integer variable. In-place operations are checked on the expanded form, sox += fandx /= 2raise too. Temporary variables now keep the type of their expression, soa = 2thenx = astill works.VariableView.set_itemraises if a floating point value or array is assigned to an integer variable (e.g.G.x = [0.7, 1.4, 3.8]orG.x = 1.5). String expressions likeG.x = "f * 2"go through the statement check.Using
int(...)(or rounding the values first) works as before. Assigning a boolean expression to an integer variable is still allowed.This checks the type and not the value, like numpy 2.x does for indices, so
G.x = 2.0raises as well. Happy to relax that to only reject non-whole values if you prefer.The Kremer et al. (2011) example assigned float expressions to integer variables, so I wrapped them in
int(...). The results are unchanged (samebarrel_xandbarrel_idxvalues).Test
New
test_float_to_integer_assignmentintest_neurongroup.py: statements with=,+=,/=, a float literal and a float temporary variable, the different ways of setting the variable directly, and theint(...)/ boolean cases that should keep working.On macOS (arm64, Python 3.13.15, Cython 3.3.0, numpy 2.5.4):
DID NOT RAISE). With it, it passes.TypeErroronnumpy,cythonandcpp_standalone.numpy,cython,cpp_standalone, long tests and doctests): all passed (doctests 57, codegen-independent 462 + 2 skipped, numpy 328 + 10 skipped, cython 328 + 10 skipped, cpp_standalone 215 + 1 skipped single run, 17 multiple runs, 40 standalone-specific + 1 skipped).ruff format --check(v0.14.2) reports no changes for the changed files inbrian2/.