Compare commits

...

7 Commits

Author SHA1 Message Date
Charlie Marsh
6a6f4651aa Bump version to 0.0.126 2022-11-17 17:19:19 -05:00
Charlie Marsh
66ae4db6cd Ignore globals when checking local variable names (#800) 2022-11-17 17:19:01 -05:00
Charlie Marsh
801c76037f Except BaseException from N818 checks (#798) 2022-11-17 15:04:42 -05:00
Charlie Marsh
ab825eb28d Fix D202 to remove line after docstring (#797) 2022-11-17 15:01:58 -05:00
Charlie Marsh
826ef7da67 Trigger N818 when parent ends in Error or Exception (#796) 2022-11-17 14:51:40 -05:00
Charlie Marsh
72f5393d3a Add flake8-tidy-imports to cache key 2022-11-17 14:46:45 -05:00
Charlie Marsh
6602f7f489 Trim dedented sections for arg detection (#793) 2022-11-17 12:55:55 -05:00
20 changed files with 210 additions and 49 deletions

View File

@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.125
rev: v0.0.126
hooks:
- id: ruff

6
Cargo.lock generated
View File

@@ -930,7 +930,7 @@ checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "flake8-to-ruff"
version = "0.0.125-dev.0"
version = "0.0.126-dev.0"
dependencies = [
"anyhow",
"clap 4.0.22",
@@ -2238,7 +2238,7 @@ dependencies = [
[[package]]
name = "ruff"
version = "0.0.125"
version = "0.0.126"
dependencies = [
"anyhow",
"assert_cmd",
@@ -2287,7 +2287,7 @@ dependencies = [
[[package]]
name = "ruff_dev"
version = "0.0.125"
version = "0.0.126"
dependencies = [
"anyhow",
"clap 4.0.22",

View File

@@ -6,7 +6,7 @@ members = [
[package]
name = "ruff"
version = "0.0.125"
version = "0.0.126"
edition = "2021"
[lib]

View File

@@ -771,7 +771,7 @@ checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "flake8_to_ruff"
version = "0.0.125"
version = "0.0.126"
dependencies = [
"anyhow",
"clap",
@@ -1975,7 +1975,7 @@ dependencies = [
[[package]]
name = "ruff"
version = "0.0.125"
version = "0.0.126"
dependencies = [
"anyhow",
"bincode",

View File

@@ -1,6 +1,6 @@
[package]
name = "flake8-to-ruff"
version = "0.0.125-dev.0"
version = "0.0.126-dev.0"
edition = "2021"
[lib]

View File

@@ -532,3 +532,37 @@ class Blah: # noqa: D203,D213
expect(os.path.normcase(__file__ if __file__[-1] != 'c' else __file__[:-1]),
'D100: Missing docstring in public module')
@expect('D201: No blank lines allowed before function docstring (found 1)')
@expect('D213: Multi-line docstring summary should start at the second line')
def multiline_leading_space():
"""Leading space.
More content.
"""
@expect('D202: No blank lines allowed after function docstring (found 1)')
@expect('D213: Multi-line docstring summary should start at the second line')
def multiline_trailing_space():
"""Leading space.
More content.
"""
pass
@expect('D201: No blank lines allowed before function docstring (found 1)')
@expect('D202: No blank lines allowed after function docstring (found 1)')
@expect('D213: Multi-line docstring summary should start at the second line')
def multiline_trailing_and_leading_space():
"""Trailing and leading space.
More content.
"""
pass

View File

@@ -1,8 +1,12 @@
import collections
from collections import namedtuple
GLOBAL: str = "foo"
def f():
global GLOBAL
GLOBAL = "bar"
lower = 0
Camel = 0
CONSTANT = 0

View File

@@ -8,3 +8,11 @@ class AnotherError(Exception):
class C(Exception):
pass
class D(BaseException):
pass
class E(AnotherError):
pass

View File

@@ -1,6 +1,6 @@
[package]
name = "ruff_dev"
version = "0.0.125"
version = "0.0.126"
edition = "2021"
[dependencies]

View File

@@ -79,8 +79,10 @@ pub enum BindingKind {
Annotation,
Argument,
Assignment,
// TODO(charlie): This seems to be a catch-all.
Binding,
LoopVar,
Global,
Builtin,
ClassDefinition,
Definition,

View File

@@ -19,8 +19,7 @@ use crate::ast::helpers::{
use crate::ast::operations::extract_all_names;
use crate::ast::relocate::relocate_expr;
use crate::ast::types::{
Binding, BindingContext, BindingKind, ClassScope, FunctionScope, ImportKind, Range, Scope,
ScopeKind,
Binding, BindingContext, BindingKind, ClassScope, ImportKind, Range, Scope, ScopeKind,
};
use crate::ast::visitor::{walk_excepthandler, Visitor};
use crate::ast::{helpers, operations, visitor};
@@ -216,21 +215,18 @@ where
match &stmt.node {
StmtKind::Global { names } | StmtKind::Nonlocal { names } => {
let global_scope_id = self.scopes[GLOBAL_SCOPE_INDEX].id;
let current_scope = self.current_scope();
let current_scope_id = current_scope.id;
if current_scope_id != global_scope_id {
let scope =
&mut self.scopes[*(self.scope_stack.last().expect("No current scope found."))];
if scope.id != global_scope_id {
for name in names {
for scope in self.scopes.iter_mut().skip(GLOBAL_SCOPE_INDEX + 1) {
scope.values.insert(
name,
Binding {
kind: BindingKind::Assignment,
used: Some((global_scope_id, Range::from_located(stmt))),
range: Range::from_located(stmt),
},
);
}
scope.values.insert(
name,
Binding {
kind: BindingKind::Global,
used: Some((global_scope_id, Range::from_located(stmt))),
range: Range::from_located(stmt),
},
);
}
}
@@ -732,10 +728,8 @@ where
));
}
let scope = &mut self.scopes[*(self
.scope_stack
.last_mut()
.expect("No current scope found."))];
let scope = &mut self.scopes
[*(self.scope_stack.last().expect("No current scope found."))];
scope.import_starred = true;
} else {
if let Some(asname) = &alias.node.asname {
@@ -1452,15 +1446,10 @@ where
if let ExprKind::Name { id, ctx } = &func.node {
if id == "locals" && matches!(ctx, ExprContext::Load) {
let scope = &mut self.scopes[*(self
.scope_stack
.last_mut()
.expect("No current scope found."))];
if matches!(
scope.kind,
ScopeKind::Function(FunctionScope { uses_locals: false })
) {
scope.kind = ScopeKind::Function(FunctionScope { uses_locals: true });
let scope = &mut self.scopes
[*(self.scope_stack.last().expect("No current scope found."))];
if let ScopeKind::Function(inner) = &mut scope.kind {
inner.uses_locals = true;
}
}
}
@@ -2243,7 +2232,16 @@ impl<'a> Checker<'a> {
if self.settings.enabled.contains(&CheckCode::N806) {
if matches!(self.current_scope().kind, ScopeKind::Function(..)) {
pep8_naming::plugins::non_lowercase_variable_in_function(self, expr, parent, id)
// Ignore globals.
if !self
.current_scope()
.values
.get(id)
.map(|binding| matches!(binding.kind, BindingKind::Global))
.unwrap_or(false)
{
pep8_naming::plugins::non_lowercase_variable_in_function(self, expr, parent, id)
}
}
}

View File

@@ -224,7 +224,7 @@ pub fn error_suffix_on_exception_name(
) -> Option<Check> {
if bases.iter().any(|base| {
if let ExprKind::Name { id, .. } = &base.node {
id == "Exception"
id == "Exception" || id.ends_with("Error")
} else {
false
}

View File

@@ -223,8 +223,11 @@ pub fn blank_before_after_function(checker: &mut Checker, definition: &Definitio
if checker.patch() {
// Delete the blank line after the docstring.
check.amend(Fix::deletion(
Location::new(docstring.location.row() + 1, 0),
Location::new(docstring.location.row() + 1 + blank_lines_after, 0),
Location::new(docstring.end_location.unwrap().row() + 1, 0),
Location::new(
docstring.end_location.unwrap().row() + 1 + blank_lines_after,
0,
),
));
}
checker.add_check(check);
@@ -1359,7 +1362,10 @@ static GOOGLE_ARGS_REGEX: Lazy<Regex> =
fn args_section(checker: &mut Checker, definition: &Definition, context: &SectionContext) {
let mut args_sections: Vec<String> = vec![];
for line in textwrap::dedent(&context.following_lines.join("\n")).lines() {
for line in textwrap::dedent(&context.following_lines.join("\n"))
.trim()
.lines()
{
if line
.chars()
.next()

View File

@@ -55,7 +55,10 @@ pub fn unused_variables(scope: &Scope, dummy_variable_rgx: &Regex) -> Vec<Check>
if matches!(
scope.kind,
ScopeKind::Function(FunctionScope { uses_locals: true })
ScopeKind::Function(FunctionScope {
uses_locals: true,
..
})
) {
return checks;
}

View File

@@ -122,6 +122,7 @@ impl Hash for Settings {
self.flake8_annotations.hash(state);
self.flake8_bugbear.hash(state);
self.flake8_quotes.hash(state);
self.flake8_tidy_imports.hash(state);
self.isort.hash(state);
self.pep8_naming.hash(state);
}

View File

@@ -38,4 +38,40 @@ expression: checks
row: 146
column: 0
applied: false
- kind:
NoBlankLineBeforeFunction: 1
location:
row: 541
column: 4
end_location:
row: 544
column: 7
fix:
patch:
content: ""
location:
row: 540
column: 0
end_location:
row: 541
column: 0
applied: false
- kind:
NoBlankLineBeforeFunction: 1
location:
row: 563
column: 4
end_location:
row: 566
column: 7
fix:
patch:
content: ""
location:
row: 562
column: 0
end_location:
row: 563
column: 0
applied: false

View File

@@ -38,4 +38,40 @@ expression: checks
row: 148
column: 0
applied: false
- kind:
NoBlankLineAfterFunction: 1
location:
row: 550
column: 4
end_location:
row: 553
column: 7
fix:
patch:
content: ""
location:
row: 554
column: 0
end_location:
row: 555
column: 0
applied: false
- kind:
NoBlankLineAfterFunction: 1
location:
row: 563
column: 4
end_location:
row: 566
column: 7
fix:
patch:
content: ""
location:
row: 567
column: 0
end_location:
row: 568
column: 0
applied: false

View File

@@ -130,4 +130,28 @@ expression: checks
row: 527
column: 7
fix: ~
- kind: MultiLineSummarySecondLine
location:
row: 541
column: 4
end_location:
row: 544
column: 7
fix: ~
- kind: MultiLineSummarySecondLine
location:
row: 550
column: 4
end_location:
row: 553
column: 7
fix: ~
- kind: MultiLineSummarySecondLine
location:
row: 563
column: 4
end_location:
row: 566
column: 7
fix: ~

View File

@@ -5,19 +5,19 @@ expression: checks
- kind:
NonLowercaseVariableInFunction: Camel
location:
row: 7
row: 11
column: 4
end_location:
row: 7
row: 11
column: 9
fix: ~
- kind:
NonLowercaseVariableInFunction: CONSTANT
location:
row: 8
row: 12
column: 4
end_location:
row: 8
row: 12
column: 12
fix: ~

View File

@@ -8,7 +8,16 @@ expression: checks
row: 9
column: 0
end_location:
row: 11
row: 13
column: 0
fix: ~
- kind:
ErrorSuffixOnExceptionName: E
location:
row: 17
column: 0
end_location:
row: 19
column: 0
fix: ~