Skip to content

Commit d110bd4

Browse files
AlexWaygoodMichaReiser
authored andcommitted
Stabilise 9 pylint rules (#12857)
1 parent eb9c7ae commit d110bd4

8 files changed

+74
-51
lines changed

crates/ruff_linter/src/codes.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,12 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
206206
(Pylint, "E0237") => (RuleGroup::Stable, rules::pylint::rules::NonSlotAssignment),
207207
(Pylint, "E0241") => (RuleGroup::Stable, rules::pylint::rules::DuplicateBases),
208208
(Pylint, "E0302") => (RuleGroup::Stable, rules::pylint::rules::UnexpectedSpecialMethodSignature),
209-
(Pylint, "E0303") => (RuleGroup::Preview, rules::pylint::rules::InvalidLengthReturnType),
209+
(Pylint, "E0303") => (RuleGroup::Stable, rules::pylint::rules::InvalidLengthReturnType),
210210
(Pylint, "E0304") => (RuleGroup::Preview, rules::pylint::rules::InvalidBoolReturnType),
211-
(Pylint, "E0305") => (RuleGroup::Preview, rules::pylint::rules::InvalidIndexReturnType),
211+
(Pylint, "E0305") => (RuleGroup::Stable, rules::pylint::rules::InvalidIndexReturnType),
212212
(Pylint, "E0307") => (RuleGroup::Stable, rules::pylint::rules::InvalidStrReturnType),
213-
(Pylint, "E0308") => (RuleGroup::Preview, rules::pylint::rules::InvalidBytesReturnType),
214-
(Pylint, "E0309") => (RuleGroup::Preview, rules::pylint::rules::InvalidHashReturnType),
213+
(Pylint, "E0308") => (RuleGroup::Stable, rules::pylint::rules::InvalidBytesReturnType),
214+
(Pylint, "E0309") => (RuleGroup::Stable, rules::pylint::rules::InvalidHashReturnType),
215215
(Pylint, "E0604") => (RuleGroup::Stable, rules::pylint::rules::InvalidAllObject),
216216
(Pylint, "E0605") => (RuleGroup::Stable, rules::pylint::rules::InvalidAllFormat),
217217
(Pylint, "E0643") => (RuleGroup::Stable, rules::pylint::rules::PotentialIndexError),
@@ -225,8 +225,8 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
225225
(Pylint, "E1307") => (RuleGroup::Stable, rules::pylint::rules::BadStringFormatType),
226226
(Pylint, "E1310") => (RuleGroup::Stable, rules::pylint::rules::BadStrStripCall),
227227
(Pylint, "E1507") => (RuleGroup::Stable, rules::pylint::rules::InvalidEnvvarValue),
228-
(Pylint, "E1519") => (RuleGroup::Preview, rules::pylint::rules::SingledispatchMethod),
229-
(Pylint, "E1520") => (RuleGroup::Preview, rules::pylint::rules::SingledispatchmethodFunction),
228+
(Pylint, "E1519") => (RuleGroup::Stable, rules::pylint::rules::SingledispatchMethod),
229+
(Pylint, "E1520") => (RuleGroup::Stable, rules::pylint::rules::SingledispatchmethodFunction),
230230
(Pylint, "E1700") => (RuleGroup::Stable, rules::pylint::rules::YieldFromInAsyncFunction),
231231
(Pylint, "E2502") => (RuleGroup::Stable, rules::pylint::rules::BidirectionalUnicode),
232232
(Pylint, "E2510") => (RuleGroup::Stable, rules::pylint::rules::InvalidCharacterBackspace),
@@ -256,7 +256,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
256256
(Pylint, "R1711") => (RuleGroup::Stable, rules::pylint::rules::UselessReturn),
257257
(Pylint, "R1714") => (RuleGroup::Stable, rules::pylint::rules::RepeatedEqualityComparison),
258258
(Pylint, "R1722") => (RuleGroup::Stable, rules::pylint::rules::SysExitAlias),
259-
(Pylint, "R1730") => (RuleGroup::Preview, rules::pylint::rules::IfStmtMinMax),
259+
(Pylint, "R1730") => (RuleGroup::Stable, rules::pylint::rules::IfStmtMinMax),
260260
(Pylint, "R1733") => (RuleGroup::Preview, rules::pylint::rules::UnnecessaryDictIndexLookup),
261261
(Pylint, "R1736") => (RuleGroup::Stable, rules::pylint::rules::UnnecessaryListIndexLookup),
262262
(Pylint, "R2004") => (RuleGroup::Stable, rules::pylint::rules::MagicValueComparison),
@@ -273,13 +273,13 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
273273
(Pylint, "W0129") => (RuleGroup::Stable, rules::pylint::rules::AssertOnStringLiteral),
274274
(Pylint, "W0131") => (RuleGroup::Stable, rules::pylint::rules::NamedExprWithoutContext),
275275
(Pylint, "W0133") => (RuleGroup::Stable, rules::pylint::rules::UselessExceptionStatement),
276-
(Pylint, "W0211") => (RuleGroup::Preview, rules::pylint::rules::BadStaticmethodArgument),
276+
(Pylint, "W0211") => (RuleGroup::Stable, rules::pylint::rules::BadStaticmethodArgument),
277277
(Pylint, "W0245") => (RuleGroup::Stable, rules::pylint::rules::SuperWithoutBrackets),
278278
(Pylint, "W0406") => (RuleGroup::Stable, rules::pylint::rules::ImportSelf),
279279
(Pylint, "W0602") => (RuleGroup::Stable, rules::pylint::rules::GlobalVariableNotAssigned),
280280
(Pylint, "W0603") => (RuleGroup::Stable, rules::pylint::rules::GlobalStatement),
281281
(Pylint, "W0604") => (RuleGroup::Stable, rules::pylint::rules::GlobalAtModuleLevel),
282-
(Pylint, "W0642") => (RuleGroup::Preview, rules::pylint::rules::SelfOrClsAssignment),
282+
(Pylint, "W0642") => (RuleGroup::Stable, rules::pylint::rules::SelfOrClsAssignment),
283283
(Pylint, "W0711") => (RuleGroup::Stable, rules::pylint::rules::BinaryOpException),
284284
(Pylint, "W1501") => (RuleGroup::Stable, rules::pylint::rules::BadOpenMode),
285285
(Pylint, "W1508") => (RuleGroup::Stable, rules::pylint::rules::InvalidEnvvarDefault),

crates/ruff_linter/src/rules/pylint/rules/if_stmt_min_max.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::fix::snippet::SourceCodeSnippet;
1414
///
1515
/// ## Why is this bad?
1616
/// An `if` statement that selects the lesser or greater of two sub-expressions
17-
/// can be replaced with a `min()` or `max()` call respectively. When possible,
17+
/// can be replaced with a `min()` or `max()` call respectively. Where possible,
1818
/// prefer `min()` and `max()`, as they're more concise and readable than the
1919
/// equivalent `if` statements.
2020
///
@@ -194,7 +194,7 @@ enum MinMax {
194194
}
195195

196196
impl MinMax {
197-
fn as_str(self) -> &'static str {
197+
const fn as_str(self) -> &'static str {
198198
match self {
199199
Self::Min => "min",
200200
Self::Max => "max",

crates/ruff_linter/src/rules/pylint/rules/invalid_bytes_return.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ruff_text_size::Ranged;
1212
use crate::checkers::ast::Checker;
1313

1414
/// ## What it does
15-
/// Checks for `__bytes__` implementations that return a type other than `bytes`.
15+
/// Checks for `__bytes__` implementations that return types other than `bytes`.
1616
///
1717
/// ## Why is this bad?
1818
/// The `__bytes__` method should return a `bytes` object. Returning a different

crates/ruff_linter/src/rules/pylint/rules/invalid_hash_return.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ use ruff_text_size::Ranged;
1212
use crate::checkers::ast::Checker;
1313

1414
/// ## What it does
15-
/// Checks for `__hash__` implementations that return a value other than an integer.
15+
/// Checks for `__hash__` implementations that return non-integer values.
1616
///
1717
/// ## Why is this bad?
1818
/// The `__hash__` method should return an integer. Returning a different
1919
/// type may cause unexpected behavior.
2020
///
2121
/// Note: `bool` is a subclass of `int`, so it's technically valid for `__hash__` to
2222
/// return `True` or `False`. However, for consistency with other rules, Ruff will
23-
/// still raise when `__hash__` returns a `bool`.
23+
/// still emit a diagnostic when `__hash__` returns a `bool`.
2424
///
2525
/// ## Example
2626
/// ```python
@@ -36,7 +36,6 @@ use crate::checkers::ast::Checker;
3636
/// return 2
3737
/// ```
3838
///
39-
///
4039
/// ## References
4140
/// - [Python documentation: The `__hash__` method](https://docs.python.org/3/reference/datamodel.html#object.__hash__)
4241
#[violation]

crates/ruff_linter/src/rules/pylint/rules/invalid_index_return.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ruff_text_size::Ranged;
1212
use crate::checkers::ast::Checker;
1313

1414
/// ## What it does
15-
/// Checks for `__index__` implementations that return a value other than an integer.
15+
/// Checks for `__index__` implementations that return non-integer values.
1616
///
1717
/// ## Why is this bad?
1818
/// The `__index__` method should return an integer. Returning a different
@@ -38,7 +38,6 @@ use crate::checkers::ast::Checker;
3838
/// return 2
3939
/// ```
4040
///
41-
///
4241
/// ## References
4342
/// - [Python documentation: The `__index__` method](https://docs.python.org/3/reference/datamodel.html#object.__index__)
4443
#[violation]

crates/ruff_linter/src/rules/pylint/rules/invalid_length_return.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ use ruff_text_size::Ranged;
1212
use crate::checkers::ast::Checker;
1313

1414
/// ## What it does
15-
/// Checks for `__len__` implementations that return values other than a non-negative
16-
/// integer.
15+
/// Checks for `__len__` implementations that return values that are not non-negative
16+
/// integers.
1717
///
1818
/// ## Why is this bad?
1919
/// The `__len__` method should return a non-negative integer. Returning a different
2020
/// value may cause unexpected behavior.
2121
///
2222
/// Note: `bool` is a subclass of `int`, so it's technically valid for `__len__` to
2323
/// return `True` or `False`. However, for consistency with other rules, Ruff will
24-
/// still raise when `__len__` returns a `bool`.
24+
/// still emit a diagnostic when `__len__` returns a `bool`.
2525
///
2626
/// ## Example
2727
/// ```python
@@ -37,7 +37,6 @@ use crate::checkers::ast::Checker;
3737
/// return 2
3838
/// ```
3939
///
40-
///
4140
/// ## References
4241
/// - [Python documentation: The `__len__` method](https://docs.python.org/3/reference/datamodel.html#object.__len__)
4342
#[violation]

crates/ruff_linter/src/rules/pylint/rules/self_or_cls_assignment.rs

+24-14
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,36 @@ use crate::checkers::ast::Checker;
1111
/// Checks for assignment of `self` and `cls` in instance and class methods respectively.
1212
///
1313
/// ## Why is this bad?
14-
/// The identifiers `self` and `cls` are conventional in Python for the first argument of instance
15-
/// methods and class methods, respectively.
14+
/// The identifiers `self` and `cls` are conventional in Python for the first parameter of instance
15+
/// methods and class methods, respectively. Assigning new values to these variables can be
16+
/// confusing for others reading your code; using a different variable name can lead to clearer
17+
/// code.
1618
///
1719
/// ## Example
1820
///
1921
/// ```python
20-
/// class Versions:
21-
/// def add(self, version):
22-
/// self = version
22+
/// class Version:
23+
/// def add(self, other):
24+
/// self = self + other
25+
/// return self
2326
///
2427
/// @classmethod
25-
/// def from_list(cls, versions):
26-
/// cls = versions
28+
/// def superclass(cls):
29+
/// cls = cls.__mro__[-1]
30+
/// return cls
2731
/// ```
2832
///
2933
/// Use instead:
3034
/// ```python
31-
/// class Versions:
32-
/// def add(self, version):
33-
/// self.versions.append(version)
35+
/// class Version:
36+
/// def add(self, other):
37+
/// new_version = self + other
38+
/// return new_version
3439
///
3540
/// @classmethod
36-
/// def from_list(cls, versions):
37-
/// return cls(versions)
41+
/// def superclass(cls):
42+
/// supercls = cls.__mro__[-1]
43+
/// return supercls
3844
/// ```
3945
#[violation]
4046
pub struct SelfOrClsAssignment {
@@ -47,10 +53,14 @@ impl Violation for SelfOrClsAssignment {
4753
let SelfOrClsAssignment { method_type } = self;
4854

4955
format!(
50-
"Invalid assignment to `{}` argument in {method_type} method",
56+
"Confusing assignment to `{}` argument in {method_type} method",
5157
method_type.arg_name(),
5258
)
5359
}
60+
61+
fn fix_title(&self) -> Option<String> {
62+
Some("Consider using a different variable name".to_string())
63+
}
5464
}
5565

5666
/// PLW0127
@@ -130,7 +140,7 @@ enum MethodType {
130140
}
131141

132142
impl MethodType {
133-
fn arg_name(self) -> &'static str {
143+
const fn arg_name(self) -> &'static str {
134144
match self {
135145
MethodType::Instance => "self",
136146
MethodType::Class => "cls",

0 commit comments

Comments
 (0)