mirror of https://github.com/astral-sh/ruff
Avoid `PYI015` for valid default value without annotation (#4043)
This commit is contained in:
parent
6e8d561090
commit
7fd44a3e12
|
|
@ -84,3 +84,10 @@ class Class1:
|
||||||
|
|
||||||
# We shouldn't emit Y015 for __all__
|
# We shouldn't emit Y015 for __all__
|
||||||
__all__ = ["Class1"]
|
__all__ = ["Class1"]
|
||||||
|
|
||||||
|
# Ignore the following for PYI015
|
||||||
|
field26 = typing.Sequence[int]
|
||||||
|
field27 = list[str]
|
||||||
|
field28 = builtins.str
|
||||||
|
field29 = str
|
||||||
|
field30 = str | bytes | None
|
||||||
|
|
|
||||||
|
|
@ -91,3 +91,10 @@ class Class1:
|
||||||
|
|
||||||
# We shouldn't emit Y015 for __all__
|
# We shouldn't emit Y015 for __all__
|
||||||
__all__ = ["Class1"]
|
__all__ = ["Class1"]
|
||||||
|
|
||||||
|
# Ignore the following for PYI015
|
||||||
|
field26 = typing.Sequence[int]
|
||||||
|
field27 = list[str]
|
||||||
|
field28 = builtins.str
|
||||||
|
field29 = str
|
||||||
|
field30 = str | bytes | None
|
||||||
|
|
|
||||||
|
|
@ -219,6 +219,40 @@ fn is_valid_default_value_with_annotation(
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if an [`Expr`] appears to be a valid PEP 604 union. (e.g. `int | None`)
|
||||||
|
fn is_valid_pep_604_union(annotation: &Expr) -> bool {
|
||||||
|
match &annotation.node {
|
||||||
|
ExprKind::BinOp {
|
||||||
|
left,
|
||||||
|
op: Operator::BitOr,
|
||||||
|
right,
|
||||||
|
} => is_valid_pep_604_union(left) && is_valid_pep_604_union(right),
|
||||||
|
ExprKind::Name { .. }
|
||||||
|
| ExprKind::Subscript { .. }
|
||||||
|
| ExprKind::Attribute { .. }
|
||||||
|
| ExprKind::Constant {
|
||||||
|
value: Constant::None,
|
||||||
|
..
|
||||||
|
} => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if an [`Expr`] appears to be a valid default value without an annotation.
|
||||||
|
fn is_valid_default_value_without_annotation(default: &Expr) -> bool {
|
||||||
|
matches!(
|
||||||
|
&default.node,
|
||||||
|
ExprKind::Call { .. }
|
||||||
|
| ExprKind::Name { .. }
|
||||||
|
| ExprKind::Attribute { .. }
|
||||||
|
| ExprKind::Subscript { .. }
|
||||||
|
| ExprKind::Constant {
|
||||||
|
value: Constant::Ellipsis | Constant::None,
|
||||||
|
..
|
||||||
|
}
|
||||||
|
) || is_valid_pep_604_union(default)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns `true` if an [`Expr`] appears to be `TypeVar`, `TypeVarTuple`, `NewType`, or `ParamSpec`
|
/// Returns `true` if an [`Expr`] appears to be `TypeVar`, `TypeVarTuple`, `NewType`, or `ParamSpec`
|
||||||
/// call.
|
/// call.
|
||||||
fn is_type_var_like_call(context: &Context, expr: &Expr) -> bool {
|
fn is_type_var_like_call(context: &Context, expr: &Expr) -> bool {
|
||||||
|
|
@ -372,6 +406,9 @@ pub fn assignment_default_in_stub(checker: &mut Checker, targets: &[Expr], value
|
||||||
if is_type_var_like_call(&checker.ctx, value) {
|
if is_type_var_like_call(&checker.ctx, value) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if is_valid_default_value_without_annotation(value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if is_valid_default_value_with_annotation(value, checker, true) {
|
if is_valid_default_value_with_annotation(value, checker, true) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue