pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.32k stars 1.14k forks source link

Clarify `no-else-raise` and `no-else-return` also apply to try/except #9901

Closed jolaf closed 1 month ago

jolaf commented 2 months ago

Bug description

def f() -> None:
    try:
        assert False
    except Exception:  # pylint: disable=try-except-raise
        raise
    else:
        pass

    try:
        assert False
    except Exception:
        return
    else:
        pass

Configuration

No response

Command used

pylint test.py

Pylint output

************* Module test
test.py:2: [R1720(no-else-raise), f] Unnecessary "else" after "raise", remove the "else" and de-indent the code inside it
test.py:9: [R1705(no-else-return), f] Unnecessary "else" after "return", remove the "else" and de-indent the code inside it

Expected behavior

No error reports.

As far as I understand, no-else-raise/no-else-return are supposed to be produced for if-else operators.

But in this example else is part of try-except-else operators, and the errors are completely inappropriate.

Pylint version

pylint 3.2.6
astroid 3.2.4
Python 3.12.3 (main, Jul 31 2024, 17:43:48) [GCC 13.2.0]

OS / Environment

Ubuntu 24.04.1

Additional dependencies

No response

jacobtylerwalls commented 2 months ago

These aren't errors, they're refactoring messages. They still apply here, but we should update the documentation to clarify, since the documentation only mentions if/else.