Skip to content

[flake8-return] Fix RET504 false positive when variable is read in except/finally#24484

Open
anishgirianish wants to merge 3 commits intoastral-sh:mainfrom
anishgirianish:fix-ret504-try-finally-false-positive
Open

[flake8-return] Fix RET504 false positive when variable is read in except/finally#24484
anishgirianish wants to merge 3 commits intoastral-sh:mainfrom
anishgirianish:fix-ret504-try-finally-false-positive

Conversation

@anishgirianish
Copy link
Copy Markdown
Contributor

@anishgirianish anishgirianish commented Apr 8, 2026

Summary

Fixes #17292

RET504 flagged assignments as unnecessary even when the variable was read in finally/except, breaking runtime behavior on fix.

Checks that the binding has only one reference (the return itself) before flagging

Test Plan

  • Fixture cases for finally, except, nested try, and the still-fires case.
  • ran ecosystem checks locally and verified expected results

@astral-sh-bot astral-sh-bot Bot requested a review from amyreese April 8, 2026 07:47
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented Apr 8, 2026

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+1 -0 violations, +0 -0 fixes in 1 projects; 55 projects unchanged)

mlflow/mlflow (+1 -0 violations, +0 -0 fixes)

+ mlflow/telemetry/track.py:31:32: RUF100 [*] Unused `noqa` directive (unused: `RET504`)

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF100 1 1 0 0 0

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+1 -0 violations, +0 -0 fixes in 1 projects; 55 projects unchanged)

mlflow/mlflow (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview

+ mlflow/telemetry/track.py:31:32: RUF100 [*] Unused `noqa` directive (unused: `RET504`)

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF100 1 1 0 0 0

@MichaReiser
Copy link
Copy Markdown
Member

MichaReiser commented Apr 8, 2026

Can you take another look at the ecosystem report. It's unclear to me why https://github.com/apache/airflow/blob/4a499ad4241876850e4ad93d1db42e3d7b71a634/airflow-core/src/airflow/api/common/mark_tasks.py#L108-L124 disappears (there's no try...finally involved)

@MichaReiser MichaReiser marked this pull request as draft April 8, 2026 12:16
@anishgirianish anishgirianish marked this pull request as ready for review April 13, 2026 01:13
@anishgirianish
Copy link
Copy Markdown
Contributor Author

@MichaReiser, thanks for catching the airflow regression earlier, which pointed me in the right direction. Would like to request you for your re-review whenever you get a chance.

Thank you

@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations labels Apr 13, 2026
parents
.iter()
.filter_map(|stmt| stmt.as_try_stmt())
.any(|stmt_try| any_over_body(&stmt_try.finalbody, &name_read))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is incorrect if the current branch is the finalbody:

  def f():
      try:
          pass
      finally:
          x = foo()
          return x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fixes Related to suggested fixes for violations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RET504 breaks code when value is used in finally

4 participants