← Back to Pull requests

Raise an error when assigning floating point values to integer variables (fixes #1914) #1950

Closed
unknown closed this pull request without merging on Nov 18, 2026 16:37 UTC
View on GitHub
unknown unknown commented on Nov 18, 2026 05:28 UTC
Author

Fixes #1914.

As discussed in the issue, assigning a floating point value to an integer variable now raises a TypeError on all targets, instead of truncating on numpy and C++ standalone, failing with += on numpy, and failing to compile with Cython.

  • Statements (run_regularly, on_pre, reset, ...): check_units_statements now 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, so x += f and x /= 2 raise too. Temporary variables now keep the type of their expression, so a = 2 then x = a still works.

  • Setting values directly: VariableView.set_item raises if a floating point value or array is assigned to an integer variable (e.g. G.x = [0.7, 1.4, 3.8] or G.x = 1.5). String expressions like G.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.0 raises 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 (same barrel_x and barrel_idx values).

    Test

    New test_float_to_integer_assignment in test_neurongroup.py: statements with =, +=, /=, a float literal and a float temporary variable, the different ways of setting the variable directly, and the int(...) / boolean cases that should keep working.

    On macOS (arm64, Python 3.13.15, Cython 3.3.0, numpy 2.5.4):

  • Without the change, the new test fails (DID NOT RAISE). With it, it passes.

  • The example from the issue now raises the same TypeError on numpy, cython and cpp_standalone.

  • Full test suite (codegen independent, 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 in brian2/.
  • Diff: 290 lines (+217 / -73) across 3 files Reviewer: mstimberg Review duration: 668m
    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
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.9% (MEDIUM) · Higher = larger blast radius
    39.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.2% · Safe to merge
    17.2%