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

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

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pylint/no_self_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,40 @@ def validate_x(self, attribute, value):
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)
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/rules/pylint/rules/no_self_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/ruff_linter/src/rules/pylint/mod.rs
snapshot_kind: text
---
no_self_use.py:7:9: PLR6301 Method `developer_greeting` could be a function, class method, or static method
|
Expand Down Expand Up @@ -36,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)
|
Loading