Skip to content

BLE001: conditional handling of errors not detected #11289

@bentheiii

Description

@bentheiii

keywords: BLE001, log, exception

The following code snippet does not trigger the BLE001 rule:

from logging import exception


try:
    pass
except Exception:
    exception("An error occurred")

but this one does:

from logging import exception

try:
    pass
except Exception:
    if True:
        exception("An error occurred")
    else:
        exception("An error occurred")

This is because ruff doesn't seem to consider the case the both branches of an if handle the error.

The reason we run into this is because sometimes we use code like the following:

from logging import exception

try:
    pass
except Exception as ex:
    error_data = gather_diagnostic_data(...)
    if isinstance(ex, SomespecificError):
        exception("A specific error occurred", extra=error_data)
    else:
        exception("A generic error occurred", extra=error_data)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions