Skip to content

UP024 error message is confusing when except clause has multiple exceptions #19696

@burrscurr

Description

@burrscurr

Summary

UP024 highlights specific aliased exceptions which might be removed in the future. When the except clause contains multiple exceptions, the error message can be a bit misleading. Simple Example:

try:
    pass
except (IOError, ValueError):
    pass

In this example, IOError is an alias of OSError, that should be replaced, while ValueError is just a regular exception. Ruff correctly understands this (see uv tool run ruff@0.12.7 check --select UP024 --fix --diff), but its output does not distinguish between exceptions targeted by this rule and ones that aren't:

example.py:3:8: UP024 [*] Replace aliased errors with `OSError`
  |
1 | try:
2 |     pass
3 | except (ValueError, IOError):
  |        ^^^^^^^^^^^^^^^^^^^^^ UP024
4 |     pass
  |
  = help: Replace with builtin `OSError`

The error message "Replace aliased errors with OSError" in connection with the highlighted section makes it seem as if both exceptions were aliases of OSError and should be replaced.

If this is possible, specifically highlighting the aliased exceptions would probably be sufficient to avoid this misinterpretation:

example.py:3:8: UP024 [*] Replace aliased errors with `OSError`
  |
1 | try:
2 |     pass
3 | except (ValueError, IOError):
  |                     ^^^^^^^ UP024
4 |     pass
  |
  = help: Replace with builtin `OSError`

Version

ruff 0.12.7 (c5ac998 2025-07-29)

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedContributions especially welcomeruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions