Upgrade RustPython (#4900)

This commit is contained in:
Micha Reiser 2023-06-08 07:53:14 +02:00 committed by GitHub
parent 4b78141f6b
commit 39a1f3980f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
51 changed files with 416 additions and 320 deletions

View file

@ -1,4 +1,4 @@
use rustpython_parser::ast::Expr;
use rustpython_parser::ast::Decorator;
use ruff_python_ast::call_path::from_qualified_name;
use ruff_python_ast::helpers::map_callable;
@ -22,18 +22,18 @@ pub fn classify(
model: &SemanticModel,
scope: &Scope,
name: &str,
decorator_list: &[Expr],
decorator_list: &[Decorator],
classmethod_decorators: &[String],
staticmethod_decorators: &[String],
) -> FunctionType {
let ScopeKind::Class(scope) = &scope.kind else {
return FunctionType::Function;
};
if decorator_list.iter().any(|expr| {
if decorator_list.iter().any(|decorator| {
// The method is decorated with a static method decorator (like
// `@staticmethod`).
model
.resolve_call_path(map_callable(expr))
.resolve_call_path(map_callable(&decorator.expression))
.map_or(false, |call_path| {
call_path.as_slice() == ["", "staticmethod"]
|| staticmethod_decorators
@ -52,9 +52,9 @@ pub fn classify(
.any(|(module, member)| call_path.as_slice() == [*module, *member])
})
})
|| decorator_list.iter().any(|expr| {
|| decorator_list.iter().any(|decorator| {
// The method is decorated with a class method decorator (like `@classmethod`).
model.resolve_call_path(map_callable(expr)).map_or(false, |call_path| {
model.resolve_call_path(map_callable(&decorator.expression)).map_or(false, |call_path| {
call_path.as_slice() == ["", "classmethod"] ||
classmethod_decorators
.iter()

View file

@ -1,6 +1,6 @@
use std::path::Path;
use rustpython_parser::ast::{self, Expr, Stmt};
use rustpython_parser::ast::{self, Decorator, Stmt};
use ruff_python_ast::call_path::{collect_call_path, CallPath};
use ruff_python_ast::helpers::map_callable;
@ -14,10 +14,10 @@ pub enum Visibility {
}
/// Returns `true` if a function is a "static method".
pub fn is_staticmethod(model: &SemanticModel, decorator_list: &[Expr]) -> bool {
decorator_list.iter().any(|expr| {
pub fn is_staticmethod(model: &SemanticModel, decorator_list: &[Decorator]) -> bool {
decorator_list.iter().any(|decorator| {
model
.resolve_call_path(map_callable(expr))
.resolve_call_path(map_callable(&decorator.expression))
.map_or(false, |call_path| {
call_path.as_slice() == ["", "staticmethod"]
})
@ -25,10 +25,10 @@ pub fn is_staticmethod(model: &SemanticModel, decorator_list: &[Expr]) -> bool {
}
/// Returns `true` if a function is a "class method".
pub fn is_classmethod(model: &SemanticModel, decorator_list: &[Expr]) -> bool {
decorator_list.iter().any(|expr| {
pub fn is_classmethod(model: &SemanticModel, decorator_list: &[Decorator]) -> bool {
decorator_list.iter().any(|decorator| {
model
.resolve_call_path(map_callable(expr))
.resolve_call_path(map_callable(&decorator.expression))
.map_or(false, |call_path| {
call_path.as_slice() == ["", "classmethod"]
})
@ -36,24 +36,24 @@ pub fn is_classmethod(model: &SemanticModel, decorator_list: &[Expr]) -> bool {
}
/// Returns `true` if a function definition is an `@overload`.
pub fn is_overload(model: &SemanticModel, decorator_list: &[Expr]) -> bool {
pub fn is_overload(model: &SemanticModel, decorator_list: &[Decorator]) -> bool {
decorator_list
.iter()
.any(|expr| model.match_typing_expr(map_callable(expr), "overload"))
.any(|decorator| model.match_typing_expr(map_callable(&decorator.expression), "overload"))
}
/// Returns `true` if a function definition is an `@override` (PEP 698).
pub fn is_override(model: &SemanticModel, decorator_list: &[Expr]) -> bool {
pub fn is_override(model: &SemanticModel, decorator_list: &[Decorator]) -> bool {
decorator_list
.iter()
.any(|expr| model.match_typing_expr(map_callable(expr), "override"))
.any(|decorator| model.match_typing_expr(map_callable(&decorator.expression), "override"))
}
/// Returns `true` if a function definition is an abstract method based on its decorators.
pub fn is_abstract(model: &SemanticModel, decorator_list: &[Expr]) -> bool {
decorator_list.iter().any(|expr| {
pub fn is_abstract(model: &SemanticModel, decorator_list: &[Decorator]) -> bool {
decorator_list.iter().any(|decorator| {
model
.resolve_call_path(map_callable(expr))
.resolve_call_path(map_callable(&decorator.expression))
.map_or(false, |call_path| {
matches!(
call_path.as_slice(),
@ -74,12 +74,12 @@ pub fn is_abstract(model: &SemanticModel, decorator_list: &[Expr]) -> bool {
/// `@property`-like decorators.
pub fn is_property(
model: &SemanticModel,
decorator_list: &[Expr],
decorator_list: &[Decorator],
extra_properties: &[CallPath],
) -> bool {
decorator_list.iter().any(|expr| {
decorator_list.iter().any(|decorator| {
model
.resolve_call_path(map_callable(expr))
.resolve_call_path(map_callable(&decorator.expression))
.map_or(false, |call_path| {
call_path.as_slice() == ["", "property"]
|| call_path.as_slice() == ["functools", "cached_property"]
@ -91,10 +91,10 @@ pub fn is_property(
}
/// Returns `true` if a class is an `final`.
pub fn is_final(model: &SemanticModel, decorator_list: &[Expr]) -> bool {
pub fn is_final(model: &SemanticModel, decorator_list: &[Decorator]) -> bool {
decorator_list
.iter()
.any(|expr| model.match_typing_expr(map_callable(expr), "final"))
.any(|decorator| model.match_typing_expr(map_callable(&decorator.expression), "final"))
}
/// Returns `true` if a function is a "magic method".
@ -206,8 +206,8 @@ pub(crate) fn method_visibility(stmt: &Stmt) -> Visibility {
..
}) => {
// Is this a setter or deleter?
if decorator_list.iter().any(|expr| {
collect_call_path(expr).map_or(false, |call_path| {
if decorator_list.iter().any(|decorator| {
collect_call_path(&decorator.expression).map_or(false, |call_path| {
call_path.as_slice() == [name, "setter"]
|| call_path.as_slice() == [name, "deleter"]
})