Skip to content

Remove dead SIM108 ignore for middle_earth; showcase ternaries in mettsim#129

Merged
MImmesberger merged 5 commits into
mainfrom
remove-dead-sim108-ignore
Jun 7, 2026
Merged

Remove dead SIM108 ignore for middle_earth; showcase ternaries in mettsim#129
MImmesberger merged 5 commits into
mainfrom
remove-dead-sim108-ignore

Conversation

@MImmesberger

@MImmesberger MImmesberger commented Jun 6, 2026

Copy link
Copy Markdown
Member

Three related changes, following the discovery in ttsim-dev/gettsim#1178 (comment) that the vectorizer handles ternary expressions (ast.IfExpxnp.where via _ifexp_to_call):

  1. Remove the dead SIM108 ignore for src_mettsim/mettsim/middle_earth/**/*.py. The ignore never suppressed anything: SIM108 only flags assignment-style if/else (if c: x = a / else: x = b; verified on ruff 0.13.0, stable and preview alike), while mettsim's if/else blocks are return-style — which no ruff rule converts to a ternary. The single assignment-style block (orc bounty) exceeds the line limit, where SIM108 auto-skips.

  2. Convert two return-style if/else to ternaries (wealth tax, child tax credit) so that mettsim showcases ternaries in real policy functions — these are swept by test_convertible for all policy years. Only sites where the ternary is a genuine short one-liner are converted: mettsim doubles as test material for the vectorizer, so it deliberately keeps exercising both supported styles. Vectorization of ternaries was already unit-tested via f2/f6/f7/f10 in tests/tt/test_vectorization.py.

  3. Narrow mettsim's blanket RET ignore to RET505. Only RET505 (superfluous else after return) conflicts with the vectorizer: its autofix produces the early-return form (if c: return a + bare return b) that _if_to_call rejects by design (see the g3 fixture). The rest of the RET family passes clean on mettsim and its autofixes are vectorizer-safe, so it is now enforced.

The SIM108 ignore for tests/tt/test_vectorization.py stays: its statement-style if/else functions are intentional fixtures for the GAST converter.

Companion sweep for gettsim's policy code: ttsim-dev/gettsim#1195.

🤖 Generated with Claude Code

The vectorizer supports ternary expressions (IfExp -> xnp.where), and
middle_earth has no SIM108 violations, so the per-file ignore is dead
config. Keep the ignore for tests/tt/test_vectorization.py, whose
statement-style if/else blocks are intentional fixtures, and say so in
the comment.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Jun 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@MImmesberger MImmesberger requested a review from hmgaudecker June 6, 2026 13:12

@hmgaudecker hmgaudecker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment thread pyproject.toml Outdated
"PLR1716", # Boolean chained comparison
"RET", # Unnecessary return logic
"SIM108", # Use ternary operator
"SIM108", # Statement-style if/else blocks are intentional fixtures for the vectorizer

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine, but we should add a test for the new behaviour, or is it already in place?

I'm amazed we don't have a single instance of an if/else block in METTSIM.

@MImmesberger MImmesberger Jun 6, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have if/else blocks, the SIM108 rule is about how they're written up (and we already wrote everything in a compliant way).

I added two oneline if/else conditions to the code and tightened the RET hook (we can allow all codes but RET505 I believe).

AFAICT there is no rule that disallowed these oneliners. Sorry, should have made that clear earlier.

Showcase that the vectorizer handles ternary expressions (ast.IfExp ->
xnp.where) in real policy functions, not just in the unit-test fixtures
(f2/f6/f7/f10 in tests/tt/test_vectorization.py). The converted functions
are swept by test_convertible for all policy years.

Keep statement-style if/else where it is multi-branch (payroll_tax
amount_y), over the line limit (orc bounty), an integration-test subject
(housing_benefits amount_m_fam), or an intentional fixture
(annotation_bug_reproducer).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@MImmesberger MImmesberger changed the title Remove dead SIM108 ignore for middle_earth Remove dead SIM108 ignore for middle_earth; showcase ternaries in mettsim Jun 6, 2026
MImmesberger and others added 2 commits June 7, 2026 00:30
mettsim doubles as test material for the vectorizer, so it should exercise
both supported styles. Revert the housing-benefit income and property-tax
cap conversions to block form; keep the ternaries only where they are
genuine short one-liners (wealth tax, child tax credit).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Only RET505 (superfluous else after return) conflicts with the vectorizer:
its autofix produces the early-return form that _if_to_call rejects (g3
fixture). The rest of the RET family passes clean on mettsim and its
autofixes are vectorizer-safe, so enforce it.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Comment thread pyproject.toml Outdated
@@ -240,7 +239,7 @@ per-file-ignores."tests/tt/test_vectorization.py" = [
"PLR1714", # Repeated equality comparison
"PLR1716", # Boolean chained comparison
"RET", # Unnecessary return logic

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"RET", # Unnecessary return logic
"RET505", # Vectorizer requires both if/else branches; it rejects the early-return form

?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether we need all that duplication in the first place, though.

@hmgaudecker hmgaudecker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent, thanks!

@hmgaudecker

Copy link
Copy Markdown
Contributor

😇 deleted a message where my own bad memory fooled me, sorry for the email noise, @MImmesberger

The E721/PLR1714/PLR1716 ignores were inherited from gettsim's config in
the initial ttsim split and never fire in either file. The blanket RET
ignore for test_vectorization.py covers exactly two live rules: RET504
(the `out = ...; return out` fixtures mirror the vectorizer's output)
and RET505 (return-style if/else fixtures).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@MImmesberger MImmesberger merged commit 2806947 into main Jun 7, 2026
13 checks passed
@MImmesberger MImmesberger deleted the remove-dead-sim108-ignore branch June 7, 2026 09:15
MImmesberger added a commit to ttsim-dev/gettsim that referenced this pull request Jun 9, 2026
Following ttsim-dev/ttsim#129: only RET505 (superfluous-else-after-return)
conflicts with the vectorizer, whose `_if_to_call` rejects the early-return
form RET505's autofix produces. The rest of the RET family is vectorizer-safe,
so enforce it.

- pyproject.toml: replace blanket "RET" ignore for src/gettsim/germany/* with
  "RET505". Clarify PLR1714/PLR1716 comments ("not vectorizable") — unlike
  mettsim these are real, needed ignores: vectorization.py has no visit_Compare,
  so `in` and chained comparisons are not lowered to element-wise ops.
- Fix the 5 now-enforced RET504 sites (out = ...; return out -> direct return):
  vorsorge.py, ids.py (x3), erwerbsminderung.py.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

2 participants