SIGN IN SIGN UP
astral-sh / ruff UNCLAIMED

An extremely fast Python linter and code formatter, written in Rust.

0 0 36 Rust

[`pylint`] Narrow diagnostic range and exclude cases without exception handlers (`PLW0717`) (#25440)

Summary
--

This PR fixes #25438 by narrowing the diagnostic range from the entire
`try` statement to only the
`try` keyword. This was one of the two alternatives mentioned on #25438,
and I thought this looked a
bit nicer than only narrowing the range to the `try` body. I think this
is also consistent with the
range for `too-many-statements` (`PLR0915`), which marks the function
name:

https://play.ruff.rs/dafe23a1-2e89-48a5-b4d7-b0825b737152

I'm happy to reconsider, though. Another possible alternative along
these lines is marking the first
statement that exceeds the limit, or the last statement, or something
like that.

This PR also fixes #25390 by limiting the rule to `try` statements with
`except` handlers. Either an
`except` or a `finally` clause is required, so this avoids emitting
diagnostics for
context-manager-like cases, such as the one reported in the issue:

```py
session = ...
try:
    print()
    print()
    print()
    print()
    print()
    print()
finally:
    session.close()
```

where the `try` is just ensuring that some cleanup is performed and
can't actually trigger the bad
behavior described in the rule docs of mistakenly catching the wrong
exception.

Test Plan
--

New mdtests based on the issues
B
Brent Westbrook committed
2110d320da20d1f6fc20b0397f75b0d08a13d5f8
Parent: f9ba228
Committed by GitHub <noreply@github.com> on 5/28/2026, 9:24:12 PM