Skip to content

Commit

Permalink
Ignore certain flake8-pyi errors within function bodies (#4029)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Apr 19, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 827cbe7 commit 10d5415
Showing 7 changed files with 46 additions and 14 deletions.
4 changes: 4 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI001.py
Original file line number Diff line number Diff line change
@@ -11,3 +11,7 @@
_TTuple = TypeVarTuple("_TTuple") # OK

_P = ParamSpec("_P") # OK


def f():
T = TypeVar("T") # OK
3 changes: 3 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI001.pyi
Original file line number Diff line number Diff line change
@@ -11,3 +11,6 @@ _T = TypeVar("_T") # OK
_TTuple = TypeVarTuple("_TTuple") # OK

_P = ParamSpec("_P") # OK

def f():
T = TypeVar("T") # OK
4 changes: 4 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI015.py
Original file line number Diff line number Diff line change
@@ -46,3 +46,7 @@
field23 = "foo" + "bar" # Y015 Only simple default values are allowed for assignments
field24 = b"foo" + b"bar" # Y015 Only simple default values are allowed for assignments
field25 = 5 * 5 # Y015 Only simple default values are allowed for assignments

# We shouldn't emit Y015 within functions
def f():
field26: list[int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
4 changes: 4 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI015.pyi
Original file line number Diff line number Diff line change
@@ -53,3 +53,7 @@ field229: dict[int, int] = {1: 2, **{3: 4}} # Y015 Only simple default values a
field23 = "foo" + "bar" # Y015 Only simple default values are allowed for assignments
field24 = b"foo" + b"bar" # Y015 Only simple default values are allowed for assignments
field25 = 5 * 5 # Y015 Only simple default values are allowed for assignments

# We shouldn't emit Y015 within functions
def f():
field26: list[int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
35 changes: 22 additions & 13 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1791,12 +1791,6 @@ where
}
}

if self.is_stub {
if self.settings.rules.enabled(Rule::UnprefixedTypeParam) {
flake8_pyi::rules::prefix_type_params(self, value, targets);
}
}

if self.settings.rules.enabled(Rule::GlobalStatement) {
for target in targets.iter() {
if let ExprKind::Name { id, .. } = &target.node {
@@ -1837,8 +1831,20 @@ where
}

if self.is_stub {
if self.settings.rules.enabled(Rule::AssignmentDefaultInStub) {
flake8_pyi::rules::assignment_default_in_stub(self, value, None);
if self
.settings
.rules
.any_enabled(&[Rule::UnprefixedTypeParam, Rule::AssignmentDefaultInStub])
{
// Ignore assignments in function bodies; those are covered by other rules.
if !self.ctx.scopes().any(|scope| scope.kind.is_function()) {
if self.settings.rules.enabled(Rule::UnprefixedTypeParam) {
flake8_pyi::rules::prefix_type_params(self, value, targets);
}
if self.settings.rules.enabled(Rule::AssignmentDefaultInStub) {
flake8_pyi::rules::assignment_default_in_stub(self, value, None);
}
}
}
}
}
@@ -1874,11 +1880,14 @@ where
if self.is_stub {
if let Some(value) = value {
if self.settings.rules.enabled(Rule::AssignmentDefaultInStub) {
flake8_pyi::rules::assignment_default_in_stub(
self,
value,
Some(annotation),
);
// Ignore assignments in function bodies; those are covered by other rules.
if !self.ctx.scopes().any(|scope| scope.kind.is_function()) {
flake8_pyi::rules::assignment_default_in_stub(
self,
value,
Some(annotation),
);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -187,6 +187,7 @@ PYI015.pyi:53:11: PYI015 [*] Only simple default values allowed for assignments
53 |+field23 = ... # Y015 Only simple default values are allowed for assignments
54 54 | field24 = b"foo" + b"bar" # Y015 Only simple default values are allowed for assignments
55 55 | field25 = 5 * 5 # Y015 Only simple default values are allowed for assignments
56 56 |

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

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

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


2 changes: 1 addition & 1 deletion crates/ruff_python_semantic/src/scope.rs
Original file line number Diff line number Diff line change
@@ -131,7 +131,7 @@ impl From<ScopeId> for usize {
}
}

#[derive(Debug)]
#[derive(Debug, is_macro::Is)]
pub enum ScopeKind<'a> {
Class(ClassDef<'a>),
Function(FunctionDef<'a>),

0 comments on commit 10d5415

Please sign in to comment.