Skip to content

Commit b9da430

Browse files
authored
doc(B024): #14455 add annotated but unassgined class variables (#14502)
# Summary Closes #14455, migrated from astral-sh/docs#106.
1 parent 87043a2 commit b9da430

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

crates/ruff_linter/src/rules/flake8_bugbear/rules/abstract_base_class.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,48 @@ use crate::checkers::ast::Checker;
1111
use crate::registry::Rule;
1212

1313
/// ## What it does
14-
/// Checks for abstract classes without abstract methods.
14+
/// Checks for abstract classes without abstract methods or properties.
15+
/// Annotated but unassigned class variables are regarded as abstract.
1516
///
1617
/// ## Why is this bad?
1718
/// Abstract base classes are used to define interfaces. If an abstract base
18-
/// class has no abstract methods, you may have forgotten to add an abstract
19-
/// method to the class or omitted an `@abstractmethod` decorator.
19+
/// class has no abstract methods or properties, you may have forgotten
20+
/// to add an abstract method or property to the class,
21+
/// or omitted an `@abstractmethod` decorator.
2022
///
2123
/// If the class is _not_ meant to be used as an interface, consider removing
2224
/// the `ABC` base class from the class definition.
2325
///
2426
/// ## Example
2527
/// ```python
2628
/// from abc import ABC
29+
/// from typing import ClassVar
2730
///
2831
///
2932
/// class Foo(ABC):
33+
/// class_var: ClassVar[str] = "assigned"
34+
///
3035
/// def method(self):
3136
/// bar()
3237
/// ```
3338
///
3439
/// Use instead:
3540
/// ```python
3641
/// from abc import ABC, abstractmethod
42+
/// from typing import ClassVar
3743
///
3844
///
3945
/// class Foo(ABC):
46+
/// class_var: ClassVar[str] # unassigned
47+
///
4048
/// @abstractmethod
4149
/// def method(self):
4250
/// bar()
4351
/// ```
4452
///
4553
/// ## References
4654
/// - [Python documentation: `abc`](https://docs.python.org/3/library/abc.html)
55+
/// - [Python documentation: `typing.ClassVar`](https://docs.python.org/3/library/typing.html#typing.ClassVar)
4756
#[violation]
4857
pub struct AbstractBaseClassWithoutAbstractMethod {
4958
name: String,
@@ -53,7 +62,7 @@ impl Violation for AbstractBaseClassWithoutAbstractMethod {
5362
#[derive_message_formats]
5463
fn message(&self) -> String {
5564
let AbstractBaseClassWithoutAbstractMethod { name } = self;
56-
format!("`{name}` is an abstract base class, but it has no abstract methods")
65+
format!("`{name}` is an abstract base class, but it has no abstract methods or properties")
5766
}
5867
}
5968

crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B024_B024.py.snap

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,61 @@
22
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
33
snapshot_kind: text
44
---
5-
B024.py:18:7: B024 `Base_1` is an abstract base class, but it has no abstract methods
5+
B024.py:18:7: B024 `Base_1` is an abstract base class, but it has no abstract methods or properties
66
|
77
18 | class Base_1(ABC): # error
88
| ^^^^^^ B024
99
19 | def method(self):
1010
20 | foo()
1111
|
1212

13-
B024.py:71:7: B024 `MetaBase_1` is an abstract base class, but it has no abstract methods
13+
B024.py:71:7: B024 `MetaBase_1` is an abstract base class, but it has no abstract methods or properties
1414
|
1515
71 | class MetaBase_1(metaclass=ABCMeta): # error
1616
| ^^^^^^^^^^ B024
1717
72 | def method(self):
1818
73 | foo()
1919
|
2020

21-
B024.py:82:7: B024 `abc_Base_1` is an abstract base class, but it has no abstract methods
21+
B024.py:82:7: B024 `abc_Base_1` is an abstract base class, but it has no abstract methods or properties
2222
|
2323
82 | class abc_Base_1(abc.ABC): # error
2424
| ^^^^^^^^^^ B024
2525
83 | def method(self):
2626
84 | foo()
2727
|
2828

29-
B024.py:87:7: B024 `abc_Base_2` is an abstract base class, but it has no abstract methods
29+
B024.py:87:7: B024 `abc_Base_2` is an abstract base class, but it has no abstract methods or properties
3030
|
3131
87 | class abc_Base_2(metaclass=abc.ABCMeta): # error
3232
| ^^^^^^^^^^ B024
3333
88 | def method(self):
3434
89 | foo()
3535
|
3636

37-
B024.py:92:7: B024 `notabc_Base_1` is an abstract base class, but it has no abstract methods
37+
B024.py:92:7: B024 `notabc_Base_1` is an abstract base class, but it has no abstract methods or properties
3838
|
3939
92 | class notabc_Base_1(notabc.ABC): # error
4040
| ^^^^^^^^^^^^^ B024
4141
93 | def method(self):
4242
94 | foo()
4343
|
4444

45-
B024.py:132:7: B024 `abc_set_class_variable_2` is an abstract base class, but it has no abstract methods
45+
B024.py:132:7: B024 `abc_set_class_variable_2` is an abstract base class, but it has no abstract methods or properties
4646
|
4747
132 | class abc_set_class_variable_2(ABC): # error (not an abstract attribute)
4848
| ^^^^^^^^^^^^^^^^^^^^^^^^ B024
4949
133 | foo = 2
5050
|
5151

52-
B024.py:136:7: B024 `abc_set_class_variable_3` is an abstract base class, but it has no abstract methods
52+
B024.py:136:7: B024 `abc_set_class_variable_3` is an abstract base class, but it has no abstract methods or properties
5353
|
5454
136 | class abc_set_class_variable_3(ABC): # error (not an abstract attribute)
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^ B024
5656
137 | foo: int = 2
5757
|
5858

59-
B024.py:141:7: B024 `abc_set_class_variable_4` is an abstract base class, but it has no abstract methods
59+
B024.py:141:7: B024 `abc_set_class_variable_4` is an abstract base class, but it has no abstract methods or properties
6060
|
6161
140 | # this doesn't actually declare a class variable, it's just an expression
6262
141 | class abc_set_class_variable_4(ABC): # error

scripts/generate_mkdocs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def generate_rule_metadata(rule_doc: Path) -> None:
110110
For example:
111111
```yaml
112112
---
113-
description: Checks for abstract classes without abstract methods.
113+
description: Checks for abstract classes without abstract methods or properties.
114114
tags:
115115
- B024
116116
---

0 commit comments

Comments
 (0)