T201/T203 Improve print/pprint docs (#18130)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
Dragon 2025-05-18 17:40:42 +01:00 committed by GitHub
parent dd04ca7f58
commit 660375d429
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 35 additions and 17 deletions

View File

@ -11,26 +11,42 @@ use crate::registry::AsRule;
/// Checks for `print` statements. /// Checks for `print` statements.
/// ///
/// ## Why is this bad? /// ## Why is this bad?
/// `print` statements are useful in some situations (e.g., debugging), but /// `print` statements used for debugging should be omitted from production
/// should typically be omitted from production code. `print` statements can /// code. They can lead the accidental inclusion of sensitive information in
/// lead to the accidental inclusion of sensitive information in logs, and are /// logs, and are not configurable by clients, unlike `logging` statements.
/// not configurable by clients, unlike `logging` statements. ///
/// `print` statements used to produce output as a part of a command-line
/// interface program are not typically a problem.
/// ///
/// ## Example /// ## Example
/// ```python /// ```python
/// def add_numbers(a, b): /// def sum_less_than_four(a, b):
/// print(f"The sum of {a} and {b} is {a + b}") /// print(f"Calling sum_less_than_four")
/// return a + b /// return a + b < 4
/// ``` /// ```
/// ///
/// Use instead: /// The automatic fix will remove the print statement entirely:
///
/// ```python /// ```python
/// def add_numbers(a, b): /// def sum_less_than_four(a, b):
/// return a + b /// return a + b < 4
/// ```
///
/// To keep the line for logging purposes, instead use something like:
///
/// ```python
/// import logging
///
/// logging.basicConfig(level=logging.INFO)
///
///
/// def sum_less_than_four(a, b):
/// logging.debug("Calling sum_less_than_four")
/// return a + b < 4
/// ``` /// ```
/// ///
/// ## Fix safety /// ## Fix safety
/// This rule's fix is marked as unsafe, as it may remove `print` statements /// This rule's fix is marked as unsafe, as it will remove `print` statements
/// that are used beyond debugging purposes. /// that are used beyond debugging purposes.
#[derive(ViolationMetadata)] #[derive(ViolationMetadata)]
pub(crate) struct Print; pub(crate) struct Print;
@ -52,11 +68,13 @@ impl Violation for Print {
/// Checks for `pprint` statements. /// Checks for `pprint` statements.
/// ///
/// ## Why is this bad? /// ## Why is this bad?
/// Like `print` statements, `pprint` statements are useful in some situations /// Like `print` statements, `pprint` statements used for debugging should
/// (e.g., debugging), but should typically be omitted from production code. /// be omitted from production code. They can lead the accidental inclusion
/// `pprint` statements can lead to the accidental inclusion of sensitive /// of sensitive information in logs, and are not configurable by clients,
/// information in logs, and are not configurable by clients, unlike `logging` /// unlike `logging` statements.
/// statements. ///
/// `pprint` statements used to produce output as a part of a command-line
/// interface program are not typically a problem.
/// ///
/// ## Example /// ## Example
/// ```python /// ```python
@ -77,7 +95,7 @@ impl Violation for Print {
/// ``` /// ```
/// ///
/// ## Fix safety /// ## Fix safety
/// This rule's fix is marked as unsafe, as it may remove `pprint` statements /// This rule's fix is marked as unsafe, as it will remove `pprint` statements
/// that are used beyond debugging purposes. /// that are used beyond debugging purposes.
#[derive(ViolationMetadata)] #[derive(ViolationMetadata)]
pub(crate) struct PPrint; pub(crate) struct PPrint;