Skip to content

Commit

Permalink
Upgrade RustPython (#5192)
Browse files Browse the repository at this point in the history
## Summary

This PR upgrade RustPython to pull in the changes to `Arguments` (zip
defaults with their identifiers) and all the renames to `CmpOp` and
friends.
  • Loading branch information
charliermarsh committed Jun 19, 2023
1 parent ddfdc3b commit 36e01ad
Show file tree
Hide file tree
Showing 103 changed files with 1,293 additions and 1,167 deletions.
13 changes: 7 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ proc-macro2 = { version = "1.0.51" }
quote = { version = "1.0.23" }
regex = { version = "1.7.1" }
rustc-hash = { version = "1.1.0" }
ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "0dc8fdf52d146698c5bcf0b842fddc9e398ad8db" }
rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "0dc8fdf52d146698c5bcf0b842fddc9e398ad8db", default-features = false, features = ["all-nodes-with-ranges"]}
rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "0dc8fdf52d146698c5bcf0b842fddc9e398ad8db" }
rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "0dc8fdf52d146698c5bcf0b842fddc9e398ad8db" }
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "0dc8fdf52d146698c5bcf0b842fddc9e398ad8db", default-features = false, features = ["full-lexer", "all-nodes-with-ranges"] }
ruff_text_size = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "8d74eee75031b68d2204219963fae54a3f31a394" }
rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "8d74eee75031b68d2204219963fae54a3f31a394" , default-features = false, features = ["all-nodes-with-ranges", "num-bigint"]}
rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "8d74eee75031b68d2204219963fae54a3f31a394", default-features = false, features = ["num-bigint"] }
rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "8d74eee75031b68d2204219963fae54a3f31a394" }
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "8d74eee75031b68d2204219963fae54a3f31a394" , default-features = false, features = ["full-lexer", "all-nodes-with-ranges", "num-bigint"] }
schemars = { version = "0.8.12" }
serde = { version = "1.0.152", features = ["derive"] }
serde_json = { version = "1.0.93" }
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/autofix/edits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Interface for generating autofix edits from higher-level actions (e.g., "remove an argument").
use anyhow::{bail, Result};
use ruff_text_size::{TextLen, TextRange, TextSize};
use rustpython_parser::ast::{self, Excepthandler, Expr, Keyword, Ranged, Stmt};
use rustpython_parser::ast::{self, ExceptHandler, Expr, Keyword, Ranged, Stmt};
use rustpython_parser::{lexer, Mode, Tok};

use ruff_diagnostics::Edit;
Expand Down Expand Up @@ -218,7 +218,7 @@ fn is_lone_child(child: &Stmt, parent: &Stmt) -> bool {
|| is_only(orelse, child)
|| is_only(finalbody, child)
|| handlers.iter().any(|handler| match handler {
Excepthandler::ExceptHandler(ast::ExcepthandlerExceptHandler {
ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
body, ..
}) => is_only(body, child),
})
Expand Down
105 changes: 49 additions & 56 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use log::error;
use ruff_text_size::{TextRange, TextSize};
use rustpython_format::cformat::{CFormatError, CFormatErrorType};
use rustpython_parser::ast::{
self, Arg, Arguments, Comprehension, Constant, Excepthandler, Expr, ExprContext, Keyword,
Operator, Pattern, Ranged, Stmt, Suite, Unaryop,
self, Arg, ArgWithDefault, Arguments, Comprehension, Constant, ExceptHandler, Expr,
ExprContext, Keyword, Operator, Pattern, Ranged, Stmt, Suite, UnaryOp,
};

use ruff_diagnostics::{Diagnostic, Fix, IsolationLevel};
Expand All @@ -17,7 +17,7 @@ use ruff_python_ast::source_code::{Generator, Indexer, Locator, Quote, Stylist};
use ruff_python_ast::str::trailing_quote;
use ruff_python_ast::types::Node;
use ruff_python_ast::typing::{parse_type_annotation, AnnotationKind};
use ruff_python_ast::visitor::{walk_excepthandler, walk_pattern, Visitor};
use ruff_python_ast::visitor::{walk_except_handler, walk_pattern, Visitor};
use ruff_python_ast::{cast, helpers, identifier, str, visitor};
use ruff_python_semantic::analyze::{branch_detection, typing, visibility};
use ruff_python_semantic::{
Expand Down Expand Up @@ -1759,22 +1759,21 @@ where
// are enabled.
let runtime_annotation = !self.semantic.future_annotations();

for arg in &args.posonlyargs {
if let Some(expr) = &arg.annotation {
for arg_with_default in args
.posonlyargs
.iter()
.chain(&args.args)
.chain(&args.kwonlyargs)
{
if let Some(expr) = &arg_with_default.def.annotation {
if runtime_annotation {
self.visit_type_definition(expr);
} else {
self.visit_annotation(expr);
};
}
}
for arg in &args.args {
if let Some(expr) = &arg.annotation {
if runtime_annotation {
self.visit_type_definition(expr);
} else {
self.visit_annotation(expr);
};
if let Some(expr) = &arg_with_default.default {
self.visit_expr(expr);
}
}
if let Some(arg) = &args.vararg {
Expand All @@ -1786,15 +1785,6 @@ where
};
}
}
for arg in &args.kwonlyargs {
if let Some(expr) = &arg.annotation {
if runtime_annotation {
self.visit_type_definition(expr);
} else {
self.visit_annotation(expr);
};
}
}
if let Some(arg) = &args.kwarg {
if let Some(expr) = &arg.annotation {
if runtime_annotation {
Expand All @@ -1811,12 +1801,6 @@ where
self.visit_annotation(expr);
};
}
for expr in &args.kw_defaults {
self.visit_expr(expr);
}
for expr in &args.defaults {
self.visit_expr(expr);
}

self.add_binding(
name,
Expand Down Expand Up @@ -1929,8 +1913,8 @@ where
self.semantic.handled_exceptions.pop();

self.semantic.flags |= SemanticModelFlags::EXCEPTION_HANDLER;
for excepthandler in handlers {
self.visit_excepthandler(excepthandler);
for except_handler in handlers {
self.visit_except_handler(except_handler);
}

self.visit_body(orelse);
Expand Down Expand Up @@ -2100,7 +2084,7 @@ where
expr,
Expr::BoolOp(_)
| Expr::UnaryOp(ast::ExprUnaryOp {
op: Unaryop::Not,
op: UnaryOp::Not,
..
})
) {
Expand Down Expand Up @@ -3301,12 +3285,21 @@ where
}

// Visit the default arguments, but avoid the body, which will be deferred.
for expr in &args.kw_defaults {
self.visit_expr(expr);
}
for expr in &args.defaults {
self.visit_expr(expr);
for ArgWithDefault {
default,
def: _,
range: _,
} in args
.posonlyargs
.iter()
.chain(&args.args)
.chain(&args.kwonlyargs)
{
if let Some(expr) = &default {
self.visit_expr(expr);
}
}

self.semantic.push_scope(ScopeKind::Lambda(lambda));
}
Expr::IfExp(ast::ExprIfExp {
Expand Down Expand Up @@ -3794,9 +3787,9 @@ where
self.semantic.pop_expr();
}

fn visit_excepthandler(&mut self, excepthandler: &'b Excepthandler) {
match excepthandler {
Excepthandler::ExceptHandler(ast::ExcepthandlerExceptHandler {
fn visit_except_handler(&mut self, except_handler: &'b ExceptHandler) {
match except_handler {
ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
type_,
name,
body,
Expand All @@ -3807,7 +3800,7 @@ where
if let Some(diagnostic) = pycodestyle::rules::bare_except(
type_.as_deref(),
body,
excepthandler,
except_handler,
self.locator,
) {
self.diagnostics.push(diagnostic);
Expand All @@ -3822,7 +3815,7 @@ where
if self.enabled(Rule::TryExceptPass) {
flake8_bandit::rules::try_except_pass(
self,
excepthandler,
except_handler,
type_.as_deref(),
name,
body,
Expand All @@ -3832,28 +3825,28 @@ where
if self.enabled(Rule::TryExceptContinue) {
flake8_bandit::rules::try_except_continue(
self,
excepthandler,
except_handler,
type_.as_deref(),
name,
body,
self.settings.flake8_bandit.check_typed_exception,
);
}
if self.enabled(Rule::ExceptWithEmptyTuple) {
flake8_bugbear::rules::except_with_empty_tuple(self, excepthandler);
flake8_bugbear::rules::except_with_empty_tuple(self, except_handler);
}
if self.enabled(Rule::ExceptWithNonExceptionClasses) {
flake8_bugbear::rules::except_with_non_exception_classes(self, excepthandler);
flake8_bugbear::rules::except_with_non_exception_classes(self, except_handler);
}
if self.enabled(Rule::ReraiseNoCause) {
tryceratops::rules::reraise_no_cause(self, body);
}
if self.enabled(Rule::BinaryOpException) {
pylint::rules::binary_op_exception(self, excepthandler);
pylint::rules::binary_op_exception(self, except_handler);
}
match name {
Some(name) => {
let range = excepthandler.try_identifier(self.locator).unwrap();
let range = except_handler.try_identifier(self.locator).unwrap();

if self.enabled(Rule::AmbiguousVariableName) {
if let Some(diagnostic) =
Expand All @@ -3866,7 +3859,7 @@ where
flake8_builtins::rules::builtin_variable_shadowing(
self,
name,
AnyShadowing::from(excepthandler),
AnyShadowing::from(except_handler),
);
}

Expand All @@ -3878,7 +3871,7 @@ where
BindingFlags::empty(),
);

walk_excepthandler(self, excepthandler);
walk_except_handler(self, except_handler);

// Remove it from the scope immediately after.
self.add_binding(
Expand All @@ -3898,7 +3891,7 @@ where
if self.patch(Rule::UnusedVariable) {
diagnostic.try_set_fix(|| {
pyflakes::fixes::remove_exception_handler_assignment(
excepthandler,
except_handler,
self.locator,
)
.map(Fix::automatic)
Expand All @@ -3908,7 +3901,7 @@ where
}
}
}
None => walk_excepthandler(self, excepthandler),
None => walk_except_handler(self, except_handler),
}
}
}
Expand Down Expand Up @@ -3946,17 +3939,17 @@ where

// Bind, but intentionally avoid walking default expressions, as we handle them
// upstream.
for arg in &arguments.posonlyargs {
self.visit_arg(arg);
for arg_with_default in &arguments.posonlyargs {
self.visit_arg(&arg_with_default.def);
}
for arg in &arguments.args {
self.visit_arg(arg);
for arg_with_default in &arguments.args {
self.visit_arg(&arg_with_default.def);
}
if let Some(arg) = &arguments.vararg {
self.visit_arg(arg);
}
for arg in &arguments.kwonlyargs {
self.visit_arg(arg);
for arg_with_default in &arguments.kwonlyargs {
self.visit_arg(&arg_with_default.def);
}
if let Some(arg) = &arguments.kwarg {
self.visit_arg(arg);
Expand Down
12 changes: 6 additions & 6 deletions crates/ruff/src/rules/flake8_2020/rules/compare.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use num_bigint::BigInt;
use rustpython_parser::ast::{self, Cmpop, Constant, Expr, Ranged};
use rustpython_parser::ast::{self, CmpOp, Constant, Expr, Ranged};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
Expand Down Expand Up @@ -66,7 +66,7 @@ impl Violation for SysVersionCmpStr10 {
}

/// YTT103, YTT201, YTT203, YTT204, YTT302
pub(crate) fn compare(checker: &mut Checker, left: &Expr, ops: &[Cmpop], comparators: &[Expr]) {
pub(crate) fn compare(checker: &mut Checker, left: &Expr, ops: &[CmpOp], comparators: &[Expr]) {
match left {
Expr::Subscript(ast::ExprSubscript { value, slice, .. })
if is_sys(value, "version_info", checker.semantic()) =>
Expand All @@ -78,7 +78,7 @@ pub(crate) fn compare(checker: &mut Checker, left: &Expr, ops: &[Cmpop], compara
{
if *i == BigInt::from(0) {
if let (
[Cmpop::Eq | Cmpop::NotEq],
[CmpOp::Eq | CmpOp::NotEq],
[Expr::Constant(ast::ExprConstant {
value: Constant::Int(n),
..
Expand All @@ -93,7 +93,7 @@ pub(crate) fn compare(checker: &mut Checker, left: &Expr, ops: &[Cmpop], compara
}
} else if *i == BigInt::from(1) {
if let (
[Cmpop::Lt | Cmpop::LtE | Cmpop::Gt | Cmpop::GtE],
[CmpOp::Lt | CmpOp::LtE | CmpOp::Gt | CmpOp::GtE],
[Expr::Constant(ast::ExprConstant {
value: Constant::Int(_),
..
Expand All @@ -114,7 +114,7 @@ pub(crate) fn compare(checker: &mut Checker, left: &Expr, ops: &[Cmpop], compara
if is_sys(value, "version_info", checker.semantic()) && attr == "minor" =>
{
if let (
[Cmpop::Lt | Cmpop::LtE | Cmpop::Gt | Cmpop::GtE],
[CmpOp::Lt | CmpOp::LtE | CmpOp::Gt | CmpOp::GtE],
[Expr::Constant(ast::ExprConstant {
value: Constant::Int(_),
..
Expand All @@ -134,7 +134,7 @@ pub(crate) fn compare(checker: &mut Checker, left: &Expr, ops: &[Cmpop], compara

if is_sys(left, "version", checker.semantic()) {
if let (
[Cmpop::Lt | Cmpop::LtE | Cmpop::Gt | Cmpop::GtE],
[CmpOp::Lt | CmpOp::LtE | CmpOp::Gt | CmpOp::GtE],
[Expr::Constant(ast::ExprConstant {
value: Constant::Str(s),
..
Expand Down
Loading

0 comments on commit 36e01ad

Please sign in to comment.