Skip to content

Commit 660375d

Browse files
T201/T203 Improve print/pprint docs (#18130)
Co-authored-by: Micha Reiser <[email protected]>
1 parent dd04ca7 commit 660375d

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

crates/ruff_linter/src/rules/flake8_print/rules/print_call.rs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,42 @@ use crate::registry::AsRule;
1111
/// Checks for `print` statements.
1212
///
1313
/// ## Why is this bad?
14-
/// `print` statements are useful in some situations (e.g., debugging), but
15-
/// should typically be omitted from production code. `print` statements can
16-
/// lead to the accidental inclusion of sensitive information in logs, and are
17-
/// not configurable by clients, unlike `logging` statements.
14+
/// `print` statements used for debugging should be omitted from production
15+
/// code. They can lead the accidental inclusion of sensitive information in
16+
/// logs, and are not configurable by clients, unlike `logging` statements.
17+
///
18+
/// `print` statements used to produce output as a part of a command-line
19+
/// interface program are not typically a problem.
1820
///
1921
/// ## Example
2022
/// ```python
21-
/// def add_numbers(a, b):
22-
/// print(f"The sum of {a} and {b} is {a + b}")
23-
/// return a + b
23+
/// def sum_less_than_four(a, b):
24+
/// print(f"Calling sum_less_than_four")
25+
/// return a + b < 4
2426
/// ```
2527
///
26-
/// Use instead:
28+
/// The automatic fix will remove the print statement entirely:
29+
///
2730
/// ```python
28-
/// def add_numbers(a, b):
29-
/// return a + b
31+
/// def sum_less_than_four(a, b):
32+
/// return a + b < 4
33+
/// ```
34+
///
35+
/// To keep the line for logging purposes, instead use something like:
36+
///
37+
/// ```python
38+
/// import logging
39+
///
40+
/// logging.basicConfig(level=logging.INFO)
41+
///
42+
///
43+
/// def sum_less_than_four(a, b):
44+
/// logging.debug("Calling sum_less_than_four")
45+
/// return a + b < 4
3046
/// ```
3147
///
3248
/// ## Fix safety
33-
/// This rule's fix is marked as unsafe, as it may remove `print` statements
49+
/// This rule's fix is marked as unsafe, as it will remove `print` statements
3450
/// that are used beyond debugging purposes.
3551
#[derive(ViolationMetadata)]
3652
pub(crate) struct Print;
@@ -52,11 +68,13 @@ impl Violation for Print {
5268
/// Checks for `pprint` statements.
5369
///
5470
/// ## Why is this bad?
55-
/// Like `print` statements, `pprint` statements are useful in some situations
56-
/// (e.g., debugging), but should typically be omitted from production code.
57-
/// `pprint` statements can lead to the accidental inclusion of sensitive
58-
/// information in logs, and are not configurable by clients, unlike `logging`
59-
/// statements.
71+
/// Like `print` statements, `pprint` statements used for debugging should
72+
/// be omitted from production code. They can lead the accidental inclusion
73+
/// of sensitive information in logs, and are not configurable by clients,
74+
/// unlike `logging` statements.
75+
///
76+
/// `pprint` statements used to produce output as a part of a command-line
77+
/// interface program are not typically a problem.
6078
///
6179
/// ## Example
6280
/// ```python
@@ -77,7 +95,7 @@ impl Violation for Print {
7795
/// ```
7896
///
7997
/// ## Fix safety
80-
/// This rule's fix is marked as unsafe, as it may remove `pprint` statements
98+
/// This rule's fix is marked as unsafe, as it will remove `pprint` statements
8199
/// that are used beyond debugging purposes.
82100
#[derive(ViolationMetadata)]
83101
pub(crate) struct PPrint;

0 commit comments

Comments
 (0)