refactor(backend): enable ruff SIM102 and collapse nested if statements - #10315
Draft
ogenstad wants to merge 1 commit into
Draft
refactor(backend): enable ruff SIM102 and collapse nested if statements#10315ogenstad wants to merge 1 commit into
ogenstad wants to merge 1 commit into
Conversation
Remove the SIM102 (collapsible-if) suppressions from pyproject.toml - both the global `ignore` entry and the `backend/tests/fixtures/**.py` per-file entry - and fix the 38 violations that surfaced. All changes are structural: nested `if A: if B:` pairs become `if A and B:`, which short-circuits identically. No conditions, operands, branches or comments were altered. Two sites in core/node/__init__.py where the collapsed form wrapped a multi-line `await` in a walrus were destructured into an assignment plus a plain `if` instead, which satisfies the rule and reads better. Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
No issues found across 24 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Shadow auto-approve: would auto-approve. Refactor enables ruff SIM102 lint rule by collapsing nested ifs into equivalent compound conditions with verified zero behavioral change across 24 files.
Re-trigger cubic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
SIM102(collapsible-if) was suppressed in two places inpyproject.toml: the globalignorelist, and a
backend/tests/fixtures/**.pyper-file entry sitting under a "Refactor code and removethe ignore rule" header. Both suppressions are now gone and the rule is fully enforced.
Goal: enable the rule with zero behavioral change.
Non-goals: no logic changes, no restructuring beyond what the rule requires, no changes to the
other rules still listed in either ignore block.
What changed
Behavioral changes: none. Every edit is structural.
"SIM102"from the globalignorelist and from thebackend/tests/fixtures/**.pyper-file ignores.grep -c SIM102 pyproject.tomlis now 0.backend/infrahub/, 2 in fixtures) across 23 files.Nested
if A: if B:becomesif A and B:, which short-circuits identically.core/node/__init__.pywhere the collapsed form wrapped a multi-lineawaitin awalrus inside an
andwere instead destructured into an assignment plus a plainif. That alsosatisfies the rule (the outer body then holds two statements) and matches the idiom already used
a few lines above.
(A and B and C) and D- inattribute_parameters.pyandgroups/parsers.py. Noand-within-andoror-within-ornesting remains anywhere in the diff.
What stayed the same: no schema changes, no API changes, no dependency changes. Three comments
moved from inside an outer
ifto just above the merged condition; no comment was added, removed,or reworded.
How to review
Most of the diff is mechanical and was produced by
ruff check --fix. Worth actual attention:backend/infrahub/core/node/__init__.py- the two destructuredawaitblocks indelete(),and the multi-term guard chains in
_collect_extra_filters/process_label.backend/infrahub/core/schema/schema_branch.py-identify_required_object_templates, the onecondition mixing
orinsideand.elifwas collapsed (graphql/app.py,graphql/field_extractor.py,core/diff/enricher/cardinality_one.pyx2). Collapsing anelifis only sound when it is theterminal branch; each was verified to have no trailing
else.Two things reviewers should weigh in on:
operand. This PR introduces 14 across 8 files, 8 of which put the walrus in a later operand
(
if guard and (x := f()):), where the binding is conditional. It is correct - the name is onlyread inside the body, and the binding condition is unchanged from the nested original - but it is
a convention arriving via a lint sweep. The alternative is destructuring those 8 the way the
delete()blocks were. Happy to do that if preferred.infrahub-demo-edge. If thatfixture is ever re-synced from upstream the violation returns, and because the per-file ignore is
gone, CI will fail until it is re-applied. That is the intended tripwire rather than an
oversight; a follow-up cleanup issue on
opsmill/infrahub-demo-edgecould remove the friction.How to test
Beyond the standard checks, equivalence was proven mechanically rather than by eye. Both versions
of every changed file were canonicalised to a maximally-nested normal form (explode
and-chainsinto nested ifs; de-sugar sole-test walruses into assignment plus test; flatten same-operator
boolean nesting - all three behaviour-preserving), then compared as ASTs. All 23 files are
identical to their base version under that normal form, and comment token streams match exactly.
The canonicaliser was itself validated against 26 control cases, confirming it rejects
and→or,flipped comparisons, reordered operands, dropped negations, swallowed
elsebranches, non-terminalelifcollapses, dropped sibling statements,and/orregrouping, and - specifically for thedestructured blocks - an assignment hoisted outside its guard.
The edited fixture check was additionally differential-tested: old and new
validate()driventhrough 122 enumerated input combinations, comparing recorded
log_errorcalls. Zero mismatches,with 112 cases producing non-empty output.
Impact & rollout
pyproject.tomllint config only.Checklist