mirror of https://github.com/astral-sh/ruff
parent
412688826c
commit
5d554edace
|
|
@ -106,3 +106,11 @@ def func(x: bool | str):
|
||||||
|
|
||||||
def func(x: int | str):
|
def func(x: int | str):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
from typing import override
|
||||||
|
|
||||||
|
|
||||||
|
@override
|
||||||
|
def func(x: bool):
|
||||||
|
pass
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ use ruff_diagnostics::{Diagnostic, Violation};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::call_path::collect_call_path;
|
use ruff_python_ast::call_path::collect_call_path;
|
||||||
use ruff_python_ast::{Decorator, ParameterWithDefault, Parameters};
|
use ruff_python_ast::{Decorator, ParameterWithDefault, Parameters};
|
||||||
|
use ruff_python_semantic::analyze::visibility;
|
||||||
use ruff_text_size::Ranged;
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
|
|
@ -94,23 +95,18 @@ impl Violation for BooleanDefaultValuePositionalArgument {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// FBT002
|
||||||
pub(crate) fn boolean_default_value_positional_argument(
|
pub(crate) fn boolean_default_value_positional_argument(
|
||||||
checker: &mut Checker,
|
checker: &mut Checker,
|
||||||
name: &str,
|
name: &str,
|
||||||
decorator_list: &[Decorator],
|
decorator_list: &[Decorator],
|
||||||
parameters: &Parameters,
|
parameters: &Parameters,
|
||||||
) {
|
) {
|
||||||
|
// Allow Boolean defaults in explicitly-allowed functions.
|
||||||
if is_allowed_func_def(name) {
|
if is_allowed_func_def(name) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if decorator_list.iter().any(|decorator| {
|
|
||||||
collect_call_path(&decorator.expression)
|
|
||||||
.is_some_and(|call_path| call_path.as_slice() == [name, "setter"])
|
|
||||||
}) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for ParameterWithDefault {
|
for ParameterWithDefault {
|
||||||
parameter,
|
parameter,
|
||||||
default,
|
default,
|
||||||
|
|
@ -121,6 +117,20 @@ pub(crate) fn boolean_default_value_positional_argument(
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.is_some_and(|default| default.is_boolean_literal_expr())
|
.is_some_and(|default| default.is_boolean_literal_expr())
|
||||||
{
|
{
|
||||||
|
// Allow Boolean defaults in setters.
|
||||||
|
if decorator_list.iter().any(|decorator| {
|
||||||
|
collect_call_path(&decorator.expression)
|
||||||
|
.is_some_and(|call_path| call_path.as_slice() == [name, "setter"])
|
||||||
|
}) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allow Boolean defaults in `@override` methods, since they're required to adhere to
|
||||||
|
// the parent signature.
|
||||||
|
if visibility::is_override(decorator_list, checker.semantic()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
BooleanDefaultValuePositionalArgument,
|
BooleanDefaultValuePositionalArgument,
|
||||||
parameter.name.range(),
|
parameter.name.range(),
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ use ruff_diagnostics::Diagnostic;
|
||||||
use ruff_diagnostics::Violation;
|
use ruff_diagnostics::Violation;
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
use ruff_macros::{derive_message_formats, violation};
|
||||||
use ruff_python_ast::call_path::collect_call_path;
|
use ruff_python_ast::call_path::collect_call_path;
|
||||||
|
use ruff_python_semantic::analyze::visibility;
|
||||||
use ruff_python_semantic::SemanticModel;
|
use ruff_python_semantic::SemanticModel;
|
||||||
use ruff_text_size::Ranged;
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
|
|
@ -109,17 +110,11 @@ pub(crate) fn boolean_type_hint_positional_argument(
|
||||||
decorator_list: &[Decorator],
|
decorator_list: &[Decorator],
|
||||||
parameters: &Parameters,
|
parameters: &Parameters,
|
||||||
) {
|
) {
|
||||||
|
// Allow Boolean type hints in explicitly-allowed functions.
|
||||||
if is_allowed_func_def(name) {
|
if is_allowed_func_def(name) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if decorator_list.iter().any(|decorator| {
|
|
||||||
collect_call_path(&decorator.expression)
|
|
||||||
.is_some_and(|call_path| call_path.as_slice() == [name, "setter"])
|
|
||||||
}) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for ParameterWithDefault {
|
for ParameterWithDefault {
|
||||||
parameter,
|
parameter,
|
||||||
default: _,
|
default: _,
|
||||||
|
|
@ -138,9 +133,26 @@ pub(crate) fn boolean_type_hint_positional_argument(
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Allow Boolean type hints in setters.
|
||||||
|
if decorator_list.iter().any(|decorator| {
|
||||||
|
collect_call_path(&decorator.expression)
|
||||||
|
.is_some_and(|call_path| call_path.as_slice() == [name, "setter"])
|
||||||
|
}) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allow Boolean defaults in `@override` methods, since they're required to adhere to
|
||||||
|
// the parent signature.
|
||||||
|
if visibility::is_override(decorator_list, checker.semantic()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If `bool` isn't actually a reference to the `bool` built-in, return.
|
||||||
if !checker.semantic().is_builtin("bool") {
|
if !checker.semantic().is_builtin("bool") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
checker.diagnostics.push(Diagnostic::new(
|
checker.diagnostics.push(Diagnostic::new(
|
||||||
BooleanTypeHintPositionalArgument,
|
BooleanTypeHintPositionalArgument,
|
||||||
parameter.name.range(),
|
parameter.name.range(),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue