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!(
/// ### 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