Make Cython directives configurable via preferences #1934
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 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.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
mstimberg
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 thecython_directivesmacro, so switching a single one — e.g. enablingcdivisionto match C semantics — meant editing the template.Closes #993.
What changed
brian2/codegen/runtime/cython_rt/cython_rt.pyDEFAULT_DIRECTIVESholding the directives we currently use.overwrite_directivespreference (dict, defaults to{}), documented as only affecting code generated after the change._get_cython_directives(), which returnsDEFAULT_DIRECTIVES | prefs.codegen.runtime.cython.overwrite_directives, and exposed it to the template viaCodeObject.env_globals.import brian2are respected (this was called out in the issue).brian2/codegen/runtime/cython_rt/templates/common_group.pyxcython_directivesmacro 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)PreferenceErrorraised for a non-dict value.Verification
pytest brian2/tests/test_cython_rt.py→ 4 passed.ruff checkandruff format --checkon both files → clean.generated code contains the expected lines and compiles/runs:
Notes / questions for review
1. Naming. The issue suggested exposing the function as
overwrite_directivesinenv_globals; I named the exposed globalget_cython_directivesso 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 intest_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.