-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
] Implement invalid-length-returned
(E0303
)
#10963
[pylint
] Implement invalid-length-returned
(E0303
)
#10963
Conversation
Try bumping the value in |
CodSpeed Performance ReportMerging #10963 will not alter performanceComparing Summary
|
ee331b8
to
57fab81
Compare
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
PYI001 | 1 | 1 | 0 | 0 | 0 |
Linter (preview)
ℹ️ ecosystem check detected linter changes. (+4 -0 violations, +0 -0 fixes in 3 projects; 41 projects unchanged)
ibis-project/ibis (+1 -0 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview
+ ibis/expr/types/relations.py:831:9: PLE0303 `__len__` does not return a non-negative integer
pandas-dev/pandas (+2 -0 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview
+ pandas/core/arrays/base.py:483:9: PLE0303 `__len__` does not return a non-negative integer + pandas/core/base.py:349:9: PLE0303 `__len__` does not return a non-negative integer
python/typeshed (+1 -0 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select E,F,FA,I,PYI,RUF,UP,W
+ stdlib/typing.pyi:330:10: PYI001 Name of private `TypeVar` must start with `_`
Changes by rule (2 rules affected)
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
PLE0303 | 3 | 3 | 0 | 0 | 0 |
PYI001 | 1 | 1 | 0 | 0 | 0 |
@charliermarsh Is the performance drop of 4% also related to this please? The newly introduced rule is quite lightweight. Solved, thanks @MichaReiser |
@@ -97,6 +97,9 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) { | |||
if checker.enabled(Rule::InvalidBoolReturnType) { | |||
pylint::rules::invalid_bool_return(checker, name, body); | |||
} | |||
if checker.enabled(Rule::InvalidLengthReturnType) { | |||
pylint::rules::invalid_length_return(checker, name, body); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: You can pass the entire function definition instead of just passing the name and body
pylint::rules::invalid_length_return(checker, name, body); | |
pylint::rules::invalid_length_return(checker, function_def); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the current bool and str cases as a blueprint, which passed also name and body. What would be the advantage of passing the function_def please? I would still need to reference name and body.
let Expr::UnaryOp(ast::ExprUnaryOp { op, .. }) = value else { | ||
return false; | ||
}; | ||
matches!(op, ast::UnaryOp::USub) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
let Expr::UnaryOp(ast::ExprUnaryOp { op, .. }) = value else { | |
return false; | |
}; | |
matches!(op, ast::UnaryOp::USub) | |
matches!( | |
value, | |
Expr::UnaryOp(ast::ExprUnaryOp { | |
op: ast::UnaryOp::USub, | |
.. | |
}) | |
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, done!
if !matches!( | ||
ResolvedPythonType::from(value), | ||
ResolvedPythonType::Unknown | ||
| ResolvedPythonType::Atom(PythonType::Number(NumberLike::Integer)) | ||
) || is_negative_integer(value) | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could make sense to change the order of the checks because is_negative_integer
is relatively simple compared to ResolvedPythonType::from
if !matches!( | |
ResolvedPythonType::from(value), | |
ResolvedPythonType::Unknown | |
| ResolvedPythonType::Atom(PythonType::Number(NumberLike::Integer)) | |
) || is_negative_integer(value) | |
{ | |
if is_negative_integer(value) || | |
!matches!( | |
ResolvedPythonType::from(value), | |
ResolvedPythonType::Unknown | |
| ResolvedPythonType::Atom(PythonType::Number(NumberLike::Integer)) | |
) | |
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TLDR: changed.
I was not sure about which should be first because I would expect that there are more cases where the return type is wrong, compared to where the type is ok (int) but the sign not.
@tibor-reiss I bumped the index (and fixed the perf regression). You can rebase and we should then see the impact of your rule. |
57fab81
to
2b50cd0
Compare
pylint
] Implement invalid-length-returned
(E0303
)
Add pylint rule invalid-length-returned (PLE0303)
See #970 for rules
Test Plan:
cargo test
TBD: from the description: "Strictly speaking
bool
is a subclass ofint
, thus returningTrue
/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."