Skip to content

Commit

Permalink
Upgrade RustPython
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jun 19, 2023
1 parent 48f4f2d commit 83e127f
Show file tree
Hide file tree
Showing 103 changed files with 1,149 additions and 1,062 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 = "1866e992ff88c24fe30e39efc401905485dbbaba" }
rustpython-ast = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1866e992ff88c24fe30e39efc401905485dbbaba" , default-features = false, features = ["all-nodes-with-ranges", "num-bigint"]}
rustpython-format = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1866e992ff88c24fe30e39efc401905485dbbaba", default-features = false, features = ["num-bigint"] }
rustpython-literal = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1866e992ff88c24fe30e39efc401905485dbbaba" }
rustpython-parser = { git = "https://github.com/astral-sh/RustPython-Parser.git", rev = "1866e992ff88c24fe30e39efc401905485dbbaba" , 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
68 changes: 26 additions & 42 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::path::Path;

use itertools::Itertools;
use itertools::{chain, Itertools};
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 Down Expand Up @@ -1759,22 +1759,16 @@ 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 chain!(&args.posonlyargs, &args.args, &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 +1780,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 +1796,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 @@ -2100,7 +2079,7 @@ where
expr,
Expr::BoolOp(_)
| Expr::UnaryOp(ast::ExprUnaryOp {
op: Unaryop::Not,
op: UnaryOp::Not,
..
})
) {
Expand Down Expand Up @@ -3301,12 +3280,17 @@ 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 chain!(&args.posonlyargs, &args.args, &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 +3778,9 @@ where
self.semantic.pop_expr();
}

fn visit_excepthandler(&mut self, excepthandler: &'b Excepthandler) {
fn visit_excepthandler(&mut self, excepthandler: &'b ExceptHandler) {
match excepthandler {
Excepthandler::ExceptHandler(ast::ExcepthandlerExceptHandler {
ExceptHandler::ExceptHandler(ast::ExceptHandlerExceptHandler {
type_,
name,
body,
Expand Down Expand Up @@ -3946,17 +3930,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
54 changes: 29 additions & 25 deletions crates/ruff/src/rules/flake8_annotations/rules/definition.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rustpython_parser::ast::{Expr, Ranged, Stmt};
use itertools::chain;
use rustpython_parser::ast::{ArgWithDefault, Expr, Ranged, Stmt};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
Expand Down Expand Up @@ -484,41 +485,39 @@ pub(crate) fn definition(
let is_overridden = visibility::is_override(decorator_list, checker.semantic());

// ANN001, ANN401
for arg in args
.posonlyargs
.iter()
.chain(args.args.iter())
.chain(args.kwonlyargs.iter())
.skip(
// If this is a non-static method, skip `cls` or `self`.
usize::from(
is_method
&& !visibility::is_staticmethod(cast::decorator_list(stmt), checker.semantic()),
),
)
{
for ArgWithDefault {
def,
default: _,
range: _,
} in chain!(&args.posonlyargs, &args.args, &args.kwonlyargs).skip(
// If this is a non-static method, skip `cls` or `self`.
usize::from(
is_method
&& !visibility::is_staticmethod(cast::decorator_list(stmt), checker.semantic()),
),
) {
// ANN401 for dynamically typed arguments
if let Some(annotation) = &arg.annotation {
if let Some(annotation) = &def.annotation {
has_any_typed_arg = true;
if checker.enabled(Rule::AnyType) {
check_dynamically_typed(
annotation,
|| arg.arg.to_string(),
|| def.arg.to_string(),
&mut diagnostics,
is_overridden,
checker.semantic(),
);
}
} else {
if !(checker.settings.flake8_annotations.suppress_dummy_args
&& checker.settings.dummy_variable_rgx.is_match(&arg.arg))
&& checker.settings.dummy_variable_rgx.is_match(&def.arg))
{
if checker.enabled(Rule::MissingTypeFunctionArgument) {
diagnostics.push(Diagnostic::new(
MissingTypeFunctionArgument {
name: arg.arg.to_string(),
name: def.arg.to_string(),
},
arg.range(),
def.range(),
));
}
}
Expand Down Expand Up @@ -591,24 +590,29 @@ pub(crate) fn definition(

// ANN101, ANN102
if is_method && !visibility::is_staticmethod(cast::decorator_list(stmt), checker.semantic()) {
if let Some(arg) = args.posonlyargs.first().or_else(|| args.args.first()) {
if arg.annotation.is_none() {
if let Some(ArgWithDefault {
def,
default: _,
range: _,
}) = args.posonlyargs.first().or_else(|| args.args.first())
{
if def.annotation.is_none() {
if visibility::is_classmethod(cast::decorator_list(stmt), checker.semantic()) {
if checker.enabled(Rule::MissingTypeCls) {
diagnostics.push(Diagnostic::new(
MissingTypeCls {
name: arg.arg.to_string(),
name: def.arg.to_string(),
},
arg.range(),
def.range(),
));
}
} else {
if checker.enabled(Rule::MissingTypeSelf) {
diagnostics.push(Diagnostic::new(
MissingTypeSelf {
name: arg.arg.to_string(),
name: def.arg.to_string(),
},
arg.range(),
def.range(),
));
}
}
Expand Down

0 comments on commit 83e127f

Please sign in to comment.