mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Rewrite documentation for yield-in-init (#2748)
This commit is contained in:
parent
0040991778
commit
3d8fb5be20
2 changed files with 14 additions and 10 deletions
|
@ -14,13 +14,15 @@ use crate::{
|
|||
define_violation!(
|
||||
/// ### What it does
|
||||
/// Checks for `__init__` methods that are turned into generators by the
|
||||
/// inclusion of `yield` or `yield from` statements.
|
||||
/// inclusion of `yield` or `yield from` expressions.
|
||||
///
|
||||
/// ### Why is this bad?
|
||||
/// The `__init__` method of a class is used to initialize new objects, not
|
||||
/// create them. As such, it should not return any value. By including a
|
||||
/// yield expression in the method turns it into a generator method. On
|
||||
/// calling, it will return a generator resulting in a runtime error.
|
||||
/// The `__init__` method is the constructor for a given Python class,
|
||||
/// responsible for initializing, rather than creating, new objects.
|
||||
///
|
||||
/// The `__init__` method has to return `None`. By including a `yield` or
|
||||
/// `yield from` expression in an `__init__`, the method will return a
|
||||
/// generator object when called at runtime, resulting in a runtime error.
|
||||
///
|
||||
/// ### Example
|
||||
/// ```python
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue