Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore certain flake8-pyi errors within function bodies #4029

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -1796,12 +1796,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 {
Expand Down Expand Up @@ -1842,8 +1836,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);
}
}
}
}
}
Expand Down Expand Up @@ -1879,11 +1885,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),
);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand All @@ -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 `...`

Expand All @@ -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
Expand Up @@ -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>),
Expand Down