Skip to content

[Feedback] PLW0133 not implements all pylint W0133 detections #10392

@Taragolis

Description

@Taragolis

I've tested this simple snippet in ruff 0.3.2 (https://play.ruff.rs/9eb226cd-c2de-41fe-9118-b2e90857fa9e) and in pylint, and the detection a bit different

# w0133.py
from __future__ import annotations
from io import UnsupportedOperation


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")
Warning("Bar")
UnsupportedOperation("Spam")

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

ruff

pipx run --spec="ruff" ruff check w0133.py --select "PLW0133" --preview

w0133.py:11:13: PLW0133 Missing `raise` statement on exception
   |
 9 |         if raise_an_error:
10 |             msg = "Use `b` instead."
11 |             ValueError(msg)
   |             ^^^^^^^^^^^^^^^ PLW0133
12 |         DeprecationWarning(msg)
13 |         b = a
   |
   = help: Add `raise` keyword

w0133.py:23:17: PLW0133 Missing `raise` statement on exception
   |
21 |             if b and a != b:
22 |                 msg = f"Unable to resolve ambiguous values b={b!r}, a={a!r}."
23 |                 ValueError(msg)
   |                 ^^^^^^^^^^^^^^^ PLW0133
24 |             b = a
25 |         self.b = b
   |
   = help: Add `raise` keyword

w0133.py:28:9: PLW0133 Missing `raise` statement on exception
   |
27 |     def abstract(self):
28 |         NotImplementedError("abstract should be implemented")
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLW0133
   |
   = help: Add `raise` keyword

w0133.py:31:1: PLW0133 Missing `raise` statement on exception
   |
31 | ValueError("Foo")
   | ^^^^^^^^^^^^^^^^^ PLW0133
32 | Warning("Bar")
33 | UnsupportedOperation("Spam")
   |
   = help: Add `raise` keyword

w0133.py:36:5: PLW0133 Missing `raise` statement on exception
   |
35 | if __name__ == "__main__":
36 |     ValueError("Spam")
   |     ^^^^^^^^^^^^^^^^^^ PLW0133
37 |     DeprecationWarning("Egg")
   |
   = help: Add `raise` keyword

Found 5 errors.
No fixes available (5 hidden fixes can be enabled with the `--unsafe-fixes` option).

pylint

pipx run --spec="pylint" pylint --disable=all --enable="W0133" w0133.py
************* Module w0133
w0133.py:11:12: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:12:8: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:20:12: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:23:16: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:28:8: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:31:0: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:32:0: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:33:0: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:36:4: W0133: Exception statement has no effect (pointless-exception-statement)
w0133.py:37:4: W0133: Exception statement has no effect (pointless-exception-statement)

What I've found:

  1. ruff detects only some of builtin exceptions
  2. ruff do not detect Warning (builtin Exception) and all subclasses, so useless warnings are undetected
  3. Classes which inherits from Exception or BaseException can't be detected

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinghelp 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