diff --git a/crates/ruff/resources/test/fixtures/flake8_pyi/PYI014.pyi b/crates/ruff/resources/test/fixtures/flake8_pyi/PYI014.pyi index ad645dfb5d45b..1aed3543ef44c 100644 --- a/crates/ruff/resources/test/fixtures/flake8_pyi/PYI014.pyi +++ b/crates/ruff/resources/test/fixtures/flake8_pyi/PYI014.pyi @@ -43,3 +43,6 @@ def f21( def f22( x=-42.5j + 4.3j, # Error PYI014 ) -> None: ... +def f23( + x=True, # OK +) -> None: ... diff --git a/crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs b/crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs index 14e213b32cb2f..ca8affdc63cb9 100644 --- a/crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs +++ b/crates/ruff/src/rules/flake8_pyi/rules/simple_defaults.rs @@ -61,42 +61,59 @@ fn is_valid_default_value_with_annotation(default: &Expr, checker: &Checker) -> value: Constant::Bytes(..), .. } => return checker.locator.slice(default).len() <= 50, + // Ex) `123`, `True`, `False`, `3.14` ExprKind::Constant { - value: Constant::Int(..), + value: Constant::Int(..) | Constant::Bool(..) | Constant::Float(..), .. } => { return checker.locator.slice(default).len() <= 10; } + // Ex) `2j` + ExprKind::Constant { + value: Constant::Complex { real, .. }, + .. + } => { + if *real == 0.0 { + return checker.locator.slice(default).len() <= 10; + } + } ExprKind::UnaryOp { op: Unaryop::USub, operand, } => { + // Ex) `-1`, `-3.14` if let ExprKind::Constant { - value: Constant::Int(..), + value: Constant::Int(..) | Constant::Float(..), .. } = &operand.node { return checker.locator.slice(operand).len() <= 10; } + // Ex) `-2j` + if let ExprKind::Constant { + value: Constant::Complex { real, .. }, + .. + } = &operand.node + { + if *real == 0.0 { + return checker.locator.slice(operand).len() <= 10; + } + } } ExprKind::BinOp { left, op: Operator::Add | Operator::Sub, right, } => { - // 1 + 2j - // 1 - 2j - // -1 - 2j - // -1 + 2j + // Ex) `1 + 2j`, `1 - 2j`, `-1 - 2j`, `-1 + 2j` if let ExprKind::Constant { value: Constant::Complex { .. }, .. } = right.node { - // 1 + 2j - // 1 - 2j + // Ex) `1 + 2j`, `1 - 2j` if let ExprKind::Constant { - value: Constant::Int(..), + value: Constant::Int(..) | Constant::Float(..), .. } = &left.node { @@ -106,10 +123,9 @@ fn is_valid_default_value_with_annotation(default: &Expr, checker: &Checker) -> operand, } = &left.node { - // -1 + 2j - // -1 - 2j + // Ex) `-1 + 2j`, `-1 - 2j` if let ExprKind::Constant { - value: Constant::Int(..), + value: Constant::Int(..) | Constant::Float(..), .. } = &operand.node { @@ -118,7 +134,7 @@ fn is_valid_default_value_with_annotation(default: &Expr, checker: &Checker) -> } } } - // `sys.stdin`, etc. + // Ex) `sys.stdin`, etc. ExprKind::Attribute { .. } => { if checker .ctx