Skip to content

RSE102 error despite parentheses being necessary #5416

@sultur

Description

@sultur

Hello, thank you for ruff, it's fantastic!
I encountered this small bug regarding rule RSE102.

Ruff version: ruff 0.0.274
Ruff settings: All rules turned on (ignoring a few specific ones, not relevant here).

Minimal code snippet:

def return_error():
    return ValueError("Something")

raise return_error()

Commands to reproduce:

$ ruff bug.py
test.py:4:19: RSE102 [*] Unnecessary parentheses on raised exception
Found 1 error.
[*] 1 potentially fixable with the --fix option.
$ ruff bug.py --fix
Found 1 error (1 fixed, 0 remaining).

Resulting file after autofix:

def return_error():
    return ValueError("Something")

raise return_error

Issue:
The line raise return_error() is diagnosed as violating RSE102 (Unecessary parentheses on raised exception), despite the parentheses being necessary.
This gets autofixed as raise return_error, which raises a TypeError (as the function isn't an exception) instead of a ValueError.


Solution idea (I took a quick glance at the changes in #2596, but I'm not well versed enough in Rust to fully understand them.):
When autofixing, instead of removing any empty parentheses at the end of raise ... lines, only attempt to remove them when they come directly after the name of a built-in exception (e.g. ValueError, TypeError, (asyncio.)?CancelledError, etc.).

So lines like

raise NotImplementedError()
raise ValueError()

can be autofixed into

raise NotImplementedError
raise ValueError

but lines like

raise SomeCustomError()
# or
raise my_error_function()

would require manual fixing.

Maybe you have some more clever solution to this than matching a long list of built-in exceptions, but I hope this helps though!

Metadata

Metadata

Assignees

No one assigned

    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