mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Re-integrate RustPython parser repository (#4359)
Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
865205d992
commit
be6e00ef6e
270 changed files with 3061 additions and 3361 deletions
|
@ -1,7 +1,6 @@
|
|||
use std::cmp::Ordering;
|
||||
|
||||
use rustpython_parser::ast::ExcepthandlerKind::ExceptHandler;
|
||||
use rustpython_parser::ast::{Stmt, StmtKind};
|
||||
use rustpython_parser::ast::{self, ExcepthandlerKind, Stmt, StmtKind};
|
||||
|
||||
use crate::node::{NodeId, Nodes};
|
||||
|
||||
|
@ -43,26 +42,28 @@ fn common_ancestor(
|
|||
/// Return the alternative branches for a given node.
|
||||
fn alternatives(stmt: &Stmt) -> Vec<Vec<&Stmt>> {
|
||||
match &stmt.node {
|
||||
StmtKind::If { body, .. } => vec![body.iter().collect()],
|
||||
StmtKind::Try {
|
||||
StmtKind::If(ast::StmtIf { body, .. }) => vec![body.iter().collect()],
|
||||
StmtKind::Try(ast::StmtTry {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
..
|
||||
}
|
||||
| StmtKind::TryStar {
|
||||
})
|
||||
| StmtKind::TryStar(ast::StmtTryStar {
|
||||
body,
|
||||
handlers,
|
||||
orelse,
|
||||
..
|
||||
} => vec![body.iter().chain(orelse.iter()).collect()]
|
||||
}) => vec![body.iter().chain(orelse.iter()).collect()]
|
||||
.into_iter()
|
||||
.chain(handlers.iter().map(|handler| {
|
||||
let ExceptHandler { body, .. } = &handler.node;
|
||||
let ExcepthandlerKind::ExceptHandler(ast::ExcepthandlerExceptHandler {
|
||||
body, ..
|
||||
}) = &handler.node;
|
||||
body.iter().collect()
|
||||
}))
|
||||
.collect(),
|
||||
StmtKind::Match { cases, .. } => cases
|
||||
StmtKind::Match(ast::StmtMatch { cases, .. }) => cases
|
||||
.iter()
|
||||
.map(|case| case.body.iter().collect())
|
||||
.collect(),
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use rustpython_parser::ast::{Expr, ExprKind};
|
||||
use rustpython_parser::ast::{self, Expr, ExprKind};
|
||||
|
||||
use ruff_python_ast::call_path::collect_call_path;
|
||||
|
||||
|
@ -17,7 +17,7 @@ use crate::context::Context;
|
|||
/// bar.error()
|
||||
/// ```
|
||||
pub fn is_logger_candidate(context: &Context, func: &Expr) -> bool {
|
||||
if let ExprKind::Attribute { value, .. } = &func.node {
|
||||
if let ExprKind::Attribute(ast::ExprAttribute { value, .. }) = &func.node {
|
||||
let Some(call_path) = (if let Some(call_path) = context.resolve_call_path(value) {
|
||||
if call_path.first().map_or(false, |module| *module == "logging") || call_path.as_slice() == ["flask", "current_app", "logger"] {
|
||||
Some(call_path)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use rustpython_parser::ast::{Constant, Expr, ExprKind, Operator};
|
||||
use rustpython_parser::ast::{self, Constant, Expr, ExprKind, Operator};
|
||||
|
||||
use ruff_python_ast::call_path::{from_unqualified_name, CallPath};
|
||||
use ruff_python_stdlib::typing::{
|
||||
|
@ -30,10 +30,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.node, ExprKind::Name(_) | ExprKind::Attribute(_)) {
|
||||
return None;
|
||||
}
|
||||
|
||||
|
@ -75,7 +72,7 @@ pub fn is_pep585_builtin(expr: &Expr, context: &Context) -> bool {
|
|||
|
||||
pub fn is_immutable_annotation(context: &Context, expr: &Expr) -> bool {
|
||||
match &expr.node {
|
||||
ExprKind::Name { .. } | ExprKind::Attribute { .. } => {
|
||||
ExprKind::Name(_) | ExprKind::Attribute(_) => {
|
||||
context.resolve_call_path(expr).map_or(false, |call_path| {
|
||||
IMMUTABLE_TYPES
|
||||
.iter()
|
||||
|
@ -83,7 +80,7 @@ pub fn is_immutable_annotation(context: &Context, expr: &Expr) -> bool {
|
|||
.any(|target| call_path.as_slice() == *target)
|
||||
})
|
||||
}
|
||||
ExprKind::Subscript { value, slice, .. } => {
|
||||
ExprKind::Subscript(ast::ExprSubscript { value, slice, .. }) => {
|
||||
context.resolve_call_path(value).map_or(false, |call_path| {
|
||||
if IMMUTABLE_GENERIC_TYPES
|
||||
.iter()
|
||||
|
@ -91,7 +88,7 @@ pub fn is_immutable_annotation(context: &Context, expr: &Expr) -> bool {
|
|||
{
|
||||
true
|
||||
} else if call_path.as_slice() == ["typing", "Union"] {
|
||||
if let ExprKind::Tuple { elts, .. } = &slice.node {
|
||||
if let ExprKind::Tuple(ast::ExprTuple { elts, .. }) = &slice.node {
|
||||
elts.iter().all(|elt| is_immutable_annotation(context, elt))
|
||||
} else {
|
||||
false
|
||||
|
@ -99,7 +96,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 { elts, .. } = &slice.node {
|
||||
if let ExprKind::Tuple(ast::ExprTuple { elts, .. }) = &slice.node {
|
||||
elts.first()
|
||||
.map_or(false, |elt| is_immutable_annotation(context, elt))
|
||||
} else {
|
||||
|
@ -110,15 +107,15 @@ pub fn is_immutable_annotation(context: &Context, expr: &Expr) -> bool {
|
|||
}
|
||||
})
|
||||
}
|
||||
ExprKind::BinOp {
|
||||
ExprKind::BinOp(ast::ExprBinOp {
|
||||
left,
|
||||
op: Operator::BitOr,
|
||||
right,
|
||||
} => is_immutable_annotation(context, left) && is_immutable_annotation(context, right),
|
||||
ExprKind::Constant {
|
||||
}) => is_immutable_annotation(context, left) && is_immutable_annotation(context, right),
|
||||
ExprKind::Constant(ast::ExprConstant {
|
||||
value: Constant::None,
|
||||
..
|
||||
} => true,
|
||||
}) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::path::Path;
|
||||
|
||||
use rustpython_parser::ast::{Expr, Stmt, StmtKind};
|
||||
use rustpython_parser::ast::{self, Expr, Stmt, StmtKind};
|
||||
|
||||
use ruff_python_ast::call_path::{collect_call_path, CallPath};
|
||||
use ruff_python_ast::helpers::map_callable;
|
||||
|
@ -162,7 +162,8 @@ pub fn module_visibility(module_path: Option<&[String]>, path: &Path) -> Visibil
|
|||
|
||||
pub fn function_visibility(stmt: &Stmt) -> Visibility {
|
||||
match &stmt.node {
|
||||
StmtKind::FunctionDef { name, .. } | StmtKind::AsyncFunctionDef { name, .. } => {
|
||||
StmtKind::FunctionDef(ast::StmtFunctionDef { name, .. })
|
||||
| StmtKind::AsyncFunctionDef(ast::StmtAsyncFunctionDef { name, .. }) => {
|
||||
if name.starts_with('_') {
|
||||
Visibility::Private
|
||||
} else {
|
||||
|
@ -175,16 +176,16 @@ pub fn function_visibility(stmt: &Stmt) -> Visibility {
|
|||
|
||||
pub fn method_visibility(stmt: &Stmt) -> Visibility {
|
||||
match &stmt.node {
|
||||
StmtKind::FunctionDef {
|
||||
StmtKind::FunctionDef(ast::StmtFunctionDef {
|
||||
name,
|
||||
decorator_list,
|
||||
..
|
||||
}
|
||||
| StmtKind::AsyncFunctionDef {
|
||||
})
|
||||
| StmtKind::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
name,
|
||||
decorator_list,
|
||||
..
|
||||
} => {
|
||||
}) => {
|
||||
// Is this a setter or deleter?
|
||||
if decorator_list.iter().any(|expr| {
|
||||
collect_call_path(expr).map_or(false, |call_path| {
|
||||
|
@ -213,7 +214,7 @@ pub fn method_visibility(stmt: &Stmt) -> Visibility {
|
|||
|
||||
pub fn class_visibility(stmt: &Stmt) -> Visibility {
|
||||
match &stmt.node {
|
||||
StmtKind::ClassDef { name, .. } => {
|
||||
StmtKind::ClassDef(ast::StmtClassDef { name, .. }) => {
|
||||
if name.starts_with('_') {
|
||||
Visibility::Private
|
||||
} else {
|
||||
|
|
|
@ -185,7 +185,7 @@ impl From<BindingId> for usize {
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct StarImportation<'a> {
|
||||
/// The level of the import. `None` or `Some(0)` indicate an absolute import.
|
||||
pub level: Option<usize>,
|
||||
pub level: Option<u32>,
|
||||
/// The module being imported. `None` indicates a wildcard import.
|
||||
pub module: Option<&'a str>,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue