Skip to content

Commit 3e81403

Browse files
authored
Add pygrep-hooks documentation (astral-sh#4131)
1 parent 3c9f5e2 commit 3e81403

5 files changed

Lines changed: 95 additions & 0 deletions

File tree

_typos.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ trivias = "trivias"
66
hel = "hel"
77
whos = "whos"
88
spawnve = "spawnve"
9+
ned = "ned"

crates/ruff/src/rules/pygrep_hooks/rules/blanket_noqa.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@ use ruff_diagnostics::{Diagnostic, Violation};
66
use ruff_macros::{derive_message_formats, violation};
77
use ruff_python_ast::newlines::Line;
88

9+
/// ## What it does
10+
/// Check for `noqa` annotations that suppress all diagnostics, as opposed to
11+
/// targeting specific diagnostics.
12+
///
13+
/// ## Why is this bad?
14+
/// Suppressing all diagnostics can hide issues in the code.
15+
///
16+
/// Blanket `noqa` annotations are also more difficult to interpret and
17+
/// maintain, as the annotation does not clarify which diagnostics are intended
18+
/// to be suppressed.
19+
///
20+
/// ## Example
21+
/// ```python
22+
/// from .base import * # noqa
23+
/// ```
24+
///
25+
/// Use instead:
26+
/// ```python
27+
/// from .base import * # noqa: F403
28+
/// ```
29+
///
30+
/// ## References
31+
/// - [Ruff documentation](https://beta.ruff.rs/docs/configuration/#error-suppression)
932
#[violation]
1033
pub struct BlanketNOQA;
1134

crates/ruff/src/rules/pygrep_hooks/rules/blanket_type_ignore.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,29 @@ use ruff_diagnostics::{Diagnostic, Violation};
77
use ruff_macros::{derive_message_formats, violation};
88
use ruff_python_ast::newlines::Line;
99

10+
/// ## What it does
11+
/// Check for `type: ignore` annotations that suppress all type warnings, as
12+
/// opposed to targeting specific type warnings.
13+
///
14+
/// ## Why is this bad?
15+
/// Suppressing all warnings can hide issues in the code.
16+
///
17+
/// Blanket `type: ignore` annotations are also more difficult to interpret and
18+
/// maintain, as the annotation does not clarify which warnings are intended
19+
/// to be suppressed.
20+
///
21+
/// ## Example
22+
/// ```python
23+
/// from foo import secrets # type: ignore
24+
/// ```
25+
///
26+
/// Use instead:
27+
/// ```python
28+
/// from foo import secrets # type: ignore[attr-defined]
29+
/// ```
30+
///
31+
/// ## References
32+
/// - [mypy](https://mypy.readthedocs.io/en/stable/common_issues.html#spurious-errors-and-locally-silencing-the-checker)
1033
#[violation]
1134
pub struct BlanketTypeIgnore;
1235

crates/ruff/src/rules/pygrep_hooks/rules/deprecated_log_warn.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@ use ruff_macros::{derive_message_formats, violation};
55

66
use crate::checkers::ast::Checker;
77

8+
/// ## What it does
9+
/// Check for usages of the deprecated `warn` method from the `logging` module.
10+
///
11+
/// ## Why is this bad?
12+
/// The `warn` method is deprecated. Use `warning` instead.
13+
///
14+
/// ## Example
15+
/// ```python
16+
/// import logging
17+
///
18+
///
19+
/// def foo():
20+
/// logging.warn("Something happened")
21+
/// ```
22+
///
23+
/// Use instead:
24+
/// ```python
25+
/// import logging
26+
///
27+
/// def foo():
28+
/// logging.warning("Something happened")
29+
/// ```
30+
///
31+
/// ## References
32+
/// - [Python documentation](https://docs.python.org/3/library/logging.html#logging.Logger.warning)
833
#[violation]
934
pub struct DeprecatedLogWarn;
1035

crates/ruff/src/rules/pygrep_hooks/rules/no_eval.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@ use ruff_macros::{derive_message_formats, violation};
55

66
use crate::checkers::ast::Checker;
77

8+
/// ## What it does
9+
/// Checks for usages of the builtin `eval()` function.
10+
///
11+
/// ## Why is this bad?
12+
/// The `eval()` function is insecure as it enables arbitrary code execution.
13+
///
14+
/// ## Example
15+
/// ```python
16+
/// def foo():
17+
/// x = eval(input("Enter a number: "))
18+
/// ...
19+
/// ```
20+
///
21+
/// Use instead:
22+
/// ```python
23+
/// def foo():
24+
/// x = input("Enter a number: ")
25+
/// ...
26+
/// ```
27+
///
28+
/// ## References
29+
/// - [Python documentation](https://docs.python.org/3/library/functions.html#eval)
30+
/// - [_Eval really is dangerous_ by Ned Batchelder](https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html)
831
#[violation]
932
pub struct Eval;
1033

0 commit comments

Comments
 (0)