← Back to Pull requests

Make Cython directives configurable via preferences #1934

Open
unknown opened this pull request with 381 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 06, 2026 15:42 UTC
Author

Summary

Makes the Cython directives used for generated code configurable through a new preference, codegen.runtime.cython.overwrite_directives, as sketched in https://github.com/brian-team/brian2/issues/993#issuecomment-2743002345.

Previously the six directives we use (language_level, boundscheck, wraparound, cdivision, cpow, infer_types) were hard-coded in the cython_directives macro, so switching a single one — e.g. enabling cdivision to match C semantics — meant editing the template.

Closes #993.

What changed

brian2/codegen/runtime/cython_rt/cython_rt.py

  • Added DEFAULT_DIRECTIVES holding the directives we currently use.

  • Added the overwrite_directives preference (dict, defaults to {}), documented as only affecting code generated after the change.

  • Added _get_cython_directives(), which returns DEFAULT_DIRECTIVES | prefs.codegen.runtime.cython.overwrite_directives, and exposed it to the template via CodeObject.env_globals.

  • It is a function rather than a module-level dict so that preferences set after import brian2 are respected (this was called out in the issue).
  • brian2/codegen/runtime/cython_rt/templates/common_group.pyx

  • The cython_directives macro now renders the directives from that dictionary instead of hard-coding them. String values are emitted unquoted (so {'language_level': '3str'} produces #cython: language_level=3str, which Cython accepts).
  • brian2/tests/test_cython_rt.py (new)

  • Covers: default directives present, single override applied without disturbing the others, string-valued directive emitted unquoted, and PreferenceError raised for a non-dict value.
  • Verification

  • pytest brian2/tests/test_cython_rt.py → 4 passed.

  • ruff check and ruff format --check on both files → clean.

  • Ran an actual simulation through the Cython runtime with overrides set:
  • prefs.codegen.runtime.cython.overwrite_directives = {'cdivision': True, 'language_level': '3str'}
    G = NeuronGroup(10, 'dv/dt = -v/(10*ms) : 1', threshold='v>1', reset='v=0', method='euler',
                    codeobj_class=CythonCodeObject)
    run(5*ms)
    

    generated code contains the expected lines and compiles/runs:

    #cython: language_level=3str
    #cython: boundscheck=False
    #cython: wraparound=False
    #cython: cdivision=True
    #cython: cpow=True
    #cython: infer_types=True
    

    Notes / questions for review

    1. Naming. The issue suggested exposing the function as overwrite_directives in env_globals; I named the exposed global get_cython_directives so it reads as a function in the template. Happy to rename if you prefer the original.
    2. Test location. I put the tests in a new brian2/tests/test_cython_rt.py; the existing Cython-related tests live in test_codegen.py. Say the word and I'll move them there instead.
    3. Documentation. The preference's docs= string should surface in the auto-generated preferences documentation; I did not touch the Sphinx sources. Let me know if an explicit section somewhere would be better.

    Diff: 381 lines (+285 / -96) across 4 files Reviewer: mstimberg Review duration: 38959m
    All Vouch review confidence checks passed
    Residual risk 0.35 = Change Risk (0.66) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.66 · 381 lines diff across 4 files
    0.66
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    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.356
    Shallow Review Depth shallow_review_depth
    0.344
    Sensitive Paths sensitive_paths
    0.066
    Real PR Conversation Timeline
    1 comments
    "Review active on #1934"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 46.7% reviewed sensitive_module
    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: 66.1% (HIGH) · Higher = larger blast radius
    66.1%
    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: 46.7% · Lower = rushed or shallow review
    46.7%
    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: 35.2% · Moderate risk
    35.2%