Refactor range from Attributed to Nodes (#4422)

This commit is contained in:
Micha Reiser 2023-05-16 08:36:32 +02:00 committed by GitHub
parent 140e0acf54
commit fa26860296
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
330 changed files with 4816 additions and 3946 deletions

View file

@ -1,4 +1,4 @@
use rustpython_parser::ast::{self, Constant, Expr, ExprKind, Operator};
use rustpython_parser::ast::{self, Constant, Expr, Operator};
use ruff_python_ast::call_path::{from_unqualified_name, CallPath};
use ruff_python_stdlib::typing::{
@ -29,7 +29,7 @@ pub fn match_annotated_subscript<'a>(
context: &Context,
typing_modules: impl Iterator<Item = &'a str>,
) -> Option<SubscriptKind> {
if !matches!(expr.node, ExprKind::Name(_) | ExprKind::Attribute(_)) {
if !matches!(expr, Expr::Name(_) | Expr::Attribute(_)) {
return None;
}
@ -113,12 +113,12 @@ pub enum Pep604Operator {
pub fn to_pep604_operator(value: &Expr, slice: &Expr, context: &Context) -> Option<Pep604Operator> {
/// Returns `true` if any argument in the slice is a string.
fn any_arg_is_str(slice: &Expr) -> bool {
match &slice.node {
ExprKind::Constant(ast::ExprConstant {
match slice {
Expr::Constant(ast::ExprConstant {
value: Constant::Str(_),
..
}) => true,
ExprKind::Tuple(ast::ExprTuple { elts, .. }) => elts.iter().any(any_arg_is_str),
Expr::Tuple(ast::ExprTuple { elts, .. }) => elts.iter().any(any_arg_is_str),
_ => false,
}
}
@ -146,8 +146,8 @@ pub fn to_pep604_operator(value: &Expr, slice: &Expr, context: &Context) -> Opti
/// Return `true` if `Expr` represents a reference to a type annotation that resolves to an
/// immutable type.
pub fn is_immutable_annotation(context: &Context, expr: &Expr) -> bool {
match &expr.node {
ExprKind::Name(_) | ExprKind::Attribute(_) => {
match expr {
Expr::Name(_) | Expr::Attribute(_) => {
context.resolve_call_path(expr).map_or(false, |call_path| {
IMMUTABLE_TYPES
.iter()
@ -155,7 +155,7 @@ pub fn is_immutable_annotation(context: &Context, expr: &Expr) -> bool {
.any(|target| call_path.as_slice() == *target)
})
}
ExprKind::Subscript(ast::ExprSubscript { value, slice, .. }) => {
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
context.resolve_call_path(value).map_or(false, |call_path| {
if IMMUTABLE_GENERIC_TYPES
.iter()
@ -163,7 +163,7 @@ pub fn is_immutable_annotation(context: &Context, expr: &Expr) -> bool {
{
true
} else if call_path.as_slice() == ["typing", "Union"] {
if let ExprKind::Tuple(ast::ExprTuple { elts, .. }) = &slice.node {
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = slice.as_ref() {
elts.iter().all(|elt| is_immutable_annotation(context, elt))
} else {
false
@ -171,7 +171,7 @@ pub fn is_immutable_annotation(context: &Context, expr: &Expr) -> bool {
} else if call_path.as_slice() == ["typing", "Optional"] {
is_immutable_annotation(context, slice)
} else if call_path.as_slice() == ["typing", "Annotated"] {
if let ExprKind::Tuple(ast::ExprTuple { elts, .. }) = &slice.node {
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = slice.as_ref() {
elts.first()
.map_or(false, |elt| is_immutable_annotation(context, elt))
} else {
@ -182,12 +182,13 @@ pub fn is_immutable_annotation(context: &Context, expr: &Expr) -> bool {
}
})
}
ExprKind::BinOp(ast::ExprBinOp {
Expr::BinOp(ast::ExprBinOp {
left,
op: Operator::BitOr,
right,
range: _range,
}) => is_immutable_annotation(context, left) && is_immutable_annotation(context, right),
ExprKind::Constant(ast::ExprConstant {
Expr::Constant(ast::ExprConstant {
value: Constant::None,
..
}) => true,