Fix output_transitions for Python 3.13+ (PEP 667)#57
Fix output_transitions for Python 3.13+ (PEP 667)#57xuan-w wants to merge 3 commits intoCASCI-lab:masterfrom
Conversation
Python 3.13 changed locals() semantics (PEP 667): bare exec() inside a function now writes to a snapshot of locals that eval() cannot see. This broke loading of models that use the "logical" file format (e.g. BREAST_CANCER, LEUKEMIA) because output_transitions() relied on exec() to create variables that eval() would read. Replace exec()/eval() with direct dict assignment and an explicit namespace, which works on all Python versions. Also rename the loop variable from `input` (shadowing the builtin) to `input_name`. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Cover the code path fixed in the previous commit: - test_output_transitions: truth tables for AND, OR, OR+NOT gates - test_from_string_boolean: parse a 3-node network from boolean rules - test_load_logical_LEUKEMIA: load a real model in logical format Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
There was a problem hiding this comment.
Pull request overview
Fixes Boolean-rule model loading on Python 3.13+ by updating output_transitions() to avoid relying on function locals mutated via exec() (affected by PEP 667), and adds regression/unit tests around logical rule parsing and dataset loading.
Changes:
- Update
cana.utils.output_transitions()to evaluate expressions using an explicit namespace dict (PEP 667-compatible). - Add unit tests for
output_transitions()truth tables andBooleanNetwork.from_string_boolean()parsing. - Add a regression test that loads the logical-format
LEUKEMIAmodel.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
cana/utils.py |
Reworks output_transitions() evaluation to be Python 3.13+ compatible by using an explicit namespace for eval(). |
tests/test_boolean_network.py |
Adds regression/unit tests covering truth table generation, boolean-rule parsing, and loading the logical-format LEUKEMIA dataset. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Pass {"__builtins__": {}} to eval() to disable builtin functions
- Pre-compile eval_line with compile() to avoid re-parsing each iteration
- Update docstring to clarify trust assumption and current implementation
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
994f2fc to
cad2444
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@xuan-w can you briefly explain what is this fix? The function output_transitions() seems to be important for STG related analysis, so I would like to make sure that we have some backwards compatibility implemented so that it won't break the existing codes just for the sake of updating. |
|
To answer your questions
|
Summary
python 3.13's change (PEP 667) breaks existing code to load models from logical expressions (not cnet LUT).
output_transitions()incana/utils.pyto use an explicit namespace dict forexec()/eval(), resolving a bug where Python 3.13+ (PEP 667) snapshots locals making dynamically created variables invisible toeval()output_transitions,from_string_booleanparsing, and loading the LEUKEMIA logical-format modelTest plan
test_output_transitions— verifies truth table generation for basic logical expressionstest_from_string_boolean— verifies parsing of logical boolean rulestest_load_logical_LEUKEMIA— verifies loading a real biological model in logical format🤖 Generated with Claude Code