Skip to content

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

Merged
ntBre merged 4 commits into
mainfrom
brent/w0717
May 28, 2026
Merged

[pylint] Narrow diagnostic range and exclude cases without exception handlers (PLW0717)#25440
ntBre merged 4 commits into
mainfrom
brent/w0717

Conversation

@ntBre

@ntBre ntBre commented May 28, 2026

Copy link
Copy Markdown
Contributor

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:

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

ntBre added 4 commits May 28, 2026 15:33
…n handlers (`PLW0717`)

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
@ntBre ntBre added rule Implementing or modifying a lint rule preview Related to preview mode features labels May 28, 2026
@astral-sh-bot

astral-sh-bot Bot commented May 28, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

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

DisnakeDev/disnake (+0 -5 violations, +0 -0 fixes)

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

- disnake/ext/commands/base_core.py:680:9: PLW0717 Try clause contains too many statements (12 > 5)
- disnake/ext/commands/cog.py:830:9: PLW0717 Try clause contains too many statements (25 > 5)
- disnake/ext/commands/core.py:1098:9: PLW0717 Try clause contains too many statements (11 > 5)
- disnake/ext/commands/flag_converter.py:272:9: PLW0717 Try clause contains too many statements (7 > 5)
- tests/test_embeds.py:145:5: PLW0717 Try clause contains too many statements (12 > 5)

apache/airflow (+0 -106 violations, +0 -0 fixes)

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

- airflow-core/src/airflow/api_fastapi/auth/managers/simple/simple_auth_manager.py:189:17: PLW0717 Try clause contains too many statements (11 > 5)
- airflow-core/src/airflow/api_fastapi/core_api/routes/public/connections.py:243:5: PLW0717 Try clause contains too many statements (11 > 5)
- airflow-core/src/airflow/cli/commands/task_command.py:441:5: PLW0717 Try clause contains too many statements (6 > 5)
- airflow-core/src/airflow/dag_processing/importers/python_importer.py:67:5: PLW0717 Try clause contains too many statements (6 > 5)
- airflow-core/src/airflow/jobs/triggerer_job_runner.py:983:9: PLW0717 Try clause contains too many statements (7 > 5)
- airflow-core/src/airflow/utils/db.py:1303:5: PLW0717 Try clause contains too many statements (12 > 5)
- airflow-core/src/airflow/utils/db.py:1560:5: PLW0717 Try clause contains too many statements (9 > 5)
- airflow-core/src/airflow/utils/db.py:1596:5: PLW0717 Try clause contains too many statements (7 > 5)
- airflow-core/src/airflow/utils/log/log_stream_accumulator.py:156:9: PLW0717 Try clause contains too many statements (7 > 5)
- airflow-core/src/airflow/utils/process_utils.py:244:5: PLW0717 Try clause contains too many statements (19 > 5)
- airflow-core/tests/integration/otel/test_otel.py:327:9: PLW0717 Try clause contains too many statements (15 > 5)
- airflow-core/tests/integration/otel/test_otel.py:490:9: PLW0717 Try clause contains too many statements (13 > 5)
- airflow-core/tests/unit/always/test_connection.py:754:9: PLW0717 Try clause contains too many statements (8 > 5)
... 93 additional changes omitted for project

apache/superset (+0 -62 violations, +0 -0 fixes)

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

- superset/tasks/manager.py:319:9: PLW0717 Try clause contains too many statements (13 > 5)
- tests/integration_tests/charts/api_tests.py:1270:9: PLW0717 Try clause contains too many statements (11 > 5)
- tests/integration_tests/dao/base_dao_test.py:1131:5: PLW0717 Try clause contains too many statements (7 > 5)
- tests/integration_tests/dashboards/api_tests.py:3991:9: PLW0717 Try clause contains too many statements (23 > 5)
- tests/integration_tests/dashboards/api_tests.py:772:9: PLW0717 Try clause contains too many statements (7 > 5)
- tests/integration_tests/dashboards/api_tests.py:794:9: PLW0717 Try clause contains too many statements (7 > 5)
- tests/integration_tests/dashboards/test_theme_integration.py:250:9: PLW0717 Try clause contains too many statements (11 > 5)
- tests/integration_tests/dashboards/test_theme_integration.py:298:9: PLW0717 Try clause contains too many statements (13 > 5)
- tests/integration_tests/dashboards/test_theme_integration.py:351:9: PLW0717 Try clause contains too many statements (6 > 5)
- tests/integration_tests/databases/api_tests.py:1007:9: PLW0717 Try clause contains too many statements (8 > 5)
... 52 additional changes omitted for project

aws/aws-sam-cli (+0 -16 violations, +0 -0 fixes)

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

- samcli/lib/cfn_language_extensions/resolvers/fn_if.py:146:9: PLW0717 Try clause contains too many statements (7 > 5)
- samcli/local/docker/durable_lambda_container.py:158:9: PLW0717 Try clause contains too many statements (24 > 5)
- samcli/local/docker/lambda_image.py:409:9: PLW0717 Try clause contains too many statements (47 > 5)
- tests/integration/deploy/test_deploy_command.py:1827:9: PLW0717 Try clause contains too many statements (26 > 5)
- tests/unit/lib/package/test_artifact_exporter.py:1209:9: PLW0717 Try clause contains too many statements (10 > 5)
- tests/unit/lib/package/test_artifact_exporter.py:1392:9: PLW0717 Try clause contains too many statements (10 > 5)
- tests/unit/lib/package/test_artifact_exporter.py:2219:9: PLW0717 Try clause contains too many statements (13 > 5)
- tests/unit/lib/package/test_artifact_exporter.py:2256:9: PLW0717 Try clause contains too many statements (7 > 5)
- tests/unit/lib/package/test_artifact_exporter.py:2302:9: PLW0717 Try clause contains too many statements (9 > 5)
- tests/unit/lib/package/test_artifact_exporter.py:2337:9: PLW0717 Try clause contains too many statements (17 > 5)
... 6 additional changes omitted for project

bokeh/bokeh (+0 -6 violations, +0 -0 fixes)

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

- src/bokeh/server/session.py:87:9: PLW0717 Try clause contains too many statements (13 > 5)
- tests/support/plugins/bokeh_server.py:94:9: PLW0717 Try clause contains too many statements (7 > 5)
- tests/unit/bokeh/core/test_validation.py:185:5: PLW0717 Try clause contains too many statements (8 > 5)
- tests/unit/bokeh/io/test_export.py:249:5: PLW0717 Try clause contains too many statements (6 > 5)
- tests/unit/bokeh/io/test_util__io.py:114:5: PLW0717 Try clause contains too many statements (6 > 5)
- tests/unit/bokeh/io/test_util__io.py:59:5: PLW0717 Try clause contains too many statements (22 > 5)

freedomofpress/securedrop (+0 -11 violations, +0 -0 fixes)

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

- molecule/testinfra/app/test_redis.py:64:9: PLW0717 Try clause contains too many statements (11 > 5)
- molecule/testinfra/app/test_smoke.py:85:9: PLW0717 Try clause contains too many statements (6 > 5)
- molecule/testinfra/common/test_grsecurity.py:154:5: PLW0717 Try clause contains too many statements (9 > 5)
- securedrop/tests/conftest.py:82:5: PLW0717 Try clause contains too many statements (7 > 5)
- securedrop/tests/functional/conftest.py:118:5: PLW0717 Try clause contains too many statements (37 > 5)
- securedrop/tests/test_journalist.py:2488:5: PLW0717 Try clause contains too many statements (17 > 5)
- securedrop/tests/test_journalist.py:2561:5: PLW0717 Try clause contains too many statements (11 > 5)
- securedrop/tests/test_rm.py:15:5: PLW0717 Try clause contains too many statements (7 > 5)
- securedrop/tests/test_source_utils.py:14:5: PLW0717 Try clause contains too many statements (12 > 5)
- securedrop/tests/test_worker.py:117:5: PLW0717 Try clause contains too many statements (21 > 5)
... 1 additional changes omitted for project

ibis-project/ibis (+0 -21 violations, +0 -0 fixes)

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

- ibis/backends/flink/tests/test_ddl.py:238:5: PLW0717 Try clause contains too many statements (7 > 5)
- ibis/backends/flink/tests/test_ddl.py:483:5: PLW0717 Try clause contains too many statements (12 > 5)
- ibis/backends/materialize/tests/test_aggregate_edge_cases.py:132:5: PLW0717 Try clause contains too many statements (6 > 5)
- ibis/backends/materialize/tests/test_aggregate_edge_cases.py:36:5: PLW0717 Try clause contains too many statements (6 > 5)
- ibis/backends/materialize/tests/test_aggregate_edge_cases.py:71:5: PLW0717 Try clause contains too many statements (7 > 5)
- ibis/backends/materialize/tests/test_client.py:312:5: PLW0717 Try clause contains too many statements (7 > 5)
- ibis/backends/materialize/tests/test_sources.py:89:9: PLW0717 Try clause contains too many statements (7 > 5)
- ibis/backends/materialize/tests/test_subscribe.py:122:9: PLW0717 Try clause contains too many statements (14 > 5)
- ibis/backends/materialize/tests/test_subscribe.py:36:9: PLW0717 Try clause contains too many statements (11 > 5)
- ibis/backends/materialize/tests/test_subscribe.py:78:9: PLW0717 Try clause contains too many statements (12 > 5)
... 11 additional changes omitted for project

langchain-ai/langchain (+0 -34 violations, +0 -0 fixes)

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

- libs/core/langchain_core/callbacks/manager.py:275:5: PLW0717 Try clause contains too many statements (19 > 5)
- libs/core/langchain_core/tracers/log_stream.py:493:9: PLW0717 Try clause contains too many statements (7 > 5)
- libs/core/langchain_core/tracers/log_stream.py:717:9: PLW0717 Try clause contains too many statements (16 > 5)
- libs/core/langchain_core/tracers/log_stream.py:756:5: PLW0717 Try clause contains too many statements (6 > 5)
- libs/core/langchain_core/utils/aiter.py:128:5: PLW0717 Try clause contains too many statements (12 > 5)
- libs/core/langchain_core/utils/iter.py:58:5: PLW0717 Try clause contains too many statements (12 > 5)
- libs/core/tests/unit_tests/indexing/test_indexing.py:2593:5: PLW0717 Try clause contains too many statements (7 > 5)
- libs/core/tests/unit_tests/language_models/chat_models/test_cache.py:107:5: PLW0717 Try clause contains too many statements (11 > 5)
- libs/core/tests/unit_tests/language_models/chat_models/test_cache.py:133:5: PLW0717 Try clause contains too many statements (11 > 5)
- libs/core/tests/unit_tests/language_models/chat_models/test_cache.py:192:5: PLW0717 Try clause contains too many statements (14 > 5)
... 24 additional changes omitted for project

milvus-io/pymilvus (+0 -7 violations, +0 -0 fixes)

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

- tests/orm/test_deprecation_warnings.py:74:5: PLW0717 Try clause contains too many statements (6 > 5)
- tests/orm/test_deprecation_warnings.py:98:5: PLW0717 Try clause contains too many statements (6 > 5)
- tests/test_async_milvus_client_reuse.py:39:9: PLW0717 Try clause contains too many statements (8 > 5)
- tests/test_milvus_lite.py:36:9: PLW0717 Try clause contains too many statements (12 > 5)
- tests/test_milvus_lite.py:76:9: PLW0717 Try clause contains too many statements (8 > 5)
- tests/test_search_result.py:834:9: PLW0717 Try clause contains too many statements (7 > 5)
- tests/test_type_info.py:99:5: PLW0717 Try clause contains too many statements (11 > 5)

... Truncated remaining completed project reports due to GitHub comment length restrictions

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PLW0717 380 0 380 0 0

@ntBre

ntBre commented May 28, 2026

Copy link
Copy Markdown
Contributor Author

Wow this pattern is way more common than I noticed on the original PR. This looks like a good change.

@ntBre ntBre marked this pull request as ready for review May 28, 2026 20:12
@ntBre ntBre merged commit 2110d32 into main May 28, 2026
45 checks passed
@ntBre ntBre deleted the brent/w0717 branch May 28, 2026 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Related to preview mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PLW0717: Visually noisy diagnostic ranges False positive: too-many-statements-in-try-clause triggers on try-finally clause

2 participants