Rewrite documentation for yield-in-init (#2748)

This commit is contained in:
Charlie Marsh 2023-02-10 17:49:55 -05:00 committed by GitHub
parent 0040991778
commit 3d8fb5be20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 10 deletions

View File

@ -14,13 +14,15 @@ use crate::{
define_violation!( define_violation!(
/// ### What it does /// ### What it does
/// Checks for `__init__` methods that are turned into generators by the /// 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? /// ### Why is this bad?
/// The `__init__` method of a class is used to initialize new objects, not /// The `__init__` method is the constructor for a given Python class,
/// create them. As such, it should not return any value. By including a /// responsible for initializing, rather than creating, new objects.
/// 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 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 /// ### Example
/// ```python /// ```python

View File

@ -4,13 +4,15 @@ Derived from the **Pylint** linter.
### What it does ### What it does
Checks for `__init__` methods that are turned into generators by the 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? ### Why is this bad?
The `__init__` method of a class is used to initialize new objects, not The `__init__` method is the constructor for a given Python class,
create them. As such, it should not return any value. By including a responsible for initializing, rather than creating, new objects.
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 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 ### Example
```python ```python