Skip to content

Commit

Permalink
[FIX] PYI011: recognize Bool / Float / Complex numbers as simpl…
Browse files Browse the repository at this point in the history
…e defaults
  • Loading branch information
XuehaiPan committed Mar 12, 2023
1 parent 7fb7268 commit 42307fd
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,22 @@ fn is_valid_default_value_with_annotation(default: &Expr, checker: &Checker) ->
value: Constant::Bytes(..),
..
} => return checker.locator.slice(default).len() <= 50,
// 123
// True / False
// 3.14
ExprKind::Constant {
value: Constant::Int(..),
value:
Constant::Int(..)
| Constant::Bool(..)
| Constant::Float(..)
..
} => {
return checker.locator.slice(default).len() <= 10;
}
// 2j
ExprKind::Constant {
value:
Constant::Complex(..)
..
} => {
return checker.locator.slice(default).len() <= 10;
Expand All @@ -71,8 +85,18 @@ fn is_valid_default_value_with_annotation(default: &Expr, checker: &Checker) ->
op: Unaryop::USub,
operand,
} => {
// - 1
// - 3.14
if let ExprKind::Constant {
value: Constant::Int(..) | Constant::Float(..),
..
} = &operand.node
{
return checker.locator.slice(operand).len() <= 10;
}
// - 2j
if let ExprKind::Constant {
value: Constant::Int(..),
value: Constant::Complex(..),
..
} = &operand.node
{
Expand All @@ -96,7 +120,7 @@ fn is_valid_default_value_with_annotation(default: &Expr, checker: &Checker) ->
// 1 + 2j
// 1 - 2j
if let ExprKind::Constant {
value: Constant::Int(..),
value: Constant::Int(..) | Constant::Float(..),
..
} = &left.node
{
Expand All @@ -109,7 +133,7 @@ fn is_valid_default_value_with_annotation(default: &Expr, checker: &Checker) ->
// -1 + 2j
// -1 - 2j
if let ExprKind::Constant {
value: Constant::Int(..),
value: Constant::Int(..) | Constant::Float(..),
..
} = &operand.node
{
Expand Down

0 comments on commit 42307fd

Please sign in to comment.