Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permit ClassVar and Final without subscript in RUF012 #6273

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/ruff/resources/test/fixtures/ruff/RUF012.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 7 additions & 9 deletions crates/ruff/src/rules/ruff/rules/helpers.rs
Original file line number Diff line number Diff line change
@@ -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__`.
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]] = []
|