Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[new rule] pylint W0133: Exception statement has no effect #10145

Closed
Taragolis opened this issue Feb 27, 2024 · 3 comments
Closed

[new rule] pylint W0133: Exception statement has no effect #10145

Taragolis opened this issue Feb 27, 2024 · 3 comments
Labels
rule Implementing or modifying a lint rule

Comments

@Taragolis
Copy link

This rule already requested into the #8674, however suggested replacement B018 can not handle all the cases.

For example this snippet do not violate any of B018 checks: https://play.ruff.rs/bef7e65b-09c6-4e8f-84e9-48a75be4e0b9

# w0133.py
from __future__ import annotations


def some_func(b: str, *, a: str | None = None, raise_an_error=True):
    if a:
        msg = "`a` is deprecated, consider to use `b` instead."
        if raise_an_error:
            msg = "Use `b` instead."
            ValueError(msg)
        DeprecationWarning(msg)
        b = a
    return b


class AwesomeClass:
    def __init__(self, b: str | None = None, **kwargs):
        if a := kwargs.pop("a", None):
            DeprecationWarning("`a` is deprecated, consider to use `b` instead.")
            if b and a != b:
                msg = f"Unable to resolve ambiguous values b={b!r}, a={a!r}."
                ValueError(msg)
            b = a
        self.b = b

    def abstract(self):
        NotImplementedError("abstract should be implemented")


ValueError("Foo")
DeprecationWarning("Bar")


if __name__ == "__main__":
    ValueError("Spam")
    DeprecationWarning("Egg")

In the other hand pylint rule W0133 catch all useless exceptions and warnings

pipx run pylint --disable=all --enable="W0133" w0133.py
************* Module w0133
w0133.py:10:12: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:11:8: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:19:12: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:22:16: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:27:8: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:30:0: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:31:0: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:35:4: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:36:4: W0133: Exception statement has no effect (pointless-exception-statement)

It would be nice if it possible to have this rule as part of the ruff, this might help to found such a mistakes in codebase of pretty big projects.

@AlexWaygood AlexWaygood added the rule Implementing or modifying a lint rule label Feb 27, 2024
@mikeleppane
Copy link
Contributor

mikeleppane commented Feb 28, 2024

I could take a look at this. Let’s see, if this can be reasonably extended to the B018. Otherwise, this may require a separate rule?

@Taragolis
Copy link
Author

If you ask me then I do not not have any preferences, so it could be either separate rule something like PLW0133 or extension of B018.

charliermarsh pushed a commit that referenced this issue Mar 1, 2024
## Summary

This review contains a new rule for handling `useless exception
statements` (`PLW0133`). Is it based on the following pylint's rule:
[W0133](https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/pointless-exception-statement.html)


Note: this rule does not cover the case if an error is a custom
exception class.

See: [Rule request](#10145)

## Test Plan

```bash
cargo test & manually
```
nkxxll pushed a commit to nkxxll/ruff that referenced this issue Mar 10, 2024
…sh#10176)

## Summary

This review contains a new rule for handling `useless exception
statements` (`PLW0133`). Is it based on the following pylint's rule:
[W0133](https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/pointless-exception-statement.html)


Note: this rule does not cover the case if an error is a custom
exception class.

See: [Rule request](astral-sh#10145)

## Test Plan

```bash
cargo test & manually
```
@zanieb
Copy link
Member

zanieb commented Mar 11, 2024

Done in #10176

@zanieb zanieb closed this as completed Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

4 participants