Use truthiness check in auto_attribs detection (#14562)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

This commit is contained in:
Charlie Marsh 2024-11-23 22:06:10 -05:00 committed by GitHub
parent d285717da8
commit de62e39eba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 54 additions and 15 deletions

View file

@ -99,3 +99,27 @@ class C:
b = field()
c: int = foo()
d = list()
@attr.s(auto_attribs=False) # auto_attribs = False
class C:
a: str = 0
b = field()
c: int = foo()
d = list()
@attr.s(auto_attribs=True) # auto_attribs = True
class C:
a: str = 0
b = field()
c: int = foo()
d = list()
@attr.s(auto_attribs=[1, 2, 3]) # auto_attribs = False
class C:
a: str = 0
b = field()
c: int = foo()
d = list()

View file

@ -311,7 +311,8 @@ pub(crate) fn shell_injection(checker: &mut Checker, call: &ast::ExprCall) {
}
// S603
Some(ShellKeyword {
truthiness: Truthiness::False | Truthiness::Falsey | Truthiness::Unknown,
truthiness:
Truthiness::False | Truthiness::Falsey | Truthiness::None | Truthiness::Unknown,
}) => {
if checker.enabled(Rule::SubprocessWithoutShellEqualsTrue) {
checker.diagnostics.push(Diagnostic::new(

View file

@ -87,6 +87,6 @@ fn exc_info_arg_is_falsey(call: &ExprCall, checker: &mut Checker) -> bool {
.is_some_and(|value| {
let truthiness =
Truthiness::from_expr(value, |id| checker.semantic().has_builtin_binding(id));
matches!(truthiness, Truthiness::False | Truthiness::Falsey)
truthiness.into_bool() == Some(false)
})
}

View file

@ -493,7 +493,7 @@ fn to_pytest_raises_args<'a>(
/// PT015
pub(crate) fn assert_falsy(checker: &mut Checker, stmt: &Stmt, test: &Expr) {
let truthiness = Truthiness::from_expr(test, |id| checker.semantic().has_builtin_binding(id));
if matches!(truthiness, Truthiness::False | Truthiness::Falsey) {
if truthiness.into_bool() == Some(false) {
checker
.diagnostics
.push(Diagnostic::new(PytestAssertAlwaysFalse, stmt.range()));

View file

@ -1,4 +1,4 @@
use ruff_python_ast::helpers::{map_callable, map_subscript};
use ruff_python_ast::helpers::{map_callable, map_subscript, Truthiness};
use ruff_python_ast::{self as ast, Expr, ExprCall};
use ruff_python_semantic::{analyze, BindingKind, Modules, SemanticModel};
@ -148,16 +148,19 @@ pub(super) fn dataclass_kind(
return Some(DataclassKind::Attrs(AttrsAutoAttribs::None));
};
let auto_attribs = match &auto_attribs.value {
Expr::BooleanLiteral(literal) => {
if literal.value {
AttrsAutoAttribs::True
} else {
AttrsAutoAttribs::False
}
let auto_attribs = match Truthiness::from_expr(&auto_attribs.value, |id| {
semantic.has_builtin_binding(id)
}) {
// `auto_attribs` requires an exact `True` to be true
Truthiness::True => AttrsAutoAttribs::True,
// Or an exact `None` to auto-detect.
Truthiness::None => AttrsAutoAttribs::None,
// Otherwise, anything else (even a truthy value, like `1`) is considered `False`.
Truthiness::Truthy | Truthiness::False | Truthiness::Falsey => {
AttrsAutoAttribs::False
}
Expr::NoneLiteral(..) => AttrsAutoAttribs::None,
_ => AttrsAutoAttribs::Unknown,
// Unless, of course, we can't determine the value.
Truthiness::Unknown => AttrsAutoAttribs::Unknown,
};
return Some(DataclassKind::Attrs(auto_attribs));

View file

@ -1,6 +1,5 @@
---
source: crates/ruff_linter/src/rules/ruff/mod.rs
snapshot_kind: text
---
RUF009_attrs_auto_attribs.py:12:14: RUF009 Do not perform function call `foo` in dataclass defaults
|
@ -100,3 +99,12 @@ RUF009_attrs_auto_attribs.py:100:14: RUF009 Do not perform function call `foo` i
| ^^^^^ RUF009
101 | d = list()
|
RUF009_attrs_auto_attribs.py:116:14: RUF009 Do not perform function call `foo` in dataclass defaults
|
114 | a: str = 0
115 | b = field()
116 | c: int = foo()
| ^^^^^ RUF009
117 | d = list()
|