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] removed dunder methods in Python 3 (PLW3201) #13194

Merged
merged 7 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def __prepare__():
def __mro_entries__(self, bases):
pass

# Deprecated in Python 3
def __unicode__(self):
pass

def __foo_bar__(): # this is not checked by the [bad-dunder-name] rule
...
27 changes: 27 additions & 0 deletions crates/ruff_linter/src/rules/pylint/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,30 @@ pub(super) fn is_known_dunder_method(method: &str) -> bool {
| "_generate_next_value_"
)
}

/// Returns `true` if the method is a known dunder method that is only allowed in Python 2.
pub(super) fn is_deprecated_dunder_method_in_python3(method: &str) -> bool {
matches!(
method,
"__unicode__"
| "__div__"
| "__rdiv__"
| "__idiv__"
| "__nonzero__"
| "__metaclass__"
| "__cmp__"
| "__getslice__"
| "__setslice__"
| "__delslice__"
| "__oct__"
| "__hex__"
| "__members__"
| "__method__"
| "__corece__"
| "__long__"
| "__rcmp__"
| "__iop__"
| "__rop__"
| "__op__"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ use ruff_python_ast::identifier::Identifier;
use ruff_python_semantic::analyze::visibility;

use crate::checkers::ast::Checker;
use crate::rules::pylint::helpers::is_deprecated_dunder_method_in_python3;
use crate::rules::pylint::helpers::is_known_dunder_method;

#[derive(PartialEq, Eq, Debug)]
enum Kind {
Misspelled,
DeprecatedInPython3,
}

/// ## What it does
/// Checks for misspelled and unknown dunder names in method definitions.
/// Checks for misspelled, unknown, and deprecated dunder names in method definitions.
///
/// ## Why is this bad?
/// Misspelled dunder name methods may cause your code to not function
/// Misspelled or deprecated dunder name methods may cause your code to not function
/// as expected.
///
/// Since dunder methods are associated with customizing the behavior
Expand Down Expand Up @@ -45,13 +52,19 @@ use crate::rules::pylint::helpers::is_known_dunder_method;
#[violation]
pub struct BadDunderMethodName {
name: String,
kind: Kind,
}

impl Violation for BadDunderMethodName {
#[derive_message_formats]
fn message(&self) -> String {
let BadDunderMethodName { name } = self;
format!("Bad or misspelled dunder method name `{name}`")
let BadDunderMethodName { name, kind } = self;
match kind {
Kind::Misspelled => format!("Bad or misspelled dunder method name `{name}`"),
Kind::DeprecatedInPython3 => {
format!("Deprecated dunder method name in Python 3 `{name}`")
}
}
}
}

Expand All @@ -78,9 +91,16 @@ pub(crate) fn bad_dunder_method_name(checker: &mut Checker, method: &ast::StmtFu
return;
}

let kind = if is_deprecated_dunder_method_in_python3(&method.name) {
Kind::DeprecatedInPython3
} else {
Kind::Misspelled
};

checker.diagnostics.push(Diagnostic::new(
BadDunderMethodName {
name: method.name.to_string(),
kind,
},
method.identifier(),
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@ bad_dunder_method_name.py:23:9: PLW3201 Bad or misspelled dunder method name `__
25 | pass
|


bad_dunder_method_name.py:98:9: PLW3201 Deprecated dunder method name in Python 3 `__unicode__`
|
97 | # Deprecated in Python 3
98 | def __unicode__(self):
| ^^^^^^^^^^^ PLW3201
99 | pass
|
Loading