Skip to content

Commit

Permalink
Apply changes
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 1, 2023
1 parent 485f6b5 commit e628cb0
Show file tree
Hide file tree
Showing 98 changed files with 1,792 additions and 1,280 deletions.
3 changes: 3 additions & 0 deletions crates/ruff/src/autofix/edits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub(crate) fn remove_unused_imports<'a>(
///
/// Supports the removal of parentheses when this is the only (kw)arg left.
/// For this behavior, set `remove_parentheses` to `true`.
///
/// TODO(charlie): Migrate this signature to take [`Arguments`] rather than
/// separate args and keywords.
pub(crate) fn remove_argument(
locator: &Locator,
call_at: TextSize,
Expand Down
17 changes: 12 additions & 5 deletions crates/ruff/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_python_ast::{self as ast, Constant, Expr, ExprContext, Operator, Ranged};
use ruff_python_ast::{self as ast, Arguments, Constant, Expr, ExprContext, Operator, Ranged};
use ruff_python_literal::cformat::{CFormatError, CFormatErrorType};

use ruff_diagnostics::Diagnostic;
Expand Down Expand Up @@ -211,11 +211,14 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
}
}
if checker.enabled(Rule::MixedCaseVariableInClassScope) {
if let ScopeKind::Class(ast::StmtClassDef { bases, .. }) =
if let ScopeKind::Class(ast::StmtClassDef { arguments, .. }) =
&checker.semantic.scope().kind
{
pep8_naming::rules::mixed_case_variable_in_class_scope(
checker, expr, id, bases,
checker,
expr,
id,
arguments.as_ref(),
);
}
}
Expand Down Expand Up @@ -323,8 +326,12 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
Expr::Call(
call @ ast::ExprCall {
func,
args,
keywords,
arguments:
Arguments {
args,
keywords,
range: _,
},
range: _,
},
) => {
Expand Down
23 changes: 15 additions & 8 deletions crates/ruff/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
Stmt::ClassDef(
class_def @ ast::StmtClassDef {
name,
bases,
keywords,
arguments,
type_params: _,
decorator_list,
body,
Expand All @@ -376,20 +375,24 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
}
if checker.enabled(Rule::DjangoExcludeWithModelForm) {
if let Some(diagnostic) =
flake8_django::rules::exclude_with_model_form(checker, bases, body)
flake8_django::rules::exclude_with_model_form(checker, arguments.as_ref(), body)
{
checker.diagnostics.push(diagnostic);
}
}
if checker.enabled(Rule::DjangoAllWithModelForm) {
if let Some(diagnostic) =
flake8_django::rules::all_with_model_form(checker, bases, body)
flake8_django::rules::all_with_model_form(checker, arguments.as_ref(), body)
{
checker.diagnostics.push(diagnostic);
}
}
if checker.enabled(Rule::DjangoUnorderedBodyContentInModel) {
flake8_django::rules::unordered_body_content_in_model(checker, bases, body);
flake8_django::rules::unordered_body_content_in_model(
checker,
arguments.as_ref(),
body,
);
}
if !checker.is_stub {
if checker.enabled(Rule::DjangoModelWithoutDunderStr) {
Expand Down Expand Up @@ -425,7 +428,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
if checker.enabled(Rule::ErrorSuffixOnExceptionName) {
if let Some(diagnostic) = pep8_naming::rules::error_suffix_on_exception_name(
stmt,
bases,
arguments.as_ref(),
name,
&checker.settings.pep8_naming.ignore_names,
) {
Expand All @@ -438,7 +441,11 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
Rule::EmptyMethodWithoutAbstractDecorator,
]) {
flake8_bugbear::rules::abstract_base_class(
checker, stmt, name, bases, keywords, body,
checker,
stmt,
name,
arguments.as_ref(),
body,
);
}
}
Expand Down Expand Up @@ -478,7 +485,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
flake8_builtins::rules::builtin_variable_shadowing(checker, name, name.range());
}
if checker.enabled(Rule::DuplicateBases) {
pylint::rules::duplicate_bases(checker, name, bases);
pylint::rules::duplicate_bases(checker, name, arguments.as_ref());
}
if checker.enabled(Rule::NoSlotsInStrSubclass) {
flake8_slots::rules::no_slots_in_str_subclass(checker, stmt, class_def);
Expand Down
27 changes: 14 additions & 13 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ use std::path::Path;
use itertools::Itertools;
use log::error;
use ruff_python_ast::{
self as ast, Comprehension, Constant, ElifElseClause, ExceptHandler, Expr, ExprContext,
Keyword, Parameter, ParameterWithDefault, Parameters, Pattern, Ranged, Stmt, Suite, UnaryOp,
self as ast, Arguments, Comprehension, Constant, ElifElseClause, ExceptHandler, Expr,
ExprContext, Keyword, Parameter, ParameterWithDefault, Parameters, Pattern, Ranged, Stmt,
Suite, UnaryOp,
};
use ruff_text_size::{TextRange, TextSize};

Expand Down Expand Up @@ -552,8 +553,7 @@ where
Stmt::ClassDef(
class_def @ ast::StmtClassDef {
body,
bases,
keywords,
arguments,
decorator_list,
type_params,
..
Expand All @@ -568,11 +568,9 @@ where
for type_param in type_params {
self.visit_type_param(type_param);
}
for expr in bases {
self.visit_expr(expr);
}
for keyword in keywords {
self.visit_keyword(keyword);

if let Some(arguments) = arguments {
self.visit_arguments(arguments);
}

let definition = docstrings::extraction::extract_definition(
Expand Down Expand Up @@ -837,8 +835,7 @@ where
match expr {
Expr::Call(ast::ExprCall {
func,
args: _,
keywords: _,
arguments: _,
range: _,
}) => {
if let Expr::Name(ast::ExprName { id, ctx, range: _ }) = func.as_ref() {
Expand Down Expand Up @@ -924,8 +921,12 @@ where
}
Expr::Call(ast::ExprCall {
func,
args,
keywords,
arguments:
Arguments {
args,
keywords,
range: _,
},
range: _,
}) => {
self.visit_expr(func);
Expand Down
9 changes: 7 additions & 2 deletions crates/ruff/src/rules/airflow/rules/task_variable_name.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ruff_python_ast as ast;
use ruff_python_ast::{Expr, Ranged};
use ruff_python_ast::{Arguments, Expr, Ranged};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
Expand Down Expand Up @@ -60,7 +60,12 @@ pub(crate) fn variable_name_task_id(
};

// If the value is not a call, we can't do anything.
let Expr::Call(ast::ExprCall { func, keywords, .. }) = value else {
let Expr::Call(ast::ExprCall {
func,
arguments: Arguments { keywords, .. },
..
}) = value
else {
return None;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_python_ast::{self as ast, Expr, Ranged, Stmt};
use ruff_python_ast::{self as ast, Arguments, Expr, Ranged, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
Expand Down Expand Up @@ -94,7 +94,12 @@ pub(crate) fn blind_except(
// If the exception is logged, don't flag an error.
if body.iter().any(|stmt| {
if let Stmt::Expr(ast::StmtExpr { value, range: _ }) = stmt {
if let Expr::Call(ast::ExprCall { func, keywords, .. }) = value.as_ref() {
if let Expr::Call(ast::ExprCall {
func,
arguments: Arguments { keywords, .. },
..
}) = value.as_ref()
{
if logging::is_logger_candidate(
func,
checker.semantic(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_python_ast::{self as ast, Constant, Expr, Keyword, Ranged, Stmt};
use ruff_python_ast::{self as ast, Arguments, Constant, Expr, Keyword, Ranged, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
Expand Down Expand Up @@ -139,14 +139,17 @@ pub(crate) fn abstract_base_class(
checker: &mut Checker,
stmt: &Stmt,
name: &str,
bases: &[Expr],
keywords: &[Keyword],
arguments: Option<&Arguments>,
body: &[Stmt],
) {
if bases.len() + keywords.len() != 1 {
let Some(Arguments { args, keywords, .. }) = arguments else {
return;
};

if args.len() + keywords.len() != 1 {
return;
}
if !is_abc_class(bases, keywords, checker.semantic()) {
if !is_abc_class(args, keywords, checker.semantic()) {
return;
}

Expand Down
15 changes: 9 additions & 6 deletions crates/ruff/src/rules/flake8_bugbear/rules/assert_false.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_python_ast::{self as ast, Expr, ExprContext, Ranged, Stmt};
use ruff_python_ast::{self as ast, Arguments, Expr, ExprContext, Ranged, Stmt};
use ruff_text_size::TextRange;

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
Expand Down Expand Up @@ -53,12 +53,15 @@ fn assertion_error(msg: Option<&Expr>) -> Stmt {
ctx: ExprContext::Load,
range: TextRange::default(),
})),
args: if let Some(msg) = msg {
vec![msg.clone()]
} else {
vec![]
arguments: Arguments {
args: if let Some(msg) = msg {
vec![msg.clone()]
} else {
vec![]
},
keywords: vec![],
range: TextRange::default(),
},
keywords: vec![],
range: TextRange::default(),
}))),
cause: None,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

use ruff_python_ast::{self as ast, Expr, Ranged, WithItem};
use ruff_python_ast::{self as ast, Arguments, Expr, Ranged, WithItem};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
Expand Down Expand Up @@ -81,8 +81,12 @@ pub(crate) fn assert_raises_exception(checker: &mut Checker, items: &[WithItem])
for item in items {
let Expr::Call(ast::ExprCall {
func,
args,
keywords,
arguments:
Arguments {
args,
keywords,
range: _,
},
range: _,
}) = &item.context_expr
else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_python_ast::{self as ast, Comprehension, Expr, ExprContext, Ranged, Stmt};
use ruff_python_ast::{self as ast, Arguments, Comprehension, Expr, ExprContext, Ranged, Stmt};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
Expand Down Expand Up @@ -130,8 +130,12 @@ impl<'a> Visitor<'a> for SuspiciousVariablesVisitor<'a> {
match expr {
Expr::Call(ast::ExprCall {
func,
args,
keywords,
arguments:
Arguments {
args,
keywords,
range: _,
},
range: _,
}) => {
match func.as_ref() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub(crate) fn re_sub_positional_args(checker: &mut Checker, call: &ast::ExprCall
return;
};

if call.args.len() > method.num_args() {
if call.arguments.args.len() > method.num_args() {
checker.diagnostics.push(Diagnostic::new(
ReSubPositionalArgs { method },
call.range(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_python_ast::{self as ast, Expr, Keyword, Ranged};
use ruff_python_ast::{self as ast, Arguments, Expr, Keyword, Ranged};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
Expand Down Expand Up @@ -68,8 +68,7 @@ pub(crate) fn zip_without_explicit_strict(
fn is_infinite_iterator(arg: &Expr, semantic: &SemanticModel) -> bool {
let Expr::Call(ast::ExprCall {
func,
args,
keywords,
arguments: Arguments { args, keywords, .. },
..
}) = &arg
else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ruff_python_ast as ast;
use ruff_python_ast::Decorator;
use ruff_python_ast::{Arguments, Decorator};
use ruff_text_size::TextRange;

use ruff_diagnostics::Diagnostic;
Expand Down Expand Up @@ -77,11 +77,12 @@ pub(crate) fn builtin_attribute_shadowing(
if shadows_builtin(name, &checker.settings.flake8_builtins.builtins_ignorelist) {
// Ignore shadowing within `TypedDict` definitions, since these are only accessible through
// subscripting and not through attribute access.
if class_def
.bases
.iter()
.any(|base| checker.semantic().match_typing_expr(base, "TypedDict"))
{
if class_def.arguments.as_ref().is_some_and(|arguments| {
arguments
.args
.iter()
.any(|base| checker.semantic().match_typing_expr(base, "TypedDict"))
}) {
return;
}

Expand Down Expand Up @@ -133,15 +134,18 @@ fn is_standard_library_override(
class_def: &ast::StmtClassDef,
model: &SemanticModel,
) -> bool {
let Some(Arguments { args: bases, .. }) = class_def.arguments.as_ref() else {
return false;
};
match name {
// Ex) `Event#set`
"set" => class_def.bases.iter().any(|base| {
"set" => bases.iter().any(|base| {
model
.resolve_call_path(base)
.is_some_and(|call_path| matches!(call_path.as_slice(), ["threading", "Event"]))
}),
// Ex) `Filter#filter`
"filter" => class_def.bases.iter().any(|base| {
"filter" => bases.iter().any(|base| {
model
.resolve_call_path(base)
.is_some_and(|call_path| matches!(call_path.as_slice(), ["logging", "Filter"]))
Expand Down
Loading

0 comments on commit e628cb0

Please sign in to comment.