Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tibor-reiss committed Apr 15, 2024
1 parent dc3c173 commit 57fab81
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
5 changes: 4 additions & 1 deletion crates/ruff_linter/src/rules/pylint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ mod tests {
#[test_case(Rule::InvalidAllFormat, Path::new("invalid_all_format.py"))]
#[test_case(Rule::InvalidAllObject, Path::new("invalid_all_object.py"))]
#[test_case(Rule::InvalidBoolReturnType, Path::new("invalid_return_type_bool.py"))]
#[test_case(Rule::InvalidLengthReturnType, Path::new("invalid_return_type_length.py"))]
#[test_case(
Rule::InvalidLengthReturnType,
Path::new("invalid_return_type_length.py")
)]
#[test_case(Rule::InvalidStrReturnType, Path::new("invalid_return_type_str.py"))]
#[test_case(Rule::DuplicateBases, Path::new("duplicate_bases.py"))]
#[test_case(Rule::InvalidCharacterBackspace, Path::new("invalid_characters.py"))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use crate::checkers::ast::Checker;
/// Note: Strictly speaking `bool` is a subclass of `int`, thus returning `True`/`False` is valid.
/// To be consistent with other rules (e.g. PLE0305 invalid-index-returned), ruff will raise, compared
/// to pylint which will not raise.
///
/// ## References
/// - [Python documentation: The `__len__` method](https://docs.python.org/3/reference/datamodel.html#object.__len__)
#[violation]
Expand Down Expand Up @@ -67,7 +66,8 @@ pub(crate) fn invalid_length_return(checker: &mut Checker, name: &str, body: &[S
ResolvedPythonType::from(value),
ResolvedPythonType::Unknown
| ResolvedPythonType::Atom(PythonType::Number(NumberLike::Integer))
) || is_negative_integer(value) {
) || is_negative_integer(value)
{
checker
.diagnostics
.push(Diagnostic::new(InvalidLengthReturnType, value.range()));
Expand All @@ -82,9 +82,8 @@ pub(crate) fn invalid_length_return(checker: &mut Checker, name: &str, body: &[S
}

fn is_negative_integer(value: &Expr) -> bool {
let Expr::UnaryOp(ast::ExprUnaryOp { op, .. }) = value else { return false; };
match op {
ast::UnaryOp::USub => true,
_ => false,
}
let Expr::UnaryOp(ast::ExprUnaryOp { op, .. }) = value else {
return false;
};
matches!(op, ast::UnaryOp::USub)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ invalid_return_type_length.py:13:16: PLE0303 `__len__` does not return a non-neg
14 |
15 | class LengthNegative:
|

invalid_return_type_length.py:17:16: PLE0303 `__len__` does not return a non-negative `integer`
|
15 | class LengthNegative:
16 | def __len__(self):
17 | return -42 # [invalid-length-return]
| ^^^ PLE0303
18 |
19 | # TODO: Once Ruff has better type checking
|

0 comments on commit 57fab81

Please sign in to comment.