From 340264525fd274ae3cb34cd4382fa13c7bd96832 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 18 Feb 2024 10:03:17 -0500 Subject: [PATCH] Allow boolean positionals in `__post_init__` (#10027) Closes https://github.com/astral-sh/ruff/issues/10011. --- .../test/fixtures/flake8_boolean_trap/FBT.py | 13 +++++++++++++ .../src/rules/flake8_boolean_trap/helpers.rs | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_boolean_trap/FBT.py b/crates/ruff_linter/resources/test/fixtures/flake8_boolean_trap/FBT.py index cdb8a97db94d7..e4dcf05345856 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_boolean_trap/FBT.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_boolean_trap/FBT.py @@ -119,3 +119,16 @@ def func(x: bool): settings(True) + + +from dataclasses import dataclass, InitVar + + +@dataclass +class Fit: + force: InitVar[bool] = False + + def __post_init__(self, force: bool) -> None: + print(force) + +Fit(force=True) diff --git a/crates/ruff_linter/src/rules/flake8_boolean_trap/helpers.rs b/crates/ruff_linter/src/rules/flake8_boolean_trap/helpers.rs index 5f8f7a5dd9cc7..49f61539e8d4e 100644 --- a/crates/ruff_linter/src/rules/flake8_boolean_trap/helpers.rs +++ b/crates/ruff_linter/src/rules/flake8_boolean_trap/helpers.rs @@ -45,7 +45,7 @@ pub(super) fn is_allowed_func_call(name: &str) -> bool { /// Returns `true` if a function definition is allowed to use a boolean trap. pub(super) fn is_allowed_func_def(name: &str) -> bool { - matches!(name, "__setitem__") + matches!(name, "__setitem__" | "__post_init__") } /// Returns `true` if an argument is allowed to use a boolean trap. To return