[pylint] Do not report methods with only one EM101-compatible raise (PLR6301) (#15507)

This commit is contained in:
InSync 2025-01-17 16:17:39 +07:00 committed by GitHub
parent 1ecb7ce645
commit dbfdaaded1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 80 additions and 1 deletions

View file

@ -103,3 +103,40 @@ class Foo:
def validate_y(self, attribute, value):
if value <= 0:
raise ValueError("y must be a positive integer")
class Foo:
# No errors
def string(self):
msg = ""
raise NotImplementedError(msg)
def fstring(self, x):
msg = f"{x}"
raise NotImplementedError(msg)
def docstring(self):
"""Lorem ipsum dolor sit amet."""
msg = ""
raise NotImplementedError(msg)
# Errors
def non_simple_assignment(self):
msg = foo = ""
raise NotImplementedError(msg)
def non_simple_assignment_2(self):
msg[0] = ""
raise NotImplementedError(msg)
def unused_message(self):
msg = ""
raise NotImplementedError("")
def unused_message_2(self, x):
msg = ""
raise NotImplementedError(x)

View file

@ -359,7 +359,7 @@ fn call<'a>(
///
/// [`is_stub`]: function_type::is_stub
/// [`EM101`]: https://docs.astral.sh/ruff/rules/raw-string-in-exception/
fn is_not_implemented_stub_with_variable(
pub(crate) fn is_not_implemented_stub_with_variable(
function_def: &StmtFunctionDef,
semantic: &SemanticModel,
) -> bool {

View file

@ -8,6 +8,7 @@ use ruff_python_semantic::{
};
use crate::checkers::ast::Checker;
use crate::rules::flake8_unused_arguments::rules::is_not_implemented_stub_with_variable;
/// ## What it does
/// Checks for the presence of unused `self` parameter in methods definitions.
@ -97,6 +98,7 @@ pub(crate) fn no_self_use(
|| visibility::is_overload(decorator_list, semantic)
|| visibility::is_property(decorator_list, extra_property_decorators, semantic)
|| visibility::is_validator(decorator_list, semantic)
|| is_not_implemented_stub_with_variable(func, semantic)
{
return;
}

View file

@ -35,3 +35,43 @@ no_self_use.py:103:9: PLR6301 Method `validate_y` could be a function, class met
104 | if value <= 0:
105 | raise ValueError("y must be a positive integer")
|
no_self_use.py:128:9: PLR6301 Method `non_simple_assignment` could be a function, class method, or static method
|
126 | # Errors
127 |
128 | def non_simple_assignment(self):
| ^^^^^^^^^^^^^^^^^^^^^ PLR6301
129 | msg = foo = ""
130 | raise NotImplementedError(msg)
|
no_self_use.py:132:9: PLR6301 Method `non_simple_assignment_2` could be a function, class method, or static method
|
130 | raise NotImplementedError(msg)
131 |
132 | def non_simple_assignment_2(self):
| ^^^^^^^^^^^^^^^^^^^^^^^ PLR6301
133 | msg[0] = ""
134 | raise NotImplementedError(msg)
|
no_self_use.py:136:9: PLR6301 Method `unused_message` could be a function, class method, or static method
|
134 | raise NotImplementedError(msg)
135 |
136 | def unused_message(self):
| ^^^^^^^^^^^^^^ PLR6301
137 | msg = ""
138 | raise NotImplementedError("")
|
no_self_use.py:140:9: PLR6301 Method `unused_message_2` could be a function, class method, or static method
|
138 | raise NotImplementedError("")
139 |
140 | def unused_message_2(self, x):
| ^^^^^^^^^^^^^^^^ PLR6301
141 | msg = ""
142 | raise NotImplementedError(x)
|