Skip to content

[flake8-blind-except] Change BLE001 to correctly parse exception tuples#19747

Merged
dylwil3 merged 1 commit intoastral-sh:mainfrom
deliro:ble-tuple-exceptions
Aug 4, 2025
Merged

[flake8-blind-except] Change BLE001 to correctly parse exception tuples#19747
dylwil3 merged 1 commit intoastral-sh:mainfrom
deliro:ble-tuple-exceptions

Conversation

@deliro
Copy link
Contributor

@deliro deliro commented Aug 4, 2025

Summary

This PR enhances the BLE001 rule to correctly detect blind exception handling in tuple exceptions. Previously, the rule only checked single exception types, but Python allows catching multiple exceptions using tuples like except (Exception, ValueError):.

Test Plan

It fails the following (whereas the main branch does not):

cargo run -p ruff -- check somefile.py --no-cache --select=BLE001
# somefile.py

try:
    1/0
except (ValueError, Exception) as e:
    print(e)
somefile.py:3:21: BLE001 Do not catch blind exception: `Exception`
  |
1 | try:
2 |     1/0
3 | except (ValueError, Exception) as e:
  |                     ^^^^^^^^^ BLE001
4 |     print(e)
  |

Found 1 error.

@deliro deliro force-pushed the ble-tuple-exceptions branch 2 times, most recently from 63115c7 to 5620d2f Compare August 4, 2025 19:12
@dylwil3 dylwil3 added the rule Implementing or modifying a lint rule label Aug 4, 2025
@dylwil3 dylwil3 changed the title [flake8-blind-except] Change BLE001 to correctly parse exception tuples [flake8-blind-except] Change BLE001 to correctly parse exception tuples Aug 4, 2025
@dylwil3 dylwil3 added the bug Something isn't working label Aug 4, 2025
Copy link
Collaborator

@dylwil3 dylwil3 left a comment

Choose a reason for hiding this comment

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

Neat, thank you! I'm gonna call this a bug instead of an extension of the rule - I don't think it needs to be gated behind preview. Let's just add a couple more tests - the implementation looks good to me though!

@github-actions
Copy link
Contributor

github-actions bot commented Aug 4, 2025

ruff-ecosystem results

Linter (stable)

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

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

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

+ airflow-core/src/airflow/models/dagbundle.py:74:31: BLE001 Do not catch blind exception: `Exception`
+ providers/celery/src/airflow/providers/celery/executors/celery_executor_utils.py:283:13: BLE001 Do not catch blind exception: `Exception`

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

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

+ zerver/webhooks/slack/view.py:225:29: BLE001 Do not catch blind exception: `Exception`

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
BLE001 3 3 0 0 0

Linter (preview)

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

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

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

+ airflow-core/src/airflow/models/dagbundle.py:74:31: BLE001 Do not catch blind exception: `Exception`
+ providers/celery/src/airflow/providers/celery/executors/celery_executor_utils.py:283:13: BLE001 Do not catch blind exception: `Exception`

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

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

+ zerver/webhooks/slack/view.py:225:29: BLE001 Do not catch blind exception: `Exception`

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
BLE001 3 3 0 0 0

@deliro deliro force-pushed the ble-tuple-exceptions branch from 5620d2f to e433fe7 Compare August 4, 2025 20:44
@deliro deliro requested a review from dylwil3 August 4, 2025 20:47
@deliro deliro changed the title [flake8-blind-except] Change BLE001 to correctly parse exception tuples [flake8-blind-except] Change BLE001 to correctly parse exception tuples Aug 4, 2025
Copy link
Collaborator

@dylwil3 dylwil3 left a comment

Choose a reason for hiding this comment

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

Nice, thank you!

@dylwil3 dylwil3 enabled auto-merge (squash) August 4, 2025 21:11
@dylwil3 dylwil3 merged commit b0f01ba into astral-sh:main Aug 4, 2025
34 checks passed
@deliro deliro deleted the ble-tuple-exceptions branch August 5, 2025 05:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants