Skip to content

Commit

Permalink
doc(B024): astral-sh#14455 add annotated but unassgined class variables
Browse files Browse the repository at this point in the history
  • Loading branch information
cmp0xff committed Nov 20, 2024
1 parent 3c52d2d commit 8d22dfc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,47 @@ use crate::checkers::ast::Checker;
use crate::registry::Rule;

/// ## What it does
/// Checks for abstract classes without abstract methods.
/// Checks for abstract classes without abstract methods or abstract class variables.
/// Annotated but unassigned class variables are regarded as abstract.
///
/// ## Why is this bad?
/// Abstract base classes are used to define interfaces. If an abstract base
/// class has no abstract methods, you may have forgotten to add an abstract
/// method to the class or omitted an `@abstractmethod` decorator.
/// class has no abstract methods or abstract class variables, you may have forgotten
/// to add an abstract method to the class or omitted an `@abstractmethod` decorator.
///
/// If the class is _not_ meant to be used as an interface, consider removing
/// the `ABC` base class from the class definition.
///
/// ## Example
/// ```python
/// from abc import ABC
/// from typing import ClassVar
///
///
/// class Foo(ABC):
/// class_var: ClassVar[str] = "assigned"
///
/// def method(self):
/// bar()
/// ```
///
/// Use instead:
/// ```python
/// from abc import ABC, abstractmethod
/// from typing import ClassVar
///
///
/// class Foo(ABC):
/// class_var: ClassVar[str] # unassigned
///
/// @abstractmethod
/// def method(self):
/// bar()
/// ```
///
/// ## References
/// - [Python documentation: `abc`](https://docs.python.org/3/library/abc.html)
/// - [Python documentation: `typing.ClassVar`](https://docs.python.org/3/library/typing.html#typing.ClassVar)
#[violation]
pub struct AbstractBaseClassWithoutAbstractMethod {
name: String,
Expand All @@ -53,7 +61,7 @@ impl Violation for AbstractBaseClassWithoutAbstractMethod {
#[derive_message_formats]
fn message(&self) -> String {
let AbstractBaseClassWithoutAbstractMethod { name } = self;
format!("`{name}` is an abstract base class, but it has no abstract methods")
format!("`{name}` is an abstract base class, but it has neither abstract methods nor abstract class variables")
}
}

Expand Down
3 changes: 2 additions & 1 deletion scripts/generate_mkdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def generate_rule_metadata(rule_doc: Path) -> None:
For example:
```yaml
---
description: Checks for abstract classes without abstract methods.
description: Checks for abstract classes without abstract methods or abstract class
variables.
tags:
- B024
---
Expand Down

0 comments on commit 8d22dfc

Please sign in to comment.