Skip to content

Commit 10d5415

Browse files
Ignore certain flake8-pyi errors within function bodies (#4029)
1 parent 827cbe7 commit 10d5415

File tree

7 files changed

+46
-14
lines changed

7 files changed

+46
-14
lines changed

crates/ruff/resources/test/fixtures/flake8_pyi/PYI001.py

+4
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@
1111
_TTuple = TypeVarTuple("_TTuple") # OK
1212

1313
_P = ParamSpec("_P") # OK
14+
15+
16+
def f():
17+
T = TypeVar("T") # OK

crates/ruff/resources/test/fixtures/flake8_pyi/PYI001.pyi

+3
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ _T = TypeVar("_T") # OK
1111
_TTuple = TypeVarTuple("_TTuple") # OK
1212

1313
_P = ParamSpec("_P") # OK
14+
15+
def f():
16+
T = TypeVar("T") # OK

crates/ruff/resources/test/fixtures/flake8_pyi/PYI015.py

+4
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@
4646
field23 = "foo" + "bar" # Y015 Only simple default values are allowed for assignments
4747
field24 = b"foo" + b"bar" # Y015 Only simple default values are allowed for assignments
4848
field25 = 5 * 5 # Y015 Only simple default values are allowed for assignments
49+
50+
# We shouldn't emit Y015 within functions
51+
def f():
52+
field26: list[int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

crates/ruff/resources/test/fixtures/flake8_pyi/PYI015.pyi

+4
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,7 @@ field229: dict[int, int] = {1: 2, **{3: 4}} # Y015 Only simple default values a
5353
field23 = "foo" + "bar" # Y015 Only simple default values are allowed for assignments
5454
field24 = b"foo" + b"bar" # Y015 Only simple default values are allowed for assignments
5555
field25 = 5 * 5 # Y015 Only simple default values are allowed for assignments
56+
57+
# We shouldn't emit Y015 within functions
58+
def f():
59+
field26: list[int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

crates/ruff/src/checkers/ast/mod.rs

+22-13
Original file line numberDiff line numberDiff line change
@@ -1791,12 +1791,6 @@ where
17911791
}
17921792
}
17931793

1794-
if self.is_stub {
1795-
if self.settings.rules.enabled(Rule::UnprefixedTypeParam) {
1796-
flake8_pyi::rules::prefix_type_params(self, value, targets);
1797-
}
1798-
}
1799-
18001794
if self.settings.rules.enabled(Rule::GlobalStatement) {
18011795
for target in targets.iter() {
18021796
if let ExprKind::Name { id, .. } = &target.node {
@@ -1837,8 +1831,20 @@ where
18371831
}
18381832

18391833
if self.is_stub {
1840-
if self.settings.rules.enabled(Rule::AssignmentDefaultInStub) {
1841-
flake8_pyi::rules::assignment_default_in_stub(self, value, None);
1834+
if self
1835+
.settings
1836+
.rules
1837+
.any_enabled(&[Rule::UnprefixedTypeParam, Rule::AssignmentDefaultInStub])
1838+
{
1839+
// Ignore assignments in function bodies; those are covered by other rules.
1840+
if !self.ctx.scopes().any(|scope| scope.kind.is_function()) {
1841+
if self.settings.rules.enabled(Rule::UnprefixedTypeParam) {
1842+
flake8_pyi::rules::prefix_type_params(self, value, targets);
1843+
}
1844+
if self.settings.rules.enabled(Rule::AssignmentDefaultInStub) {
1845+
flake8_pyi::rules::assignment_default_in_stub(self, value, None);
1846+
}
1847+
}
18421848
}
18431849
}
18441850
}
@@ -1874,11 +1880,14 @@ where
18741880
if self.is_stub {
18751881
if let Some(value) = value {
18761882
if self.settings.rules.enabled(Rule::AssignmentDefaultInStub) {
1877-
flake8_pyi::rules::assignment_default_in_stub(
1878-
self,
1879-
value,
1880-
Some(annotation),
1881-
);
1883+
// Ignore assignments in function bodies; those are covered by other rules.
1884+
if !self.ctx.scopes().any(|scope| scope.kind.is_function()) {
1885+
flake8_pyi::rules::assignment_default_in_stub(
1886+
self,
1887+
value,
1888+
Some(annotation),
1889+
);
1890+
}
18821891
}
18831892
}
18841893
}

crates/ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI015_PYI015.pyi.snap

+8
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ PYI015.pyi:53:11: PYI015 [*] Only simple default values allowed for assignments
187187
53 |+field23 = ... # Y015 Only simple default values are allowed for assignments
188188
54 54 | field24 = b"foo" + b"bar" # Y015 Only simple default values are allowed for assignments
189189
55 55 | field25 = 5 * 5 # Y015 Only simple default values are allowed for assignments
190+
56 56 |
190191

191192
PYI015.pyi:54:11: PYI015 [*] Only simple default values allowed for assignments
192193
|
@@ -205,13 +206,17 @@ PYI015.pyi:54:11: PYI015 [*] Only simple default values allowed for assignments
205206
54 |-field24 = b"foo" + b"bar" # Y015 Only simple default values are allowed for assignments
206207
54 |+field24 = ... # Y015 Only simple default values are allowed for assignments
207208
55 55 | field25 = 5 * 5 # Y015 Only simple default values are allowed for assignments
209+
56 56 |
210+
57 57 | # We shouldn't emit Y015 within functions
208211

209212
PYI015.pyi:55:11: PYI015 [*] Only simple default values allowed for assignments
210213
|
211214
55 | field23 = "foo" + "bar" # Y015 Only simple default values are allowed for assignments
212215
56 | field24 = b"foo" + b"bar" # Y015 Only simple default values are allowed for assignments
213216
57 | field25 = 5 * 5 # Y015 Only simple default values are allowed for assignments
214217
| ^^^^^ PYI015
218+
58 |
219+
59 | # We shouldn't emit Y015 within functions
215220
|
216221
= help: Replace default value with `...`
217222

@@ -221,5 +226,8 @@ PYI015.pyi:55:11: PYI015 [*] Only simple default values allowed for assignments
221226
54 54 | field24 = b"foo" + b"bar" # Y015 Only simple default values are allowed for assignments
222227
55 |-field25 = 5 * 5 # Y015 Only simple default values are allowed for assignments
223228
55 |+field25 = ... # Y015 Only simple default values are allowed for assignments
229+
56 56 |
230+
57 57 | # We shouldn't emit Y015 within functions
231+
58 58 | def f():
224232

225233

crates/ruff_python_semantic/src/scope.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl From<ScopeId> for usize {
131131
}
132132
}
133133

134-
#[derive(Debug)]
134+
#[derive(Debug, is_macro::Is)]
135135
pub enum ScopeKind<'a> {
136136
Class(ClassDef<'a>),
137137
Function(FunctionDef<'a>),

0 commit comments

Comments
 (0)