mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 10:48:32 +00:00
Permit ClassVar
and Final
without subscript in RUF012 (#6273)
Fix #6267.
This commit is contained in:
parent
b4f224ecea
commit
0d62ad2480
3 changed files with 15 additions and 15 deletions
|
@ -11,6 +11,8 @@ class A:
|
|||
without_annotation = []
|
||||
class_variable: ClassVar[list[int]] = []
|
||||
final_variable: Final[list[int]] = []
|
||||
class_variable_without_subscript: ClassVar = []
|
||||
final_variable_without_subscript: Final = []
|
||||
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use ruff_python_ast::{self as ast, Expr};
|
||||
|
||||
use ruff_python_ast::helpers::map_callable;
|
||||
use ruff_python_ast::helpers::{map_callable, map_subscript};
|
||||
use ruff_python_semantic::{BindingKind, SemanticModel};
|
||||
|
||||
/// Return `true` if the given [`Expr`] is a special class attribute, like `__slots__`.
|
||||
|
@ -28,18 +28,16 @@ pub(super) fn is_dataclass_field(func: &Expr, semantic: &SemanticModel) -> bool
|
|||
|
||||
/// Returns `true` if the given [`Expr`] is a `typing.ClassVar` annotation.
|
||||
pub(super) fn is_class_var_annotation(annotation: &Expr, semantic: &SemanticModel) -> bool {
|
||||
let Expr::Subscript(ast::ExprSubscript { value, .. }) = &annotation else {
|
||||
return false;
|
||||
};
|
||||
semantic.match_typing_expr(value, "ClassVar")
|
||||
// ClassVar can be used either with a subscript `ClassVar[...]` or without (the type is
|
||||
// inferred).
|
||||
semantic.match_typing_expr(map_subscript(annotation), "ClassVar")
|
||||
}
|
||||
|
||||
/// Returns `true` if the given [`Expr`] is a `typing.Final` annotation.
|
||||
pub(super) fn is_final_annotation(annotation: &Expr, semantic: &SemanticModel) -> bool {
|
||||
let Expr::Subscript(ast::ExprSubscript { value, .. }) = &annotation else {
|
||||
return false;
|
||||
};
|
||||
semantic.match_typing_expr(value, "Final")
|
||||
// Final can be used either with a subscript `Final[...]` or without (the type is
|
||||
// inferred).
|
||||
semantic.match_typing_expr(map_subscript(annotation), "Final")
|
||||
}
|
||||
|
||||
/// Returns `true` if the given class is a dataclass.
|
||||
|
|
|
@ -21,14 +21,14 @@ RUF012.py:11:26: RUF012 Mutable class attributes should be annotated with `typin
|
|||
13 | final_variable: Final[list[int]] = []
|
||||
|
|
||||
|
||||
RUF012.py:23:26: RUF012 Mutable class attributes should be annotated with `typing.ClassVar`
|
||||
RUF012.py:25:26: RUF012 Mutable class attributes should be annotated with `typing.ClassVar`
|
||||
|
|
||||
21 | mutable_default: list[int] = []
|
||||
22 | immutable_annotation: Sequence[int] = []
|
||||
23 | without_annotation = []
|
||||
23 | mutable_default: list[int] = []
|
||||
24 | immutable_annotation: Sequence[int] = []
|
||||
25 | without_annotation = []
|
||||
| ^^ RUF012
|
||||
24 | perfectly_fine: list[int] = field(default_factory=list)
|
||||
25 | class_variable: ClassVar[list[int]] = []
|
||||
26 | perfectly_fine: list[int] = field(default_factory=list)
|
||||
27 | class_variable: ClassVar[list[int]] = []
|
||||
|
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue