mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +00:00
Allow Final
assignments in stubs (#5490)
## Summary This fixes one incompatibility with `flake8-pyi`, and gives us a clean pass on `typeshed`.
This commit is contained in:
parent
ed1dd09d02
commit
8de5a3d29d
3 changed files with 15 additions and 0 deletions
|
@ -91,3 +91,4 @@ field27 = list[str]
|
|||
field28 = builtins.str
|
||||
field29 = str
|
||||
field30 = str | bytes | None
|
||||
field31: typing.Final = field30
|
||||
|
|
|
@ -98,3 +98,4 @@ field27 = list[str]
|
|||
field28 = builtins.str
|
||||
field29 = str
|
||||
field30 = str | bytes | None
|
||||
field31: typing.Final = field30
|
||||
|
|
|
@ -298,6 +298,16 @@ fn is_special_assignment(target: &Expr, semantic: &SemanticModel) -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if this is an assignment to a simple `Final`-annotated variable.
|
||||
fn is_final_assignment(annotation: &Expr, value: &Expr, semantic: &SemanticModel) -> bool {
|
||||
if matches!(value, Expr::Name(_) | Expr::Attribute(_)) {
|
||||
if semantic.match_typing_expr(annotation, "Final") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Returns `true` if the a class is an enum, based on its base classes.
|
||||
fn is_enum(bases: &[Expr], semantic: &SemanticModel) -> bool {
|
||||
return bases.iter().any(|expr| {
|
||||
|
@ -438,6 +448,9 @@ pub(crate) fn annotated_assignment_default_in_stub(
|
|||
if is_type_var_like_call(value, checker.semantic()) {
|
||||
return;
|
||||
}
|
||||
if is_final_assignment(annotation, value, checker.semantic()) {
|
||||
return;
|
||||
}
|
||||
if is_valid_default_value_with_annotation(value, true, checker.locator, checker.semantic()) {
|
||||
return;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue