This commit is contained in:
Loïc Riegel 2025-11-16 05:13:09 +01:00 committed by GitHub
commit 2f0afd06f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 792 additions and 198 deletions

View file

@ -2,15 +2,34 @@ from abc import ABC, abstractmethod
from contextlib import suppress from contextlib import suppress
class MyError(Exception):
...
class MyValueError(ValueError):
...
class MyUserWarning(UserWarning):
...
# Violation test cases with builtin errors: PLW0133
# Test case 1: Useless exception statement # Test case 1: Useless exception statement
def func(): def func():
AssertionError("This is an assertion error") # PLW0133 AssertionError("This is an assertion error") # PLW0133
MyError("This is a custom error") # PLW0133
MyValueError("This is a custom value error") # PLW0133
# Test case 2: Useless exception statement in try-except block # Test case 2: Useless exception statement in try-except block
def func(): def func():
try: try:
Exception("This is an exception") # PLW0133 Exception("This is an exception") # PLW0133
MyError("This is an exception") # PLW0133
MyValueError("This is an exception") # PLW0133
except Exception as err: except Exception as err:
pass pass
@ -19,6 +38,8 @@ def func():
def func(): def func():
if True: if True:
RuntimeError("This is an exception") # PLW0133 RuntimeError("This is an exception") # PLW0133
MyError("This is an exception") # PLW0133
MyValueError("This is an exception") # PLW0133
# Test case 4: Useless exception statement in class # Test case 4: Useless exception statement in class
@ -26,12 +47,16 @@ def func():
class Class: class Class:
def __init__(self): def __init__(self):
TypeError("This is an exception") # PLW0133 TypeError("This is an exception") # PLW0133
MyError("This is an exception") # PLW0133
MyValueError("This is an exception") # PLW0133
# Test case 5: Useless exception statement in function # Test case 5: Useless exception statement in function
def func(): def func():
def inner(): def inner():
IndexError("This is an exception") # PLW0133 IndexError("This is an exception") # PLW0133
MyError("This is an exception") # PLW0133
MyValueError("This is an exception") # PLW0133
inner() inner()
@ -40,6 +65,8 @@ def func():
def func(): def func():
while True: while True:
KeyError("This is an exception") # PLW0133 KeyError("This is an exception") # PLW0133
MyError("This is an exception") # PLW0133
MyValueError("This is an exception") # PLW0133
# Test case 7: Useless exception statement in abstract class # Test case 7: Useless exception statement in abstract class
@ -48,27 +75,52 @@ def func():
@abstractmethod @abstractmethod
def method(self): def method(self):
NotImplementedError("This is an exception") # PLW0133 NotImplementedError("This is an exception") # PLW0133
MyError("This is an exception") # PLW0133
MyValueError("This is an exception") # PLW0133
# Test case 8: Useless exception statement inside context manager # Test case 8: Useless exception statement inside context manager
def func(): def func():
with suppress(AttributeError): with suppress(Exception):
AttributeError("This is an exception") # PLW0133 AttributeError("This is an exception") # PLW0133
MyError("This is an exception") # PLW0133
MyValueError("This is an exception") # PLW0133
# Test case 9: Useless exception statement in parentheses # Test case 9: Useless exception statement in parentheses
def func(): def func():
(RuntimeError("This is an exception")) # PLW0133 (RuntimeError("This is an exception")) # PLW0133
(MyError("This is an exception")) # PLW0133
(MyValueError("This is an exception")) # PLW0133
# Test case 10: Useless exception statement in continuation # Test case 10: Useless exception statement in continuation
def func(): def func():
x = 1; (RuntimeError("This is an exception")); y = 2 # PLW0133 x = 1; (RuntimeError("This is an exception")); y = 2 # PLW0133
x = 1; (MyError("This is an exception")); y = 2 # PLW0133
x = 1; (MyValueError("This is an exception")); y = 2 # PLW0133
# Test case 11: Useless warning statement # Test case 11: Useless warning statement
def func(): def func():
UserWarning("This is an assertion error") # PLW0133 UserWarning("This is a user warning") # PLW0133
MyUserWarning("This is a custom user warning") # PLW0133
# Test case 12: Useless exception statement at module level
import builtins
builtins.TypeError("still an exception even though it's an Attribute") # PLW0133
PythonFinalizationError("Added in Python 3.13") # PLW0133
MyError("This is an exception") # PLW0133
MyValueError("This is an exception") # PLW0133
UserWarning("This is a user warning") # PLW0133
MyUserWarning("This is a custom user warning") # PLW0133
# Non-violation test cases: PLW0133 # Non-violation test cases: PLW0133
@ -120,9 +172,3 @@ def func():
with suppress(AttributeError): with suppress(AttributeError):
raise AttributeError("This is an exception") # OK raise AttributeError("This is an exception") # OK
import builtins
builtins.TypeError("still an exception even though it's an Attribute")
PythonFinalizationError("Added in Python 3.13")

View file

@ -1,6 +1,6 @@
use ruff_macros::{ViolationMetadata, derive_message_formats}; use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::{self as ast, Expr}; use ruff_python_ast::{self as ast, Expr};
use ruff_python_semantic::SemanticModel; use ruff_python_semantic::{SemanticModel, analyze};
use ruff_python_stdlib::builtins; use ruff_python_stdlib::builtins;
use ruff_text_size::Ranged; use ruff_text_size::Ranged;
@ -16,10 +16,6 @@ use ruff_python_ast::PythonVersion;
/// `ValueError("...")` on its own will have no effect (unlike /// `ValueError("...")` on its own will have no effect (unlike
/// `raise ValueError("...")`) and is likely a mistake. /// `raise ValueError("...")`) and is likely a mistake.
/// ///
/// ## Known problems
/// This rule only detects built-in exceptions, like `ValueError`, and does
/// not catch user-defined exceptions.
///
/// ## Example /// ## Example
/// ```python /// ```python
/// ValueError("...") /// ValueError("...")
@ -56,7 +52,9 @@ pub(crate) fn useless_exception_statement(checker: &Checker, expr: &ast::StmtExp
return; return;
}; };
if is_builtin_exception(func, checker.semantic(), checker.target_version()) { if is_builtin_exception(func, checker.semantic(), checker.target_version())
|| is_custom_exception(func, checker.semantic(), checker.target_version())
{
let mut diagnostic = checker.report_diagnostic(UselessExceptionStatement, expr.range()); let mut diagnostic = checker.report_diagnostic(UselessExceptionStatement, expr.range());
diagnostic.set_fix(Fix::unsafe_edit(Edit::insertion( diagnostic.set_fix(Fix::unsafe_edit(Edit::insertion(
"raise ".to_string(), "raise ".to_string(),
@ -78,3 +76,34 @@ fn is_builtin_exception(
if builtins::is_exception(name, target_version.minor)) if builtins::is_exception(name, target_version.minor))
}) })
} }
/// Returns `true` if the given expression is a custom exception.
fn is_custom_exception(
expr: &Expr,
semantic: &SemanticModel,
target_version: PythonVersion,
) -> bool {
let Some(qualified_name) = semantic.resolve_qualified_name(expr) else {
return false;
};
let Some(symbol) = qualified_name.segments().last() else {
return false;
};
let Some(binding_id) = semantic.lookup_symbol(symbol) else {
return false;
};
let binding = semantic.binding(binding_id);
let Some(source) = binding.source else {
return false;
};
let statement = semantic.statement(source);
if let ast::Stmt::ClassDef(class_def) = statement {
return analyze::class::any_qualified_base_class(class_def, semantic, &|qualified_name| {
if let ["" | "builtins", name] = qualified_name.segments() {
return builtins::is_exception(name, target_version.minor);
}
false
});
}
false
}

View file

@ -2,250 +2,769 @@
source: crates/ruff_linter/src/rules/pylint/mod.rs source: crates/ruff_linter/src/rules/pylint/mod.rs
--- ---
PLW0133 [*] Missing `raise` statement on exception PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:7:5 --> useless_exception_statement.py:22:5
| |
5 | # Test case 1: Useless exception statement 20 | # Test case 1: Useless exception statement
6 | def func(): 21 | def func():
7 | AssertionError("This is an assertion error") # PLW0133 22 | AssertionError("This is an assertion error") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23 | MyError("This is a custom error") # PLW0133
24 | MyValueError("This is a custom value error") # PLW0133
|
help: Add `raise` keyword
19 |
20 | # Test case 1: Useless exception statement
21 | def func():
- AssertionError("This is an assertion error") # PLW0133
22 + raise AssertionError("This is an assertion error") # PLW0133
23 | MyError("This is a custom error") # PLW0133
24 | MyValueError("This is a custom value error") # PLW0133
25 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:23:5
|
21 | def func():
22 | AssertionError("This is an assertion error") # PLW0133
23 | MyError("This is a custom error") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24 | MyValueError("This is a custom value error") # PLW0133
|
help: Add `raise` keyword
20 | # Test case 1: Useless exception statement
21 | def func():
22 | AssertionError("This is an assertion error") # PLW0133
- MyError("This is a custom error") # PLW0133
23 + raise MyError("This is a custom error") # PLW0133
24 | MyValueError("This is a custom value error") # PLW0133
25 |
26 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:24:5
|
22 | AssertionError("This is an assertion error") # PLW0133
23 | MyError("This is a custom error") # PLW0133
24 | MyValueError("This is a custom value error") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
help: Add `raise` keyword help: Add `raise` keyword
4 | 21 | def func():
5 | # Test case 1: Useless exception statement 22 | AssertionError("This is an assertion error") # PLW0133
6 | def func(): 23 | MyError("This is a custom error") # PLW0133
- AssertionError("This is an assertion error") # PLW0133 - MyValueError("This is a custom value error") # PLW0133
7 + raise AssertionError("This is an assertion error") # PLW0133 24 + raise MyValueError("This is a custom value error") # PLW0133
8 | 25 |
9 | 26 |
10 | # Test case 2: Useless exception statement in try-except block 27 | # Test case 2: Useless exception statement in try-except block
note: This is an unsafe fix and may change runtime behavior note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:13:9 --> useless_exception_statement.py:30:9
| |
11 | def func(): 28 | def func():
12 | try: 29 | try:
13 | Exception("This is an exception") # PLW0133 30 | Exception("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14 | except Exception as err: 31 | MyError("This is an exception") # PLW0133
15 | pass 32 | MyValueError("This is an exception") # PLW0133
| |
help: Add `raise` keyword help: Add `raise` keyword
10 | # Test case 2: Useless exception statement in try-except block 27 | # Test case 2: Useless exception statement in try-except block
11 | def func(): 28 | def func():
12 | try: 29 | try:
- Exception("This is an exception") # PLW0133 - Exception("This is an exception") # PLW0133
13 + raise Exception("This is an exception") # PLW0133 30 + raise Exception("This is an exception") # PLW0133
14 | except Exception as err: 31 | MyError("This is an exception") # PLW0133
15 | pass 32 | MyValueError("This is an exception") # PLW0133
16 | 33 | except Exception as err:
note: This is an unsafe fix and may change runtime behavior note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:21:9 --> useless_exception_statement.py:31:9
| |
19 | def func(): 29 | try:
20 | if True: 30 | Exception("This is an exception") # PLW0133
21 | RuntimeError("This is an exception") # PLW0133 31 | MyError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32 | MyValueError("This is an exception") # PLW0133
33 | except Exception as err:
|
help: Add `raise` keyword
28 | def func():
29 | try:
30 | Exception("This is an exception") # PLW0133
- MyError("This is an exception") # PLW0133
31 + raise MyError("This is an exception") # PLW0133
32 | MyValueError("This is an exception") # PLW0133
33 | except Exception as err:
34 | pass
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:32:9
|
30 | Exception("This is an exception") # PLW0133
31 | MyError("This is an exception") # PLW0133
32 | MyValueError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
33 | except Exception as err:
34 | pass
| |
help: Add `raise` keyword help: Add `raise` keyword
18 | # Test case 3: Useless exception statement in if statement 29 | try:
19 | def func(): 30 | Exception("This is an exception") # PLW0133
20 | if True: 31 | MyError("This is an exception") # PLW0133
- MyValueError("This is an exception") # PLW0133
32 + raise MyValueError("This is an exception") # PLW0133
33 | except Exception as err:
34 | pass
35 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:40:9
|
38 | def func():
39 | if True:
40 | RuntimeError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41 | MyError("This is an exception") # PLW0133
42 | MyValueError("This is an exception") # PLW0133
|
help: Add `raise` keyword
37 | # Test case 3: Useless exception statement in if statement
38 | def func():
39 | if True:
- RuntimeError("This is an exception") # PLW0133 - RuntimeError("This is an exception") # PLW0133
21 + raise RuntimeError("This is an exception") # PLW0133 40 + raise RuntimeError("This is an exception") # PLW0133
22 | 41 | MyError("This is an exception") # PLW0133
23 | 42 | MyValueError("This is an exception") # PLW0133
24 | # Test case 4: Useless exception statement in class 43 |
note: This is an unsafe fix and may change runtime behavior note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:28:13 --> useless_exception_statement.py:41:9
| |
26 | class Class: 39 | if True:
27 | def __init__(self): 40 | RuntimeError("This is an exception") # PLW0133
28 | TypeError("This is an exception") # PLW0133 41 | MyError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42 | MyValueError("This is an exception") # PLW0133
| |
help: Add `raise` keyword help: Add `raise` keyword
25 | def func(): 38 | def func():
26 | class Class: 39 | if True:
27 | def __init__(self): 40 | RuntimeError("This is an exception") # PLW0133
- TypeError("This is an exception") # PLW0133 - MyError("This is an exception") # PLW0133
28 + raise TypeError("This is an exception") # PLW0133 41 + raise MyError("This is an exception") # PLW0133
29 | 42 | MyValueError("This is an exception") # PLW0133
30 | 43 |
31 | # Test case 5: Useless exception statement in function 44 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:34:9
|
32 | def func():
33 | def inner():
34 | IndexError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35 |
36 | inner()
|
help: Add `raise` keyword
31 | # Test case 5: Useless exception statement in function
32 | def func():
33 | def inner():
- IndexError("This is an exception") # PLW0133
34 + raise IndexError("This is an exception") # PLW0133
35 |
36 | inner()
37 |
note: This is an unsafe fix and may change runtime behavior note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:42:9 --> useless_exception_statement.py:42:9
| |
40 | def func(): 40 | RuntimeError("This is an exception") # PLW0133
41 | while True: 41 | MyError("This is an exception") # PLW0133
42 | KeyError("This is an exception") # PLW0133 42 | MyValueError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
help: Add `raise` keyword help: Add `raise` keyword
39 | # Test case 6: Useless exception statement in while loop 39 | if True:
40 | def func(): 40 | RuntimeError("This is an exception") # PLW0133
41 | while True: 41 | MyError("This is an exception") # PLW0133
- KeyError("This is an exception") # PLW0133 - MyValueError("This is an exception") # PLW0133
42 + raise KeyError("This is an exception") # PLW0133 42 + raise MyValueError("This is an exception") # PLW0133
43 | 43 |
44 | 44 |
45 | # Test case 7: Useless exception statement in abstract class 45 | # Test case 4: Useless exception statement in class
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:49:13
|
47 | class Class:
48 | def __init__(self):
49 | TypeError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50 | MyError("This is an exception") # PLW0133
51 | MyValueError("This is an exception") # PLW0133
|
help: Add `raise` keyword
46 | def func():
47 | class Class:
48 | def __init__(self):
- TypeError("This is an exception") # PLW0133
49 + raise TypeError("This is an exception") # PLW0133
50 | MyError("This is an exception") # PLW0133
51 | MyValueError("This is an exception") # PLW0133
52 |
note: This is an unsafe fix and may change runtime behavior note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:50:13 --> useless_exception_statement.py:50:13
| |
48 | @abstractmethod 48 | def __init__(self):
49 | def method(self): 49 | TypeError("This is an exception") # PLW0133
50 | NotImplementedError("This is an exception") # PLW0133 50 | MyError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
51 | MyValueError("This is an exception") # PLW0133
| |
help: Add `raise` keyword help: Add `raise` keyword
47 | class Class(ABC): 47 | class Class:
48 | @abstractmethod 48 | def __init__(self):
49 | def method(self): 49 | TypeError("This is an exception") # PLW0133
- NotImplementedError("This is an exception") # PLW0133 - MyError("This is an exception") # PLW0133
50 + raise NotImplementedError("This is an exception") # PLW0133 50 + raise MyError("This is an exception") # PLW0133
51 | 51 | MyValueError("This is an exception") # PLW0133
52 | 52 |
53 | # Test case 8: Useless exception statement inside context manager 53 |
note: This is an unsafe fix and may change runtime behavior note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:56:9 --> useless_exception_statement.py:51:13
| |
54 | def func(): 49 | TypeError("This is an exception") # PLW0133
55 | with suppress(AttributeError): 50 | MyError("This is an exception") # PLW0133
56 | AttributeError("This is an exception") # PLW0133 51 | MyValueError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
help: Add `raise` keyword help: Add `raise` keyword
53 | # Test case 8: Useless exception statement inside context manager 48 | def __init__(self):
54 | def func(): 49 | TypeError("This is an exception") # PLW0133
55 | with suppress(AttributeError): 50 | MyError("This is an exception") # PLW0133
- AttributeError("This is an exception") # PLW0133 - MyValueError("This is an exception") # PLW0133
56 + raise AttributeError("This is an exception") # PLW0133 51 + raise MyValueError("This is an exception") # PLW0133
57 | 52 |
58 | 53 |
59 | # Test case 9: Useless exception statement in parentheses 54 | # Test case 5: Useless exception statement in function
note: This is an unsafe fix and may change runtime behavior note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:61:5 --> useless_exception_statement.py:57:9
| |
59 | # Test case 9: Useless exception statement in parentheses 55 | def func():
60 | def func(): 56 | def inner():
61 | (RuntimeError("This is an exception")) # PLW0133 57 | IndexError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
58 | MyError("This is an exception") # PLW0133
59 | MyValueError("This is an exception") # PLW0133
| |
help: Add `raise` keyword help: Add `raise` keyword
58 | 54 | # Test case 5: Useless exception statement in function
59 | # Test case 9: Useless exception statement in parentheses 55 | def func():
60 | def func(): 56 | def inner():
- (RuntimeError("This is an exception")) # PLW0133 - IndexError("This is an exception") # PLW0133
61 + raise (RuntimeError("This is an exception")) # PLW0133 57 + raise IndexError("This is an exception") # PLW0133
58 | MyError("This is an exception") # PLW0133
59 | MyValueError("This is an exception") # PLW0133
60 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:58:9
|
56 | def inner():
57 | IndexError("This is an exception") # PLW0133
58 | MyError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
59 | MyValueError("This is an exception") # PLW0133
|
help: Add `raise` keyword
55 | def func():
56 | def inner():
57 | IndexError("This is an exception") # PLW0133
- MyError("This is an exception") # PLW0133
58 + raise MyError("This is an exception") # PLW0133
59 | MyValueError("This is an exception") # PLW0133
60 |
61 | inner()
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:59:9
|
57 | IndexError("This is an exception") # PLW0133
58 | MyError("This is an exception") # PLW0133
59 | MyValueError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
60 |
61 | inner()
|
help: Add `raise` keyword
56 | def inner():
57 | IndexError("This is an exception") # PLW0133
58 | MyError("This is an exception") # PLW0133
- MyValueError("This is an exception") # PLW0133
59 + raise MyValueError("This is an exception") # PLW0133
60 |
61 | inner()
62 | 62 |
63 |
64 | # Test case 10: Useless exception statement in continuation
note: This is an unsafe fix and may change runtime behavior note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:66:12 --> useless_exception_statement.py:67:9
| |
64 | # Test case 10: Useless exception statement in continuation
65 | def func(): 65 | def func():
66 | x = 1; (RuntimeError("This is an exception")); y = 2 # PLW0133 66 | while True:
67 | KeyError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
68 | MyError("This is an exception") # PLW0133
69 | MyValueError("This is an exception") # PLW0133
|
help: Add `raise` keyword
64 | # Test case 6: Useless exception statement in while loop
65 | def func():
66 | while True:
- KeyError("This is an exception") # PLW0133
67 + raise KeyError("This is an exception") # PLW0133
68 | MyError("This is an exception") # PLW0133
69 | MyValueError("This is an exception") # PLW0133
70 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:68:9
|
66 | while True:
67 | KeyError("This is an exception") # PLW0133
68 | MyError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
69 | MyValueError("This is an exception") # PLW0133
|
help: Add `raise` keyword
65 | def func():
66 | while True:
67 | KeyError("This is an exception") # PLW0133
- MyError("This is an exception") # PLW0133
68 + raise MyError("This is an exception") # PLW0133
69 | MyValueError("This is an exception") # PLW0133
70 |
71 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:69:9
|
67 | KeyError("This is an exception") # PLW0133
68 | MyError("This is an exception") # PLW0133
69 | MyValueError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: Add `raise` keyword
66 | while True:
67 | KeyError("This is an exception") # PLW0133
68 | MyError("This is an exception") # PLW0133
- MyValueError("This is an exception") # PLW0133
69 + raise MyValueError("This is an exception") # PLW0133
70 |
71 |
72 | # Test case 7: Useless exception statement in abstract class
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:77:13
|
75 | @abstractmethod
76 | def method(self):
77 | NotImplementedError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
78 | MyError("This is an exception") # PLW0133
79 | MyValueError("This is an exception") # PLW0133
|
help: Add `raise` keyword
74 | class Class(ABC):
75 | @abstractmethod
76 | def method(self):
- NotImplementedError("This is an exception") # PLW0133
77 + raise NotImplementedError("This is an exception") # PLW0133
78 | MyError("This is an exception") # PLW0133
79 | MyValueError("This is an exception") # PLW0133
80 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:78:13
|
76 | def method(self):
77 | NotImplementedError("This is an exception") # PLW0133
78 | MyError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
79 | MyValueError("This is an exception") # PLW0133
|
help: Add `raise` keyword
75 | @abstractmethod
76 | def method(self):
77 | NotImplementedError("This is an exception") # PLW0133
- MyError("This is an exception") # PLW0133
78 + raise MyError("This is an exception") # PLW0133
79 | MyValueError("This is an exception") # PLW0133
80 |
81 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:79:13
|
77 | NotImplementedError("This is an exception") # PLW0133
78 | MyError("This is an exception") # PLW0133
79 | MyValueError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: Add `raise` keyword
76 | def method(self):
77 | NotImplementedError("This is an exception") # PLW0133
78 | MyError("This is an exception") # PLW0133
- MyValueError("This is an exception") # PLW0133
79 + raise MyValueError("This is an exception") # PLW0133
80 |
81 |
82 | # Test case 8: Useless exception statement inside context manager
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:85:9
|
83 | def func():
84 | with suppress(Exception):
85 | AttributeError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86 | MyError("This is an exception") # PLW0133
87 | MyValueError("This is an exception") # PLW0133
|
help: Add `raise` keyword
82 | # Test case 8: Useless exception statement inside context manager
83 | def func():
84 | with suppress(Exception):
- AttributeError("This is an exception") # PLW0133
85 + raise AttributeError("This is an exception") # PLW0133
86 | MyError("This is an exception") # PLW0133
87 | MyValueError("This is an exception") # PLW0133
88 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:86:9
|
84 | with suppress(Exception):
85 | AttributeError("This is an exception") # PLW0133
86 | MyError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
87 | MyValueError("This is an exception") # PLW0133
|
help: Add `raise` keyword
83 | def func():
84 | with suppress(Exception):
85 | AttributeError("This is an exception") # PLW0133
- MyError("This is an exception") # PLW0133
86 + raise MyError("This is an exception") # PLW0133
87 | MyValueError("This is an exception") # PLW0133
88 |
89 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:87:9
|
85 | AttributeError("This is an exception") # PLW0133
86 | MyError("This is an exception") # PLW0133
87 | MyValueError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: Add `raise` keyword
84 | with suppress(Exception):
85 | AttributeError("This is an exception") # PLW0133
86 | MyError("This is an exception") # PLW0133
- MyValueError("This is an exception") # PLW0133
87 + raise MyValueError("This is an exception") # PLW0133
88 |
89 |
90 | # Test case 9: Useless exception statement in parentheses
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:92:5
|
90 | # Test case 9: Useless exception statement in parentheses
91 | def func():
92 | (RuntimeError("This is an exception")) # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
93 | (MyError("This is an exception")) # PLW0133
94 | (MyValueError("This is an exception")) # PLW0133
|
help: Add `raise` keyword
89 |
90 | # Test case 9: Useless exception statement in parentheses
91 | def func():
- (RuntimeError("This is an exception")) # PLW0133
92 + raise (RuntimeError("This is an exception")) # PLW0133
93 | (MyError("This is an exception")) # PLW0133
94 | (MyValueError("This is an exception")) # PLW0133
95 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:93:5
|
91 | def func():
92 | (RuntimeError("This is an exception")) # PLW0133
93 | (MyError("This is an exception")) # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
94 | (MyValueError("This is an exception")) # PLW0133
|
help: Add `raise` keyword
90 | # Test case 9: Useless exception statement in parentheses
91 | def func():
92 | (RuntimeError("This is an exception")) # PLW0133
- (MyError("This is an exception")) # PLW0133
93 + raise (MyError("This is an exception")) # PLW0133
94 | (MyValueError("This is an exception")) # PLW0133
95 |
96 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:94:5
|
92 | (RuntimeError("This is an exception")) # PLW0133
93 | (MyError("This is an exception")) # PLW0133
94 | (MyValueError("This is an exception")) # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
help: Add `raise` keyword help: Add `raise` keyword
63 | 91 | def func():
64 | # Test case 10: Useless exception statement in continuation 92 | (RuntimeError("This is an exception")) # PLW0133
65 | def func(): 93 | (MyError("This is an exception")) # PLW0133
- (MyValueError("This is an exception")) # PLW0133
94 + raise (MyValueError("This is an exception")) # PLW0133
95 |
96 |
97 | # Test case 10: Useless exception statement in continuation
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:99:12
|
97 | # Test case 10: Useless exception statement in continuation
98 | def func():
99 | x = 1; (RuntimeError("This is an exception")); y = 2 # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
100 | x = 1; (MyError("This is an exception")); y = 2 # PLW0133
101 | x = 1; (MyValueError("This is an exception")); y = 2 # PLW0133
|
help: Add `raise` keyword
96 |
97 | # Test case 10: Useless exception statement in continuation
98 | def func():
- x = 1; (RuntimeError("This is an exception")); y = 2 # PLW0133 - x = 1; (RuntimeError("This is an exception")); y = 2 # PLW0133
66 + x = 1; raise (RuntimeError("This is an exception")); y = 2 # PLW0133 99 + x = 1; raise (RuntimeError("This is an exception")); y = 2 # PLW0133
67 | 100 | x = 1; (MyError("This is an exception")); y = 2 # PLW0133
68 | 101 | x = 1; (MyValueError("This is an exception")); y = 2 # PLW0133
69 | # Test case 11: Useless warning statement 102 |
note: This is an unsafe fix and may change runtime behavior note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:71:5 --> useless_exception_statement.py:100:12
| |
69 | # Test case 11: Useless warning statement 98 | def func():
70 | def func(): 99 | x = 1; (RuntimeError("This is an exception")); y = 2 # PLW0133
71 | UserWarning("This is an assertion error") # PLW0133 100 | x = 1; (MyError("This is an exception")); y = 2 # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
101 | x = 1; (MyValueError("This is an exception")); y = 2 # PLW0133
| |
help: Add `raise` keyword help: Add `raise` keyword
68 | 97 | # Test case 10: Useless exception statement in continuation
69 | # Test case 11: Useless warning statement 98 | def func():
70 | def func(): 99 | x = 1; (RuntimeError("This is an exception")); y = 2 # PLW0133
- UserWarning("This is an assertion error") # PLW0133 - x = 1; (MyError("This is an exception")); y = 2 # PLW0133
71 + raise UserWarning("This is an assertion error") # PLW0133 100 + x = 1; raise (MyError("This is an exception")); y = 2 # PLW0133
72 | 101 | x = 1; (MyValueError("This is an exception")); y = 2 # PLW0133
73 | 102 |
74 | # Non-violation test cases: PLW0133 103 |
note: This is an unsafe fix and may change runtime behavior note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:126:1 --> useless_exception_statement.py:101:12
| |
124 | import builtins 99 | x = 1; (RuntimeError("This is an exception")); y = 2 # PLW0133
125 | 100 | x = 1; (MyError("This is an exception")); y = 2 # PLW0133
126 | builtins.TypeError("still an exception even though it's an Attribute") 101 | x = 1; (MyValueError("This is an exception")); y = 2 # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: Add `raise` keyword
98 | def func():
99 | x = 1; (RuntimeError("This is an exception")); y = 2 # PLW0133
100 | x = 1; (MyError("This is an exception")); y = 2 # PLW0133
- x = 1; (MyValueError("This is an exception")); y = 2 # PLW0133
101 + x = 1; raise (MyValueError("This is an exception")); y = 2 # PLW0133
102 |
103 |
104 | # Test case 11: Useless warning statement
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:106:5
|
104 | # Test case 11: Useless warning statement
105 | def func():
106 | UserWarning("This is a user warning") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
107 | MyUserWarning("This is a custom user warning") # PLW0133
|
help: Add `raise` keyword
103 |
104 | # Test case 11: Useless warning statement
105 | def func():
- UserWarning("This is a user warning") # PLW0133
106 + raise UserWarning("This is a user warning") # PLW0133
107 | MyUserWarning("This is a custom user warning") # PLW0133
108 |
109 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:107:5
|
105 | def func():
106 | UserWarning("This is a user warning") # PLW0133
107 | MyUserWarning("This is a custom user warning") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: Add `raise` keyword
104 | # Test case 11: Useless warning statement
105 | def func():
106 | UserWarning("This is a user warning") # PLW0133
- MyUserWarning("This is a custom user warning") # PLW0133
107 + raise MyUserWarning("This is a custom user warning") # PLW0133
108 |
109 |
110 | # Test case 12: Useless exception statement at module level
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:113:1
|
111 | import builtins
112 |
113 | builtins.TypeError("still an exception even though it's an Attribute") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
127 | 114 |
128 | PythonFinalizationError("Added in Python 3.13") 115 | PythonFinalizationError("Added in Python 3.13") # PLW0133
| |
help: Add `raise` keyword help: Add `raise` keyword
123 | 110 | # Test case 12: Useless exception statement at module level
124 | import builtins 111 | import builtins
125 | 112 |
- builtins.TypeError("still an exception even though it's an Attribute") - builtins.TypeError("still an exception even though it's an Attribute") # PLW0133
126 + raise builtins.TypeError("still an exception even though it's an Attribute") 113 + raise builtins.TypeError("still an exception even though it's an Attribute") # PLW0133
127 | 114 |
128 | PythonFinalizationError("Added in Python 3.13") 115 | PythonFinalizationError("Added in Python 3.13") # PLW0133
116 |
note: This is an unsafe fix and may change runtime behavior note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:128:1 --> useless_exception_statement.py:115:1
| |
126 | builtins.TypeError("still an exception even though it's an Attribute") 113 | builtins.TypeError("still an exception even though it's an Attribute") # PLW0133
127 | 114 |
128 | PythonFinalizationError("Added in Python 3.13") 115 | PythonFinalizationError("Added in Python 3.13") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116 |
117 | MyError("This is an exception") # PLW0133
| |
help: Add `raise` keyword help: Add `raise` keyword
125 | 112 |
126 | builtins.TypeError("still an exception even though it's an Attribute") 113 | builtins.TypeError("still an exception even though it's an Attribute") # PLW0133
127 | 114 |
- PythonFinalizationError("Added in Python 3.13") - PythonFinalizationError("Added in Python 3.13") # PLW0133
128 + raise PythonFinalizationError("Added in Python 3.13") 115 + raise PythonFinalizationError("Added in Python 3.13") # PLW0133
116 |
117 | MyError("This is an exception") # PLW0133
118 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:117:1
|
115 | PythonFinalizationError("Added in Python 3.13") # PLW0133
116 |
117 | MyError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
118 |
119 | MyValueError("This is an exception") # PLW0133
|
help: Add `raise` keyword
114 |
115 | PythonFinalizationError("Added in Python 3.13") # PLW0133
116 |
- MyError("This is an exception") # PLW0133
117 + raise MyError("This is an exception") # PLW0133
118 |
119 | MyValueError("This is an exception") # PLW0133
120 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:119:1
|
117 | MyError("This is an exception") # PLW0133
118 |
119 | MyValueError("This is an exception") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
120 |
121 | UserWarning("This is a user warning") # PLW0133
|
help: Add `raise` keyword
116 |
117 | MyError("This is an exception") # PLW0133
118 |
- MyValueError("This is an exception") # PLW0133
119 + raise MyValueError("This is an exception") # PLW0133
120 |
121 | UserWarning("This is a user warning") # PLW0133
122 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:121:1
|
119 | MyValueError("This is an exception") # PLW0133
120 |
121 | UserWarning("This is a user warning") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
122 |
123 | MyUserWarning("This is a custom user warning") # PLW0133
|
help: Add `raise` keyword
118 |
119 | MyValueError("This is an exception") # PLW0133
120 |
- UserWarning("This is a user warning") # PLW0133
121 + raise UserWarning("This is a user warning") # PLW0133
122 |
123 | MyUserWarning("This is a custom user warning") # PLW0133
124 |
note: This is an unsafe fix and may change runtime behavior
PLW0133 [*] Missing `raise` statement on exception
--> useless_exception_statement.py:123:1
|
121 | UserWarning("This is a user warning") # PLW0133
122 |
123 | MyUserWarning("This is a custom user warning") # PLW0133
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: Add `raise` keyword
120 |
121 | UserWarning("This is a user warning") # PLW0133
122 |
- MyUserWarning("This is a custom user warning") # PLW0133
123 + raise MyUserWarning("This is a custom user warning") # PLW0133
124 |
125 |
126 | # Non-violation test cases: PLW0133
note: This is an unsafe fix and may change runtime behavior note: This is an unsafe fix and may change runtime behavior