[pycodestyle] Stabilize behavior to ignore stub files in ambiguous-variable-name (E741) (#14405)

This commit is contained in:
Dylan 2024-11-18 07:39:16 -06:00 committed by Micha Reiser
parent 1b180c8342
commit 8b925ea626
4 changed files with 9 additions and 224 deletions

View file

@ -26,8 +26,8 @@ mod tests {
#[test_case(Rule::AmbiguousClassName, Path::new("E742.py"))]
#[test_case(Rule::AmbiguousFunctionName, Path::new("E743.py"))]
#[test_case(Rule::AmbiguousVariableName, Path::new("E741.py"))]
// E741 has different behaviour for `.pyi` files in preview mode;
// this test case checks it still has the old behaviour in stable mode
// E741 is disapplied for `.pyi` files (see #13119 for rationale);
// this fixture tests that we emit no errors there
#[test_case(Rule::AmbiguousVariableName, Path::new("E741.pyi"))]
#[test_case(Rule::LambdaAssignment, Path::new("E731.py"))]
#[test_case(Rule::BareExcept, Path::new("E722.py"))]

View file

@ -9,6 +9,12 @@ use crate::rules::pycodestyle::helpers::is_ambiguous_name;
/// ## What it does
/// Checks for the use of the characters 'l', 'O', or 'I' as variable names.
///
/// Note: This rule is automatically disabled for all stub files
/// (files with `.pyi` extensions). The rule has little relevance for authors
/// of stubs: a well-written stub should aim to faithfully represent the
/// interface of the equivalent .py file as it exists at runtime, including any
/// ambiguously named variables in the runtime module.
///
/// ## Why is this bad?
/// In some fonts, these characters are indistinguishable from the
/// numerals one and zero. When tempted to use 'l', use 'L' instead.
@ -27,14 +33,6 @@ use crate::rules::pycodestyle::helpers::is_ambiguous_name;
/// i = 42
/// ```
///
/// ## Preview mode behavior for stub files
/// In [preview] mode, this rule is automatically disabled for all stub files
/// (files with `.pyi` extensions). The rule has little relevance for authors
/// of stubs: a well-written stub should aim to faithfully represent the
/// interface of the equivalent .py file as it exists at runtime, including any
/// ambiguously named variables in the runtime module.
/// [preview]: https://docs.astral.sh/ruff/preview/
#[violation]
pub struct AmbiguousVariableName(pub String);
@ -48,7 +46,7 @@ impl Violation for AmbiguousVariableName {
/// E741
pub(crate) fn ambiguous_variable_name(checker: &mut Checker, name: &str, range: TextRange) {
if checker.settings.preview.is_enabled() && checker.source_type.is_stub() {
if checker.source_type.is_stub() {
return;
}
if is_ambiguous_name(name) {

View file

@ -1,212 +1,4 @@
---
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
snapshot_kind: text
---
E741.pyi:3:1: E741 Ambiguous variable name: `l`
|
1 | from contextlib import contextmanager
2 |
3 | l = 0
| ^ E741
4 | I = 0
5 | O = 0
|
E741.pyi:4:1: E741 Ambiguous variable name: `I`
|
3 | l = 0
4 | I = 0
| ^ E741
5 | O = 0
6 | l: int = 0
|
E741.pyi:5:1: E741 Ambiguous variable name: `O`
|
3 | l = 0
4 | I = 0
5 | O = 0
| ^ E741
6 | l: int = 0
|
E741.pyi:6:1: E741 Ambiguous variable name: `l`
|
4 | I = 0
5 | O = 0
6 | l: int = 0
| ^ E741
7 |
8 | a, l = 0, 1
|
E741.pyi:8:4: E741 Ambiguous variable name: `l`
|
6 | l: int = 0
7 |
8 | a, l = 0, 1
| ^ E741
9 | [a, l] = 0, 1
10 | a, *l = 0, 1, 2
|
E741.pyi:9:5: E741 Ambiguous variable name: `l`
|
8 | a, l = 0, 1
9 | [a, l] = 0, 1
| ^ E741
10 | a, *l = 0, 1, 2
11 | a = l = 0
|
E741.pyi:10:5: E741 Ambiguous variable name: `l`
|
8 | a, l = 0, 1
9 | [a, l] = 0, 1
10 | a, *l = 0, 1, 2
| ^ E741
11 | a = l = 0
|
E741.pyi:11:5: E741 Ambiguous variable name: `l`
|
9 | [a, l] = 0, 1
10 | a, *l = 0, 1, 2
11 | a = l = 0
| ^ E741
12 |
13 | o = 0
|
E741.pyi:16:5: E741 Ambiguous variable name: `l`
|
14 | i = 0
15 |
16 | for l in range(3):
| ^ E741
17 | pass
|
E741.pyi:20:8: E741 Ambiguous variable name: `l`
|
20 | for a, l in zip(range(3), range(3)):
| ^ E741
21 | pass
|
E741.pyi:25:12: E741 Ambiguous variable name: `l`
|
24 | def f1():
25 | global l
| ^ E741
26 | l = 0
|
E741.pyi:26:5: E741 Ambiguous variable name: `l`
|
24 | def f1():
25 | global l
26 | l = 0
| ^ E741
|
E741.pyi:30:5: E741 Ambiguous variable name: `l`
|
29 | def f2():
30 | l = 0
| ^ E741
31 |
32 | def f3():
|
E741.pyi:33:18: E741 Ambiguous variable name: `l`
|
32 | def f3():
33 | nonlocal l
| ^ E741
34 | l = 1
|
E741.pyi:34:9: E741 Ambiguous variable name: `l`
|
32 | def f3():
33 | nonlocal l
34 | l = 1
| ^ E741
35 |
36 | f3()
|
E741.pyi:40:8: E741 Ambiguous variable name: `l`
|
40 | def f4(l, /, I):
| ^ E741
41 | return l, I, O
|
E741.pyi:40:14: E741 Ambiguous variable name: `I`
|
40 | def f4(l, /, I):
| ^ E741
41 | return l, I, O
|
E741.pyi:44:8: E741 Ambiguous variable name: `l`
|
44 | def f5(l=0, *, I=1):
| ^ E741
45 | return l, I
|
E741.pyi:44:16: E741 Ambiguous variable name: `I`
|
44 | def f5(l=0, *, I=1):
| ^ E741
45 | return l, I
|
E741.pyi:48:9: E741 Ambiguous variable name: `l`
|
48 | def f6(*l, **I):
| ^ E741
49 | return l, I
|
E741.pyi:48:14: E741 Ambiguous variable name: `I`
|
48 | def f6(*l, **I):
| ^ E741
49 | return l, I
|
E741.pyi:57:16: E741 Ambiguous variable name: `l`
|
57 | with ctx1() as l:
| ^ E741
58 | pass
|
E741.pyi:66:20: E741 Ambiguous variable name: `l`
|
66 | with ctx2() as (a, l):
| ^ E741
67 | pass
|
E741.pyi:71:22: E741 Ambiguous variable name: `l`
|
69 | try:
70 | pass
71 | except ValueError as l:
| ^ E741
72 | pass
|
E741.pyi:74:5: E741 Ambiguous variable name: `l`
|
72 | pass
73 |
74 | if (l := 5) > 0:
| ^ E741
75 | pass
|

View file

@ -1,5 +0,0 @@
---
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
snapshot_kind: text
---