Skip to content

Refactor: reduce the cognitive complexity of _difference_bit_map (SonarCloud S3776) - #672

Merged
peacker merged 2 commits into
feature/quasidifferential-implementationfrom
fix/quasidifferential-sonar-cognitive-complexity
Oct 23, 2026
Merged

peacker merged 2 commits into
feature/quasidifferential-implementationfrom
fix/quasidifferential-sonar-cognitive-complexity

Conversation

@peacker

@peacker peacker commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

SonarCloud reports 1 new issue on #515:

python:S3776 (Critical): smt_xor_quasidifferential_model.py:218, Refactor this function to reduce its Cognitive Complexity from 19 to the 15 allowed.

The function is SmtXorQuasidifferentialModel._difference_bit_map. Most of the score comes from the nested if/for loops of its SHIFT case.

Fix

Move the SHIFT handling into a new _shift_bit_map(output_bit_size, shift_amount) static helper, with doctests for left and right shifts. _difference_bit_map drops to a cognitive complexity of 10. The behaviour is unchanged.

Verification (local)

  • smt_xor_quasidifferential_model.py passes all 378 of its doctests, including the new _shift_bit_map examples.
  • The 44 quasidifferential pytest cases pass (smt_xor_quasidifferential_model_test.py and the *quasidifferential* tests in tests/unit/components/, including the shift component).

Also: race on ./test_reports in the report export tests

The x86_64 pytest job failed on this PR in report_test.py::test_save_as_dataframe with OSError: Cannot save file into a non-existent directory: '.../test_reports/...'. That's unrelated to the refactor above. test_save_as_latex_table, test_save_as_dataframe and test_save_as_json all exported into the shared default ./test_reports and ended with clean_reports(), which deletes it. Under xdist one test could delete the folder while another was still writing. Each test now exports into, and cleans, its own tmp_path folder, as test_save_as_image already does. The default-directory behaviour stays covered by the *_uses_runtime_cwd_default tests.

Checked locally: the dataframe, LaTeX and JSON exports write into the given folder, and clean_reports(output_dir=...) removes it. The full tests weren't run locally, because dieharder is too slow on the dev machine and TensorFlow isn't installed there.

🤖 Generated with Claude Code

… cognitive complexity

SonarCloud (python:S3776) flagged _difference_bit_map in the SMT quasidifferential
model with a Cognitive Complexity of 19 against the 15 allowed. Move the nested
SHIFT handling into a _shift_bit_map static helper, bringing it down to 10. The
behaviour is unchanged.
test_save_as_latex_table, test_save_as_dataframe and test_save_as_json all
exported into the default ./test_reports and each ended with clean_reports(),
which deletes that folder. Run in parallel by xdist, one test could delete the
folder while another was still writing into it (seen on the x86_64 runner as
'OSError: Cannot save file into a non-existent directory' in
test_save_as_dataframe). Export and clean into a per-test tmp_path folder
instead, as test_save_as_image already does.
@peacker
peacker merged commit ed2869d into feature/quasidifferential-implementation Oct 23, 2026
6 checks passed
@peacker
peacker deleted the fix/quasidifferential-sonar-cognitive-complexity branch October 23, 2026 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant