mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Remove async AST node variants for with
, for
, and def
(#6369)
## Summary Per the suggestion in https://github.com/astral-sh/ruff/discussions/6183, this PR removes `AsyncWith`, `AsyncFor`, and `AsyncFunctionDef`, replacing them with an `is_async` field on the non-async variants of those structs. Unlike an interpreter, we _generally_ have identical handling for these nodes, so separating them into distinct variants adds complexity from which we don't really benefit. This can be seen below, where we get to remove a _ton_ of code related to adding generic `Any*` wrappers, and a ton of duplicate branches for these cases. ## Test Plan `cargo test` is unchanged, apart from parser snapshots.
This commit is contained in:
parent
c895252aae
commit
daefa74e9a
91 changed files with 375 additions and 1478 deletions
|
@ -179,16 +179,13 @@ fn is_only<T: PartialEq>(vec: &[T], value: &T) -> bool {
|
|||
fn is_lone_child(child: &Stmt, parent: &Stmt) -> bool {
|
||||
match parent {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. })
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. })
|
||||
| Stmt::ClassDef(ast::StmtClassDef { body, .. })
|
||||
| Stmt::With(ast::StmtWith { body, .. })
|
||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
||||
| Stmt::With(ast::StmtWith { body, .. }) => {
|
||||
if is_only(body, child) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Stmt::For(ast::StmtFor { body, orelse, .. })
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. })
|
||||
| Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||
if is_only(body, child) || is_only(orelse, child) {
|
||||
return true;
|
||||
|
|
|
@ -11,21 +11,18 @@ pub(crate) fn deferred_for_loops(checker: &mut Checker) {
|
|||
for snapshot in for_loops {
|
||||
checker.semantic.restore(snapshot);
|
||||
|
||||
if let Stmt::For(ast::StmtFor {
|
||||
let Stmt::For(ast::StmtFor {
|
||||
target, iter, body, ..
|
||||
})
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor {
|
||||
target, iter, body, ..
|
||||
}) = &checker.semantic.current_statement()
|
||||
{
|
||||
if checker.enabled(Rule::UnusedLoopControlVariable) {
|
||||
flake8_bugbear::rules::unused_loop_control_variable(checker, target, body);
|
||||
}
|
||||
if checker.enabled(Rule::IncorrectDictIterator) {
|
||||
perflint::rules::incorrect_dict_iterator(checker, target, iter);
|
||||
}
|
||||
} else {
|
||||
unreachable!("Expected Expr::For | Expr::AsyncFor");
|
||||
}) = checker.semantic.current_statement()
|
||||
else {
|
||||
unreachable!("Expected Stmt::For");
|
||||
};
|
||||
|
||||
if checker.enabled(Rule::UnusedLoopControlVariable) {
|
||||
flake8_bugbear::rules::unused_loop_control_variable(checker, target, body);
|
||||
}
|
||||
if checker.enabled(Rule::IncorrectDictIterator) {
|
||||
perflint::rules::incorrect_dict_iterator(checker, target, iter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -241,10 +241,7 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
|
|||
flake8_pyi::rules::unused_private_typed_dict(checker, scope, &mut diagnostics);
|
||||
}
|
||||
|
||||
if matches!(
|
||||
scope.kind,
|
||||
ScopeKind::Function(_) | ScopeKind::AsyncFunction(_) | ScopeKind::Lambda(_)
|
||||
) {
|
||||
if matches!(scope.kind, ScopeKind::Function(_) | ScopeKind::Lambda(_)) {
|
||||
if checker.enabled(Rule::UnusedVariable) {
|
||||
pyflakes::rules::unused_variable(checker, scope, &mut diagnostics);
|
||||
}
|
||||
|
@ -270,10 +267,7 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
|
|||
}
|
||||
}
|
||||
|
||||
if matches!(
|
||||
scope.kind,
|
||||
ScopeKind::Function(_) | ScopeKind::AsyncFunction(_) | ScopeKind::Module
|
||||
) {
|
||||
if matches!(scope.kind, ScopeKind::Function(_) | ScopeKind::Module) {
|
||||
if enforce_typing_imports {
|
||||
let runtime_imports: Vec<&Binding> = checker
|
||||
.semantic
|
||||
|
|
|
@ -206,7 +206,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
|
|||
}
|
||||
ExprContext::Store => {
|
||||
if checker.enabled(Rule::NonLowercaseVariableInFunction) {
|
||||
if checker.semantic.current_scope().kind.is_any_function() {
|
||||
if checker.semantic.current_scope().kind.is_function() {
|
||||
// Ignore globals.
|
||||
if !checker
|
||||
.semantic
|
||||
|
|
|
@ -70,22 +70,14 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
|||
}
|
||||
}
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
is_async,
|
||||
name,
|
||||
decorator_list,
|
||||
returns,
|
||||
parameters,
|
||||
body,
|
||||
type_params,
|
||||
..
|
||||
})
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
name,
|
||||
decorator_list,
|
||||
returns,
|
||||
parameters,
|
||||
body,
|
||||
type_params,
|
||||
..
|
||||
range: _,
|
||||
}) => {
|
||||
if checker.enabled(Rule::DjangoNonLeadingReceiverDecorator) {
|
||||
flake8_django::rules::non_leading_receiver_decorator(checker, decorator_list);
|
||||
|
@ -151,11 +143,11 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
|||
flake8_pyi::rules::non_self_return_type(
|
||||
checker,
|
||||
stmt,
|
||||
*is_async,
|
||||
name,
|
||||
decorator_list,
|
||||
returns.as_ref().map(AsRef::as_ref),
|
||||
parameters,
|
||||
stmt.is_async_function_def_stmt(),
|
||||
);
|
||||
}
|
||||
if checker.enabled(Rule::CustomTypeVarReturnType) {
|
||||
|
@ -181,12 +173,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
|||
}
|
||||
}
|
||||
if checker.enabled(Rule::BadExitAnnotation) {
|
||||
flake8_pyi::rules::bad_exit_annotation(
|
||||
checker,
|
||||
stmt.is_async_function_def_stmt(),
|
||||
name,
|
||||
parameters,
|
||||
);
|
||||
flake8_pyi::rules::bad_exit_annotation(checker, *is_async, name, parameters);
|
||||
}
|
||||
if checker.enabled(Rule::RedundantNumericUnion) {
|
||||
flake8_pyi::rules::redundant_numeric_union(checker, parameters);
|
||||
|
@ -1097,8 +1084,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
|||
pygrep_hooks::rules::non_existent_mock_method(checker, test);
|
||||
}
|
||||
}
|
||||
Stmt::With(ast::StmtWith { items, body, .. })
|
||||
| Stmt::AsyncWith(ast::StmtAsyncWith { items, body, .. }) => {
|
||||
Stmt::With(with_ @ ast::StmtWith { items, body, .. }) => {
|
||||
if checker.enabled(Rule::AssertRaisesException) {
|
||||
flake8_bugbear::rules::assert_raises_exception(checker, items);
|
||||
}
|
||||
|
@ -1108,8 +1094,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
|||
if checker.enabled(Rule::MultipleWithStatements) {
|
||||
flake8_simplify::rules::multiple_with_statements(
|
||||
checker,
|
||||
stmt,
|
||||
body,
|
||||
with_,
|
||||
checker.semantic.current_statement_parent(),
|
||||
);
|
||||
}
|
||||
|
@ -1134,13 +1119,6 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
|||
iter,
|
||||
orelse,
|
||||
..
|
||||
})
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor {
|
||||
target,
|
||||
body,
|
||||
iter,
|
||||
orelse,
|
||||
..
|
||||
}) => {
|
||||
if checker.any_enabled(&[Rule::UnusedLoopControlVariable, Rule::IncorrectDictIterator])
|
||||
{
|
||||
|
@ -1339,7 +1317,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
|||
if !checker
|
||||
.semantic
|
||||
.current_scopes()
|
||||
.any(|scope| scope.kind.is_any_function())
|
||||
.any(|scope| scope.kind.is_function())
|
||||
{
|
||||
if checker.enabled(Rule::UnprefixedTypeParam) {
|
||||
flake8_pyi::rules::prefix_type_params(checker, value, targets);
|
||||
|
@ -1404,7 +1382,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
|
|||
if !checker
|
||||
.semantic
|
||||
.current_scopes()
|
||||
.any(|scope| scope.kind.is_any_function())
|
||||
.any(|scope| scope.kind.is_function())
|
||||
{
|
||||
flake8_pyi::rules::annotated_assignment_default_in_stub(
|
||||
checker, target, value, annotation,
|
||||
|
|
|
@ -462,14 +462,6 @@ where
|
|||
returns,
|
||||
type_params,
|
||||
..
|
||||
})
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
body,
|
||||
parameters,
|
||||
decorator_list,
|
||||
type_params,
|
||||
returns,
|
||||
..
|
||||
}) => {
|
||||
// Visit the decorators and arguments, but avoid the body, which will be
|
||||
// deferred.
|
||||
|
@ -540,8 +532,7 @@ where
|
|||
|
||||
self.semantic.push_scope(match &stmt {
|
||||
Stmt::FunctionDef(stmt) => ScopeKind::Function(stmt),
|
||||
Stmt::AsyncFunctionDef(stmt) => ScopeKind::AsyncFunction(stmt),
|
||||
_ => unreachable!("Expected Stmt::FunctionDef | Stmt::AsyncFunctionDef"),
|
||||
_ => unreachable!("Expected Stmt::FunctionDef"),
|
||||
});
|
||||
|
||||
self.deferred.functions.push(self.semantic.snapshot());
|
||||
|
@ -743,8 +734,7 @@ where
|
|||
|
||||
// Step 3: Clean-up
|
||||
match stmt {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. })
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { name, .. }) => {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. }) => {
|
||||
let scope_id = self.semantic.scope_id;
|
||||
self.deferred.scopes.push(scope_id);
|
||||
self.semantic.pop_scope(); // Function scope
|
||||
|
@ -1626,7 +1616,7 @@ impl<'a> Checker<'a> {
|
|||
return;
|
||||
}
|
||||
|
||||
if matches!(parent, Stmt::For(_) | Stmt::AsyncFor(_)) {
|
||||
if parent.is_for_stmt() {
|
||||
self.add_binding(
|
||||
id,
|
||||
expr.range(),
|
||||
|
@ -1825,19 +1815,14 @@ impl<'a> Checker<'a> {
|
|||
for snapshot in deferred_functions {
|
||||
self.semantic.restore(snapshot);
|
||||
|
||||
match &self.semantic.current_statement() {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
body, parameters, ..
|
||||
})
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
body, parameters, ..
|
||||
}) => {
|
||||
self.visit_parameters(parameters);
|
||||
self.visit_body(body);
|
||||
}
|
||||
_ => {
|
||||
unreachable!("Expected Stmt::FunctionDef | Stmt::AsyncFunctionDef")
|
||||
}
|
||||
if let Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
body, parameters, ..
|
||||
}) = self.semantic.current_statement()
|
||||
{
|
||||
self.visit_parameters(parameters);
|
||||
self.visit_body(body);
|
||||
} else {
|
||||
unreachable!("Expected Stmt::FunctionDef")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,8 +30,7 @@ pub(crate) fn extract_docstring<'a>(definition: &'a Definition<'a>) -> Option<&'
|
|||
Definition::Module(module) => docstring_from(module.python_ast),
|
||||
Definition::Member(member) => {
|
||||
if let Stmt::ClassDef(ast::StmtClassDef { body, .. })
|
||||
| Stmt::FunctionDef(ast::StmtFunctionDef { body, .. })
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. }) = &member.stmt
|
||||
| Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) = &member.stmt
|
||||
{
|
||||
docstring_from(body)
|
||||
} else {
|
||||
|
|
|
@ -15,14 +15,6 @@ pub(super) fn match_function_def(
|
|||
body,
|
||||
decorator_list,
|
||||
..
|
||||
})
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
name,
|
||||
parameters,
|
||||
returns,
|
||||
body,
|
||||
decorator_list,
|
||||
..
|
||||
}) => (
|
||||
name,
|
||||
parameters,
|
||||
|
|
|
@ -159,18 +159,12 @@ pub(crate) fn abstract_base_class(
|
|||
continue;
|
||||
}
|
||||
|
||||
let (Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
let Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
decorator_list,
|
||||
body,
|
||||
name: method_name,
|
||||
..
|
||||
})
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
decorator_list,
|
||||
body,
|
||||
name: method_name,
|
||||
..
|
||||
})) = stmt
|
||||
}) = stmt
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
|
|
|
@ -86,9 +86,6 @@ impl<'a> Visitor<'a> for SuspiciousVariablesVisitor<'a> {
|
|||
match stmt {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
parameters, body, ..
|
||||
})
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
parameters, body, ..
|
||||
}) => {
|
||||
// Collect all loaded variable names.
|
||||
let mut visitor = LoadedNamesVisitor::default();
|
||||
|
@ -236,7 +233,7 @@ struct AssignedNamesVisitor<'a> {
|
|||
/// `Visitor` to collect all used identifiers in a statement.
|
||||
impl<'a> Visitor<'a> for AssignedNamesVisitor<'a> {
|
||||
fn visit_stmt(&mut self, stmt: &'a Stmt) {
|
||||
if matches!(stmt, Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_)) {
|
||||
if stmt.is_function_def_stmt() {
|
||||
// Don't recurse.
|
||||
return;
|
||||
}
|
||||
|
@ -251,8 +248,7 @@ impl<'a> Visitor<'a> for AssignedNamesVisitor<'a> {
|
|||
}
|
||||
Stmt::AugAssign(ast::StmtAugAssign { target, .. })
|
||||
| Stmt::AnnAssign(ast::StmtAnnAssign { target, .. })
|
||||
| Stmt::For(ast::StmtFor { target, .. })
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor { target, .. }) => {
|
||||
| Stmt::For(ast::StmtFor { target, .. }) => {
|
||||
let mut visitor = NamesFromAssignmentsVisitor::default();
|
||||
visitor.visit_expr(target);
|
||||
self.names.extend(visitor.names);
|
||||
|
|
|
@ -69,16 +69,13 @@ fn walk_stmt(checker: &mut Checker, body: &[Stmt], f: fn(&Stmt) -> bool) {
|
|||
));
|
||||
}
|
||||
match stmt {
|
||||
Stmt::While(ast::StmtWhile { body, .. })
|
||||
| Stmt::For(ast::StmtFor { body, .. })
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor { body, .. }) => {
|
||||
Stmt::While(ast::StmtWhile { body, .. }) | Stmt::For(ast::StmtFor { body, .. }) => {
|
||||
walk_stmt(checker, body, Stmt::is_return_stmt);
|
||||
}
|
||||
Stmt::If(ast::StmtIf { body, .. })
|
||||
| Stmt::Try(ast::StmtTry { body, .. })
|
||||
| Stmt::TryStar(ast::StmtTryStar { body, .. })
|
||||
| Stmt::With(ast::StmtWith { body, .. })
|
||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
||||
| Stmt::With(ast::StmtWith { body, .. }) => {
|
||||
walk_stmt(checker, body, f);
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||
|
|
|
@ -49,14 +49,14 @@ use crate::checkers::ast::Checker;
|
|||
/// ```
|
||||
#[violation]
|
||||
pub struct IterMethodReturnIterable {
|
||||
async_: bool,
|
||||
is_async: bool,
|
||||
}
|
||||
|
||||
impl Violation for IterMethodReturnIterable {
|
||||
#[derive_message_formats]
|
||||
fn message(&self) -> String {
|
||||
let IterMethodReturnIterable { async_ } = self;
|
||||
if *async_ {
|
||||
let IterMethodReturnIterable { is_async } = self;
|
||||
if *is_async {
|
||||
format!("`__aiter__` methods should return an `AsyncIterator`, not an `AsyncIterable`")
|
||||
} else {
|
||||
format!("`__iter__` methods should return an `Iterator`, not an `Iterable`")
|
||||
|
@ -91,7 +91,7 @@ pub(crate) fn iter_method_return_iterable(checker: &mut Checker, definition: &De
|
|||
returns
|
||||
};
|
||||
|
||||
let async_ = match name.as_str() {
|
||||
let is_async = match name.as_str() {
|
||||
"__iter__" => false,
|
||||
"__aiter__" => true,
|
||||
_ => return,
|
||||
|
@ -101,7 +101,7 @@ pub(crate) fn iter_method_return_iterable(checker: &mut Checker, definition: &De
|
|||
.semantic()
|
||||
.resolve_call_path(annotation)
|
||||
.is_some_and(|call_path| {
|
||||
if async_ {
|
||||
if is_async {
|
||||
matches!(
|
||||
call_path.as_slice(),
|
||||
["typing", "AsyncIterable"] | ["collections", "abc", "AsyncIterable"]
|
||||
|
@ -115,7 +115,7 @@ pub(crate) fn iter_method_return_iterable(checker: &mut Checker, definition: &De
|
|||
})
|
||||
{
|
||||
checker.diagnostics.push(Diagnostic::new(
|
||||
IterMethodReturnIterable { async_ },
|
||||
IterMethodReturnIterable { is_async },
|
||||
returns.range(),
|
||||
));
|
||||
}
|
||||
|
|
|
@ -113,11 +113,11 @@ impl Violation for NonSelfReturnType {
|
|||
pub(crate) fn non_self_return_type(
|
||||
checker: &mut Checker,
|
||||
stmt: &Stmt,
|
||||
is_async: bool,
|
||||
name: &str,
|
||||
decorator_list: &[Decorator],
|
||||
returns: Option<&Expr>,
|
||||
parameters: &Parameters,
|
||||
async_: bool,
|
||||
) {
|
||||
let ScopeKind::Class(class_def) = checker.semantic().current_scope().kind else {
|
||||
return;
|
||||
|
@ -138,7 +138,7 @@ pub(crate) fn non_self_return_type(
|
|||
return;
|
||||
}
|
||||
|
||||
if async_ {
|
||||
if is_async {
|
||||
if name == "__aenter__"
|
||||
&& is_name(returns, &class_def.name)
|
||||
&& !is_final(&class_def.decorator_list, checker.semantic())
|
||||
|
|
|
@ -448,7 +448,7 @@ where
|
|||
self.has_return_with_value = true;
|
||||
}
|
||||
}
|
||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) => {}
|
||||
Stmt::FunctionDef(_) => {}
|
||||
_ => visitor::walk_stmt(self, stmt),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -195,12 +195,9 @@ pub(crate) fn complex_raises(
|
|||
if raises_called {
|
||||
let is_too_complex = if let [stmt] = body {
|
||||
match stmt {
|
||||
Stmt::With(ast::StmtWith { body, .. })
|
||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
||||
is_non_trivial_with_body(body)
|
||||
}
|
||||
Stmt::With(ast::StmtWith { body, .. }) => is_non_trivial_with_body(body),
|
||||
// Allow function and class definitions to test decorators
|
||||
Stmt::ClassDef(_) | Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) => false,
|
||||
Stmt::ClassDef(_) | Stmt::FunctionDef(_) => false,
|
||||
stmt => is_compound_statement(stmt),
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -425,9 +425,7 @@ fn implicit_return(checker: &mut Checker, stmt: &Stmt) {
|
|||
}
|
||||
Stmt::Assert(ast::StmtAssert { test, .. }) if is_const_false(test) => {}
|
||||
Stmt::While(ast::StmtWhile { test, .. }) if is_const_true(test) => {}
|
||||
Stmt::For(ast::StmtFor { orelse, .. })
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor { orelse, .. })
|
||||
| Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||
Stmt::For(ast::StmtFor { orelse, .. }) | Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||
if let Some(last_stmt) = orelse.last() {
|
||||
implicit_return(checker, last_stmt);
|
||||
} else {
|
||||
|
@ -454,8 +452,7 @@ fn implicit_return(checker: &mut Checker, stmt: &Stmt) {
|
|||
}
|
||||
}
|
||||
}
|
||||
Stmt::With(ast::StmtWith { body, .. })
|
||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
||||
Stmt::With(ast::StmtWith { body, .. }) => {
|
||||
if let Some(last_stmt) = body.last() {
|
||||
implicit_return(checker, last_stmt);
|
||||
}
|
||||
|
|
|
@ -50,12 +50,6 @@ impl<'a> Visitor<'a> for ReturnVisitor<'a> {
|
|||
decorator_list,
|
||||
returns,
|
||||
..
|
||||
})
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
parameters,
|
||||
decorator_list,
|
||||
returns,
|
||||
..
|
||||
}) => {
|
||||
// Visit the decorators, etc.
|
||||
self.sibling = Some(stmt);
|
||||
|
@ -101,8 +95,7 @@ impl<'a> Visitor<'a> for ReturnVisitor<'a> {
|
|||
// x = f.read()
|
||||
// return x
|
||||
// ```
|
||||
Stmt::With(ast::StmtWith { body, .. })
|
||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
||||
Stmt::With(ast::StmtWith { body, .. }) => {
|
||||
if let Some(stmt_assign) = body.last().and_then(Stmt::as_assign_stmt) {
|
||||
self.stack
|
||||
.assignment_return
|
||||
|
|
|
@ -160,8 +160,7 @@ pub(crate) fn negation_with_equal_op(
|
|||
}
|
||||
|
||||
// Avoid flagging issues in dunder implementations.
|
||||
if let ScopeKind::Function(ast::StmtFunctionDef { name, .. })
|
||||
| ScopeKind::AsyncFunction(ast::StmtAsyncFunctionDef { name, .. }) =
|
||||
if let ScopeKind::Function(ast::StmtFunctionDef { name, .. }) =
|
||||
&checker.semantic().current_scope().kind
|
||||
{
|
||||
if is_dunder_method(name) {
|
||||
|
@ -218,8 +217,7 @@ pub(crate) fn negation_with_not_equal_op(
|
|||
}
|
||||
|
||||
// Avoid flagging issues in dunder implementations.
|
||||
if let ScopeKind::Function(ast::StmtFunctionDef { name, .. })
|
||||
| ScopeKind::AsyncFunction(ast::StmtAsyncFunctionDef { name, .. }) =
|
||||
if let ScopeKind::Function(ast::StmtFunctionDef { name, .. }) =
|
||||
&checker.semantic().current_scope().kind
|
||||
{
|
||||
if is_dunder_method(name) {
|
||||
|
|
|
@ -63,18 +63,22 @@ impl Violation for MultipleWithStatements {
|
|||
/// Returns a boolean indicating whether it's an async with statement, the items
|
||||
/// and body.
|
||||
fn next_with(body: &[Stmt]) -> Option<(bool, &[WithItem], &[Stmt])> {
|
||||
match body {
|
||||
[Stmt::With(ast::StmtWith { items, body, .. })] => Some((false, items, body)),
|
||||
[Stmt::AsyncWith(ast::StmtAsyncWith { items, body, .. })] => Some((true, items, body)),
|
||||
_ => None,
|
||||
}
|
||||
let [Stmt::With(ast::StmtWith {
|
||||
is_async,
|
||||
items,
|
||||
body,
|
||||
..
|
||||
})] = body
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
Some((*is_async, items, body))
|
||||
}
|
||||
|
||||
/// SIM117
|
||||
pub(crate) fn multiple_with_statements(
|
||||
checker: &mut Checker,
|
||||
with_stmt: &Stmt,
|
||||
with_body: &[Stmt],
|
||||
with_stmt: &ast::StmtWith,
|
||||
with_parent: Option<&Stmt>,
|
||||
) {
|
||||
// Make sure we fix from top to bottom for nested with statements, e.g. for
|
||||
|
@ -102,8 +106,8 @@ pub(crate) fn multiple_with_statements(
|
|||
}
|
||||
}
|
||||
|
||||
if let Some((is_async, items, body)) = next_with(with_body) {
|
||||
if is_async != with_stmt.is_async_with_stmt() {
|
||||
if let Some((is_async, items, body)) = next_with(&with_stmt.body) {
|
||||
if is_async != with_stmt.is_async {
|
||||
// One of the statements is an async with, while the other is not,
|
||||
// we can't merge those statements.
|
||||
return;
|
||||
|
@ -133,7 +137,7 @@ pub(crate) fn multiple_with_statements(
|
|||
if !checker
|
||||
.indexer()
|
||||
.comment_ranges()
|
||||
.intersects(TextRange::new(with_stmt.start(), with_body[0].start()))
|
||||
.intersects(TextRange::new(with_stmt.start(), with_stmt.body[0].start()))
|
||||
{
|
||||
match fix_with::fix_multiple_with_statements(
|
||||
checker.locator(),
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use anyhow::{bail, Result};
|
||||
use libcst_native::{CompoundStatement, Statement, Suite, With};
|
||||
use ruff_python_ast::Ranged;
|
||||
use ruff_python_ast::{self as ast, Ranged};
|
||||
|
||||
use crate::autofix::codemods::CodegenStylist;
|
||||
use ruff_diagnostics::Edit;
|
||||
|
@ -14,15 +14,15 @@ use crate::cst::matchers::{match_function_def, match_indented_block, match_state
|
|||
pub(crate) fn fix_multiple_with_statements(
|
||||
locator: &Locator,
|
||||
stylist: &Stylist,
|
||||
stmt: &ruff_python_ast::Stmt,
|
||||
with_stmt: &ast::StmtWith,
|
||||
) -> Result<Edit> {
|
||||
// Infer the indentation of the outer block.
|
||||
let Some(outer_indent) = whitespace::indentation(locator, stmt) else {
|
||||
let Some(outer_indent) = whitespace::indentation(locator, with_stmt) else {
|
||||
bail!("Unable to fix multiline statement");
|
||||
};
|
||||
|
||||
// Extract the module text.
|
||||
let contents = locator.lines(stmt.range());
|
||||
let contents = locator.lines(with_stmt.range());
|
||||
|
||||
// If the block is indented, "embed" it in a function definition, to preserve
|
||||
// indentation while retaining valid source code. (We'll strip the prefix later
|
||||
|
@ -82,7 +82,7 @@ pub(crate) fn fix_multiple_with_statements(
|
|||
.to_string()
|
||||
};
|
||||
|
||||
let range = locator.lines_range(stmt.range());
|
||||
let range = locator.lines_range(with_stmt.range());
|
||||
|
||||
Ok(Edit::range_replacement(contents, range))
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ impl Violation for ReimplementedBuiltin {
|
|||
|
||||
/// SIM110, SIM111
|
||||
pub(crate) fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt) {
|
||||
if !checker.semantic().current_scope().kind.is_any_function() {
|
||||
if !checker.semantic().current_scope().kind.is_function() {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -328,13 +328,6 @@ pub(crate) fn unused_arguments(
|
|||
body,
|
||||
decorator_list,
|
||||
..
|
||||
})
|
||||
| ScopeKind::AsyncFunction(ast::StmtAsyncFunctionDef {
|
||||
name,
|
||||
parameters,
|
||||
body,
|
||||
decorator_list,
|
||||
..
|
||||
}) => {
|
||||
match function_type::classify(
|
||||
name,
|
||||
|
|
|
@ -89,7 +89,7 @@ impl<'a> BlockBuilder<'a> {
|
|||
// sibling (i.e., as if the comment is the next statement, as
|
||||
// opposed to the class or function).
|
||||
match stmt {
|
||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) => {
|
||||
Stmt::FunctionDef(_) => {
|
||||
if helpers::has_comment_break(stmt, self.locator) {
|
||||
Trailer::Sibling
|
||||
} else {
|
||||
|
@ -196,12 +196,6 @@ where
|
|||
}
|
||||
self.finalize(None);
|
||||
}
|
||||
Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. }) => {
|
||||
for stmt in body {
|
||||
self.visit_stmt(stmt);
|
||||
}
|
||||
self.finalize(None);
|
||||
}
|
||||
Stmt::ClassDef(ast::StmtClassDef { body, .. }) => {
|
||||
for stmt in body {
|
||||
self.visit_stmt(stmt);
|
||||
|
@ -219,17 +213,6 @@ where
|
|||
}
|
||||
self.finalize(None);
|
||||
}
|
||||
Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. }) => {
|
||||
for stmt in body {
|
||||
self.visit_stmt(stmt);
|
||||
}
|
||||
self.finalize(None);
|
||||
|
||||
for stmt in orelse {
|
||||
self.visit_stmt(stmt);
|
||||
}
|
||||
self.finalize(None);
|
||||
}
|
||||
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||
for stmt in body {
|
||||
self.visit_stmt(stmt);
|
||||
|
@ -261,12 +244,6 @@ where
|
|||
}
|
||||
self.finalize(None);
|
||||
}
|
||||
Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
||||
for stmt in body {
|
||||
self.visit_stmt(stmt);
|
||||
}
|
||||
self.finalize(None);
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||
for match_case in cases {
|
||||
self.visit_match_case(match_case);
|
||||
|
|
|
@ -82,14 +82,12 @@ fn get_complexity_number(stmts: &[Stmt]) -> usize {
|
|||
complexity += get_complexity_number(&clause.body);
|
||||
}
|
||||
}
|
||||
Stmt::For(ast::StmtFor { body, orelse, .. })
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. }) => {
|
||||
Stmt::For(ast::StmtFor { body, orelse, .. }) => {
|
||||
complexity += 1;
|
||||
complexity += get_complexity_number(body);
|
||||
complexity += get_complexity_number(orelse);
|
||||
}
|
||||
Stmt::With(ast::StmtWith { body, .. })
|
||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
||||
Stmt::With(ast::StmtWith { body, .. }) => {
|
||||
complexity += get_complexity_number(body);
|
||||
}
|
||||
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||
|
@ -131,8 +129,7 @@ fn get_complexity_number(stmts: &[Stmt]) -> usize {
|
|||
complexity += get_complexity_number(body);
|
||||
}
|
||||
}
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. })
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. }) => {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) => {
|
||||
complexity += 1;
|
||||
complexity += get_complexity_number(body);
|
||||
}
|
||||
|
|
|
@ -220,6 +220,7 @@ fn function(
|
|||
})
|
||||
.collect::<Vec<_>>();
|
||||
let func = Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
is_async: false,
|
||||
name: Identifier::new(name.to_string(), TextRange::default()),
|
||||
parameters: Box::new(Parameters {
|
||||
posonlyargs: new_posonlyargs,
|
||||
|
@ -236,6 +237,7 @@ fn function(
|
|||
}
|
||||
}
|
||||
let func = Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
is_async: false,
|
||||
name: Identifier::new(name.to_string(), TextRange::default()),
|
||||
parameters: Box::new(parameters.clone()),
|
||||
body: vec![body],
|
||||
|
|
|
@ -1726,9 +1726,7 @@ fn missing_args(checker: &mut Checker, docstring: &Docstring, docstrings_args: &
|
|||
return;
|
||||
};
|
||||
|
||||
let (Stmt::FunctionDef(ast::StmtFunctionDef { parameters, .. })
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { parameters, .. })) = stmt
|
||||
else {
|
||||
let Stmt::FunctionDef(ast::StmtFunctionDef { parameters, .. }) = stmt else {
|
||||
return;
|
||||
};
|
||||
|
||||
|
|
|
@ -36,14 +36,12 @@ pub(crate) fn break_outside_loop<'a>(
|
|||
let mut child = stmt;
|
||||
for parent in parents {
|
||||
match parent {
|
||||
Stmt::For(ast::StmtFor { orelse, .. })
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor { orelse, .. })
|
||||
| Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||
Stmt::For(ast::StmtFor { orelse, .. }) | Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||
if !orelse.contains(child) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) | Stmt::ClassDef(_) => {
|
||||
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
|
||||
break;
|
||||
}
|
||||
_ => {}
|
||||
|
|
|
@ -36,14 +36,12 @@ pub(crate) fn continue_outside_loop<'a>(
|
|||
let mut child = stmt;
|
||||
for parent in parents {
|
||||
match parent {
|
||||
Stmt::For(ast::StmtFor { orelse, .. })
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor { orelse, .. })
|
||||
| Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||
Stmt::For(ast::StmtFor { orelse, .. }) | Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||
if !orelse.contains(child) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) | Stmt::ClassDef(_) => {
|
||||
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
|
||||
break;
|
||||
}
|
||||
_ => {}
|
||||
|
|
|
@ -51,7 +51,7 @@ pub(crate) fn undefined_local(
|
|||
scope: &Scope,
|
||||
diagnostics: &mut Vec<Diagnostic>,
|
||||
) {
|
||||
if scope.kind.is_any_function() {
|
||||
if scope.kind.is_function() {
|
||||
for (name, binding_id) in scope.bindings() {
|
||||
// If the variable shadows a binding in a parent scope...
|
||||
if let Some(shadowed_id) = checker.semantic().shadowed_binding(binding_id) {
|
||||
|
|
|
@ -299,7 +299,7 @@ fn remove_unused_variable(
|
|||
|
||||
/// F841
|
||||
pub(crate) fn unused_variable(checker: &Checker, scope: &Scope, diagnostics: &mut Vec<Diagnostic>) {
|
||||
if scope.uses_locals() && scope.kind.is_any_function() {
|
||||
if scope.uses_locals() && scope.kind.is_function() {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -24,16 +24,11 @@ pub(super) fn type_param_name(arguments: &Arguments) -> Option<&str> {
|
|||
|
||||
pub(super) fn in_dunder_init(semantic: &SemanticModel, settings: &Settings) -> bool {
|
||||
let scope = semantic.current_scope();
|
||||
let (ScopeKind::Function(ast::StmtFunctionDef {
|
||||
let ScopeKind::Function(ast::StmtFunctionDef {
|
||||
name,
|
||||
decorator_list,
|
||||
..
|
||||
})
|
||||
| ScopeKind::AsyncFunction(ast::StmtAsyncFunctionDef {
|
||||
name,
|
||||
decorator_list,
|
||||
..
|
||||
})) = scope.kind
|
||||
}) = scope.kind
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
|
|
|
@ -69,11 +69,10 @@ fn traverse_body(checker: &mut Checker, body: &[Stmt]) {
|
|||
traverse_body(checker, body);
|
||||
traverse_body(checker, orelse);
|
||||
}
|
||||
Stmt::For(ast::StmtFor { orelse, .. })
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor { orelse, .. })
|
||||
| Stmt::While(ast::StmtWhile { orelse, .. }) => traverse_body(checker, orelse),
|
||||
Stmt::With(ast::StmtWith { body, .. })
|
||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
||||
Stmt::For(ast::StmtFor { orelse, .. }) | Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||
traverse_body(checker, orelse);
|
||||
}
|
||||
Stmt::With(ast::StmtWith { body, .. }) => {
|
||||
traverse_body(checker, body);
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||
|
|
|
@ -147,9 +147,7 @@ impl<'a, 'b> StatementVisitor<'b> for InnerForWithAssignTargetsVisitor<'a, 'b> {
|
|||
fn visit_stmt(&mut self, stmt: &'b Stmt) {
|
||||
// Collect target expressions.
|
||||
match stmt {
|
||||
// For and async for.
|
||||
Stmt::For(ast::StmtFor { target, .. })
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor { target, .. }) => {
|
||||
Stmt::For(ast::StmtFor { target, .. }) => {
|
||||
self.assignment_targets.extend(
|
||||
assignment_targets_from_expr(target, self.dummy_variable_rgx).map(|expr| {
|
||||
ExprWithInnerBindingKind {
|
||||
|
@ -159,7 +157,6 @@ impl<'a, 'b> StatementVisitor<'b> for InnerForWithAssignTargetsVisitor<'a, 'b> {
|
|||
}),
|
||||
);
|
||||
}
|
||||
// With.
|
||||
Stmt::With(ast::StmtWith { items, .. }) => {
|
||||
self.assignment_targets.extend(
|
||||
assignment_targets_from_with_items(items, self.dummy_variable_rgx).map(
|
||||
|
@ -170,7 +167,6 @@ impl<'a, 'b> StatementVisitor<'b> for InnerForWithAssignTargetsVisitor<'a, 'b> {
|
|||
),
|
||||
);
|
||||
}
|
||||
// Assignment, augmented assignment, and annotated assignment.
|
||||
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
|
||||
// Check for single-target assignments which are of the
|
||||
// form `x = cast(..., x)`.
|
||||
|
@ -217,8 +213,7 @@ impl<'a, 'b> StatementVisitor<'b> for InnerForWithAssignTargetsVisitor<'a, 'b> {
|
|||
// Decide whether to recurse.
|
||||
match stmt {
|
||||
// Don't recurse into blocks that create a new scope.
|
||||
Stmt::ClassDef(_) => {}
|
||||
Stmt::FunctionDef(_) => {}
|
||||
Stmt::ClassDef(_) | Stmt::FunctionDef(_) => {}
|
||||
// Otherwise, do recurse.
|
||||
_ => {
|
||||
walk_stmt(self, stmt);
|
||||
|
@ -339,8 +334,7 @@ fn assignment_targets_from_assign_targets<'a>(
|
|||
/// PLW2901
|
||||
pub(crate) fn redefined_loop_name(checker: &mut Checker, stmt: &Stmt) {
|
||||
let (outer_assignment_targets, inner_assignment_targets) = match stmt {
|
||||
Stmt::With(ast::StmtWith { items, body, .. })
|
||||
| Stmt::AsyncWith(ast::StmtAsyncWith { items, body, .. }) => {
|
||||
Stmt::With(ast::StmtWith { items, body, .. }) => {
|
||||
let outer_assignment_targets: Vec<ExprWithOuterBindingKind> =
|
||||
assignment_targets_from_with_items(items, &checker.settings.dummy_variable_rgx)
|
||||
.map(|expr| ExprWithOuterBindingKind {
|
||||
|
@ -358,8 +352,7 @@ pub(crate) fn redefined_loop_name(checker: &mut Checker, stmt: &Stmt) {
|
|||
}
|
||||
(outer_assignment_targets, visitor.assignment_targets)
|
||||
}
|
||||
Stmt::For(ast::StmtFor { target, body, .. })
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor { target, body, .. }) => {
|
||||
Stmt::For(ast::StmtFor { target, body, .. }) => {
|
||||
let outer_assignment_targets: Vec<ExprWithOuterBindingKind> =
|
||||
assignment_targets_from_expr(target, &checker.settings.dummy_variable_rgx)
|
||||
.map(|expr| ExprWithOuterBindingKind {
|
||||
|
@ -377,9 +370,7 @@ pub(crate) fn redefined_loop_name(checker: &mut Checker, stmt: &Stmt) {
|
|||
}
|
||||
(outer_assignment_targets, visitor.assignment_targets)
|
||||
}
|
||||
_ => panic!(
|
||||
"redefined_loop_name called on Statement that is not a With, For, AsyncWith, or AsyncFor"
|
||||
)
|
||||
_ => panic!("redefined_loop_name called on Statement that is not a `With` or `For`"),
|
||||
};
|
||||
|
||||
let mut diagnostics = Vec::new();
|
||||
|
|
|
@ -108,7 +108,6 @@ fn num_branches(stmts: &[Stmt]) -> usize {
|
|||
.sum::<usize>()
|
||||
}
|
||||
Stmt::For(ast::StmtFor { body, orelse, .. })
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. })
|
||||
| Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||
1 + num_branches(body)
|
||||
+ (if orelse.is_empty() {
|
||||
|
|
|
@ -78,8 +78,7 @@ fn num_statements(stmts: &[Stmt]) -> usize {
|
|||
count += num_statements(&clause.body);
|
||||
}
|
||||
}
|
||||
Stmt::For(ast::StmtFor { body, orelse, .. })
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. }) => {
|
||||
Stmt::For(ast::StmtFor { body, orelse, .. }) => {
|
||||
count += num_statements(body);
|
||||
count += num_statements(orelse);
|
||||
}
|
||||
|
@ -129,7 +128,6 @@ fn num_statements(stmts: &[Stmt]) -> usize {
|
|||
}
|
||||
}
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. })
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. })
|
||||
| Stmt::With(ast::StmtWith { body, .. }) => {
|
||||
count += 1;
|
||||
count += num_statements(body);
|
||||
|
|
|
@ -63,8 +63,7 @@ fn loop_exits_early(body: &[Stmt]) -> bool {
|
|||
.iter()
|
||||
.any(|clause| loop_exits_early(&clause.body))
|
||||
}
|
||||
Stmt::With(ast::StmtWith { body, .. })
|
||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => loop_exits_early(body),
|
||||
Stmt::With(ast::StmtWith { body, .. }) => loop_exits_early(body),
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => cases
|
||||
.iter()
|
||||
.any(|MatchCase { body, .. }| loop_exits_early(body)),
|
||||
|
@ -91,9 +90,9 @@ fn loop_exits_early(body: &[Stmt]) -> bool {
|
|||
}) => loop_exits_early(body),
|
||||
})
|
||||
}
|
||||
Stmt::For(ast::StmtFor { orelse, .. })
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor { orelse, .. })
|
||||
| Stmt::While(ast::StmtWhile { orelse, .. }) => loop_exits_early(orelse),
|
||||
Stmt::For(ast::StmtFor { orelse, .. }) | Stmt::While(ast::StmtWhile { orelse, .. }) => {
|
||||
loop_exits_early(orelse)
|
||||
}
|
||||
Stmt::Break(_) => true,
|
||||
_ => false,
|
||||
})
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use ruff_python_ast::{ExprYieldFrom, Ranged};
|
||||
|
||||
use ruff_diagnostics::{Diagnostic, Violation};
|
||||
use ruff_macros::{derive_message_formats, violation};
|
||||
use ruff_python_ast::{self as ast, Ranged};
|
||||
use ruff_python_semantic::ScopeKind;
|
||||
|
||||
use crate::checkers::ast::Checker;
|
||||
|
||||
|
@ -37,9 +37,11 @@ impl Violation for YieldFromInAsyncFunction {
|
|||
}
|
||||
|
||||
/// PLE1700
|
||||
pub(crate) fn yield_from_in_async_function(checker: &mut Checker, expr: &ExprYieldFrom) {
|
||||
let scope = checker.semantic().current_scope();
|
||||
if scope.kind.is_async_function() {
|
||||
pub(crate) fn yield_from_in_async_function(checker: &mut Checker, expr: &ast::ExprYieldFrom) {
|
||||
if matches!(
|
||||
checker.semantic().current_scope().kind,
|
||||
ScopeKind::Function(ast::StmtFunctionDef { is_async: true, .. })
|
||||
) {
|
||||
checker
|
||||
.diagnostics
|
||||
.push(Diagnostic::new(YieldFromInAsyncFunction, expr.range()));
|
||||
|
|
|
@ -83,7 +83,7 @@ pub(crate) fn super_call_with_parameters(
|
|||
let scope = checker.semantic().current_scope();
|
||||
|
||||
// Check: are we in a Function scope?
|
||||
if !scope.kind.is_any_function() {
|
||||
if !scope.kind.is_function() {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use ruff_python_ast::{self as ast, Expr, ExprContext, Ranged, Stmt};
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
|
||||
use ruff_macros::{derive_message_formats, violation};
|
||||
use ruff_python_ast::statement_visitor::StatementVisitor;
|
||||
use ruff_python_ast::visitor::Visitor;
|
||||
use ruff_python_ast::{self as ast, Expr, ExprContext, Ranged, Stmt};
|
||||
use ruff_python_ast::{statement_visitor, visitor};
|
||||
use ruff_python_semantic::StatementKey;
|
||||
|
||||
|
@ -120,7 +120,7 @@ impl<'a> StatementVisitor<'a> for YieldFromVisitor<'a> {
|
|||
}
|
||||
}
|
||||
}
|
||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) | Stmt::ClassDef(_) => {
|
||||
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
|
||||
// Don't recurse into anything that defines a new scope.
|
||||
}
|
||||
_ => statement_visitor::walk_stmt(self, stmt),
|
||||
|
@ -162,39 +162,46 @@ impl<'a> Visitor<'a> for ReferenceVisitor<'a> {
|
|||
/// UP028
|
||||
pub(crate) fn yield_in_for_loop(checker: &mut Checker, stmt: &Stmt) {
|
||||
// Intentionally omit async functions.
|
||||
if let Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) = stmt {
|
||||
let yields = {
|
||||
let mut visitor = YieldFromVisitor::default();
|
||||
visitor.visit_body(body);
|
||||
visitor.yields
|
||||
};
|
||||
let Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
is_async: false,
|
||||
body,
|
||||
..
|
||||
}) = stmt
|
||||
else {
|
||||
return;
|
||||
};
|
||||
|
||||
let references = {
|
||||
let mut visitor = ReferenceVisitor::default();
|
||||
visitor.visit_body(body);
|
||||
visitor.references
|
||||
};
|
||||
let yields = {
|
||||
let mut visitor = YieldFromVisitor::default();
|
||||
visitor.visit_body(body);
|
||||
visitor.yields
|
||||
};
|
||||
|
||||
for item in yields {
|
||||
// If any of the bound names are used outside of the loop, don't rewrite.
|
||||
if references.iter().any(|(statement, names)| {
|
||||
*statement != StatementKey::from(item.stmt)
|
||||
&& *statement != StatementKey::from(item.body)
|
||||
&& item.names.iter().any(|name| names.contains(name))
|
||||
}) {
|
||||
continue;
|
||||
}
|
||||
let references = {
|
||||
let mut visitor = ReferenceVisitor::default();
|
||||
visitor.visit_body(body);
|
||||
visitor.references
|
||||
};
|
||||
|
||||
let mut diagnostic = Diagnostic::new(YieldInForLoop, item.stmt.range());
|
||||
if checker.patch(diagnostic.kind.rule()) {
|
||||
let contents = checker.locator().slice(item.iter.range());
|
||||
let contents = format!("yield from {contents}");
|
||||
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
|
||||
contents,
|
||||
item.stmt.range(),
|
||||
)));
|
||||
}
|
||||
checker.diagnostics.push(diagnostic);
|
||||
for item in yields {
|
||||
// If any of the bound names are used outside of the loop, don't rewrite.
|
||||
if references.iter().any(|(statement, names)| {
|
||||
*statement != StatementKey::from(item.stmt)
|
||||
&& *statement != StatementKey::from(item.body)
|
||||
&& item.names.iter().any(|name| names.contains(name))
|
||||
}) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut diagnostic = Diagnostic::new(YieldInForLoop, item.stmt.range());
|
||||
if checker.patch(diagnostic.kind.rule()) {
|
||||
let contents = checker.locator().slice(item.iter.range());
|
||||
let contents = format!("yield from {contents}");
|
||||
diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
|
||||
contents,
|
||||
item.stmt.range(),
|
||||
)));
|
||||
}
|
||||
checker.diagnostics.push(diagnostic);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,8 +2,8 @@ use std::{fmt, iter, usize};
|
|||
|
||||
use log::error;
|
||||
use ruff_python_ast::{
|
||||
Expr, Identifier, MatchCase, Pattern, PatternMatchAs, Ranged, Stmt, StmtAsyncFor,
|
||||
StmtAsyncWith, StmtFor, StmtMatch, StmtReturn, StmtTry, StmtTryStar, StmtWhile, StmtWith,
|
||||
Expr, Identifier, MatchCase, Pattern, PatternMatchAs, Ranged, Stmt, StmtFor, StmtMatch,
|
||||
StmtReturn, StmtTry, StmtTryStar, StmtWhile, StmtWith,
|
||||
};
|
||||
use ruff_text_size::{TextRange, TextSize};
|
||||
|
||||
|
@ -467,7 +467,6 @@ impl<'stmt> BasicBlocksBuilder<'stmt> {
|
|||
let next = match stmt {
|
||||
// Statements that continue to the next statement after execution.
|
||||
Stmt::FunctionDef(_)
|
||||
| Stmt::AsyncFunctionDef(_)
|
||||
| Stmt::Import(_)
|
||||
| Stmt::ImportFrom(_)
|
||||
| Stmt::ClassDef(_)
|
||||
|
@ -535,12 +534,6 @@ impl<'stmt> BasicBlocksBuilder<'stmt> {
|
|||
body,
|
||||
orelse,
|
||||
..
|
||||
})
|
||||
| Stmt::AsyncFor(StmtAsyncFor {
|
||||
iter: condition,
|
||||
body,
|
||||
orelse,
|
||||
..
|
||||
}) => loop_block(self, Condition::Iterator(condition), body, orelse, after),
|
||||
Stmt::Try(StmtTry {
|
||||
body,
|
||||
|
@ -566,8 +559,7 @@ impl<'stmt> BasicBlocksBuilder<'stmt> {
|
|||
let _ = (body, handlers, orelse, finalbody); // Silence unused code warnings.
|
||||
self.unconditional_next_block(after)
|
||||
}
|
||||
Stmt::With(StmtWith { items, body, .. })
|
||||
| Stmt::AsyncWith(StmtAsyncWith { items, body, .. }) => {
|
||||
Stmt::With(StmtWith { items, body, .. }) => {
|
||||
// TODO: handle `with` statements, see
|
||||
// <https://docs.python.org/3/reference/compound_stmts.html#the-with-statement>.
|
||||
// I recommend to `try` statements first as `with` can desugar
|
||||
|
@ -889,7 +881,6 @@ fn needs_next_block(stmts: &[Stmt]) -> bool {
|
|||
Stmt::Return(_) | Stmt::Raise(_) => false,
|
||||
Stmt::If(stmt) => needs_next_block(&stmt.body) || stmt.elif_else_clauses.last().map_or(true, |clause| needs_next_block(&clause.body)),
|
||||
Stmt::FunctionDef(_)
|
||||
| Stmt::AsyncFunctionDef(_)
|
||||
| Stmt::Import(_)
|
||||
| Stmt::ImportFrom(_)
|
||||
| Stmt::ClassDef(_)
|
||||
|
@ -905,10 +896,8 @@ fn needs_next_block(stmts: &[Stmt]) -> bool {
|
|||
| Stmt::Break(_)
|
||||
| Stmt::Continue(_)
|
||||
| Stmt::For(_)
|
||||
| Stmt::AsyncFor(_)
|
||||
| Stmt::While(_)
|
||||
| Stmt::With(_)
|
||||
| Stmt::AsyncWith(_)
|
||||
| Stmt::Match(_)
|
||||
| Stmt::Try(_)
|
||||
| Stmt::TryStar(_)
|
||||
|
@ -923,7 +912,6 @@ fn needs_next_block(stmts: &[Stmt]) -> bool {
|
|||
fn is_control_flow_stmt(stmt: &Stmt) -> bool {
|
||||
match stmt {
|
||||
Stmt::FunctionDef(_)
|
||||
| Stmt::AsyncFunctionDef(_)
|
||||
| Stmt::Import(_)
|
||||
| Stmt::ImportFrom(_)
|
||||
| Stmt::ClassDef(_)
|
||||
|
@ -937,11 +925,9 @@ fn is_control_flow_stmt(stmt: &Stmt) -> bool {
|
|||
| Stmt::Pass(_) => false,
|
||||
Stmt::Return(_)
|
||||
| Stmt::For(_)
|
||||
| Stmt::AsyncFor(_)
|
||||
| Stmt::While(_)
|
||||
| Stmt::If(_)
|
||||
| Stmt::With(_)
|
||||
| Stmt::AsyncWith(_)
|
||||
| Stmt::Match(_)
|
||||
| Stmt::Raise(_)
|
||||
| Stmt::Try(_)
|
||||
|
|
|
@ -55,7 +55,7 @@ where
|
|||
{
|
||||
fn visit_stmt(&mut self, stmt: &'b Stmt) {
|
||||
match stmt {
|
||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) | Stmt::ClassDef(_) => {
|
||||
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
|
||||
// Don't recurse.
|
||||
}
|
||||
Stmt::Return(_) => self.returns.push(stmt),
|
||||
|
|
|
@ -1,19 +1,15 @@
|
|||
use crate::{nodes, Decorator, Stmt};
|
||||
|
||||
pub fn name(stmt: &Stmt) -> &str {
|
||||
match stmt {
|
||||
Stmt::FunctionDef(nodes::StmtFunctionDef { name, .. })
|
||||
| Stmt::AsyncFunctionDef(nodes::StmtAsyncFunctionDef { name, .. }) => name.as_str(),
|
||||
_ => panic!("Expected Stmt::FunctionDef | Stmt::AsyncFunctionDef"),
|
||||
}
|
||||
let Stmt::FunctionDef(nodes::StmtFunctionDef { name, .. }) = stmt else {
|
||||
panic!("Expected Stmt::FunctionDef")
|
||||
};
|
||||
name.as_str()
|
||||
}
|
||||
|
||||
pub fn decorator_list(stmt: &Stmt) -> &[Decorator] {
|
||||
match stmt {
|
||||
Stmt::FunctionDef(nodes::StmtFunctionDef { decorator_list, .. })
|
||||
| Stmt::AsyncFunctionDef(nodes::StmtAsyncFunctionDef { decorator_list, .. }) => {
|
||||
decorator_list
|
||||
}
|
||||
_ => panic!("Expected Stmt::FunctionDef | Stmt::AsyncFunctionDef"),
|
||||
}
|
||||
let Stmt::FunctionDef(nodes::StmtFunctionDef { decorator_list, .. }) = stmt else {
|
||||
panic!("Expected Stmt::FunctionDef")
|
||||
};
|
||||
decorator_list
|
||||
}
|
||||
|
|
|
@ -950,16 +950,7 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
|
|||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StmtFunctionDef<'a> {
|
||||
decorator_list: Vec<ComparableDecorator<'a>>,
|
||||
name: &'a str,
|
||||
type_params: Option<ComparableTypeParams<'a>>,
|
||||
parameters: ComparableParameters<'a>,
|
||||
returns: Option<ComparableExpr<'a>>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StmtAsyncFunctionDef<'a> {
|
||||
is_async: bool,
|
||||
decorator_list: Vec<ComparableDecorator<'a>>,
|
||||
name: &'a str,
|
||||
type_params: Option<ComparableTypeParams<'a>>,
|
||||
|
@ -1084,14 +1075,7 @@ pub struct StmtAnnAssign<'a> {
|
|||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StmtFor<'a> {
|
||||
target: ComparableExpr<'a>,
|
||||
iter: ComparableExpr<'a>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
orelse: Vec<ComparableStmt<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StmtAsyncFor<'a> {
|
||||
is_async: bool,
|
||||
target: ComparableExpr<'a>,
|
||||
iter: ComparableExpr<'a>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
|
@ -1114,12 +1098,7 @@ pub struct StmtIf<'a> {
|
|||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StmtWith<'a> {
|
||||
items: Vec<ComparableWithItem<'a>>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StmtAsyncWith<'a> {
|
||||
is_async: bool,
|
||||
items: Vec<ComparableWithItem<'a>>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
}
|
||||
|
@ -1194,7 +1173,6 @@ pub struct StmtLineMagic<'a> {
|
|||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ComparableStmt<'a> {
|
||||
FunctionDef(StmtFunctionDef<'a>),
|
||||
AsyncFunctionDef(StmtAsyncFunctionDef<'a>),
|
||||
ClassDef(StmtClassDef<'a>),
|
||||
Return(StmtReturn<'a>),
|
||||
Delete(StmtDelete<'a>),
|
||||
|
@ -1202,11 +1180,9 @@ pub enum ComparableStmt<'a> {
|
|||
AugAssign(StmtAugAssign<'a>),
|
||||
AnnAssign(StmtAnnAssign<'a>),
|
||||
For(StmtFor<'a>),
|
||||
AsyncFor(StmtAsyncFor<'a>),
|
||||
While(StmtWhile<'a>),
|
||||
If(StmtIf<'a>),
|
||||
With(StmtWith<'a>),
|
||||
AsyncWith(StmtAsyncWith<'a>),
|
||||
Match(StmtMatch<'a>),
|
||||
Raise(StmtRaise<'a>),
|
||||
Try(StmtTry<'a>),
|
||||
|
@ -1228,6 +1204,7 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
fn from(stmt: &'a ast::Stmt) -> Self {
|
||||
match stmt {
|
||||
ast::Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
is_async,
|
||||
name,
|
||||
parameters,
|
||||
body,
|
||||
|
@ -1236,22 +1213,7 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
type_params,
|
||||
range: _,
|
||||
}) => Self::FunctionDef(StmtFunctionDef {
|
||||
name: name.as_str(),
|
||||
parameters: parameters.into(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
decorator_list: decorator_list.iter().map(Into::into).collect(),
|
||||
returns: returns.as_ref().map(Into::into),
|
||||
type_params: type_params.as_ref().map(Into::into),
|
||||
}),
|
||||
ast::Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
name,
|
||||
parameters,
|
||||
body,
|
||||
decorator_list,
|
||||
returns,
|
||||
type_params,
|
||||
range: _,
|
||||
}) => Self::AsyncFunctionDef(StmtAsyncFunctionDef {
|
||||
is_async: *is_async,
|
||||
name: name.as_str(),
|
||||
parameters: parameters.into(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
|
@ -1320,24 +1282,14 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
simple: *simple,
|
||||
}),
|
||||
ast::Stmt::For(ast::StmtFor {
|
||||
is_async,
|
||||
target,
|
||||
iter,
|
||||
body,
|
||||
orelse,
|
||||
range: _,
|
||||
}) => Self::For(StmtFor {
|
||||
target: target.into(),
|
||||
iter: iter.into(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
orelse: orelse.iter().map(Into::into).collect(),
|
||||
}),
|
||||
ast::Stmt::AsyncFor(ast::StmtAsyncFor {
|
||||
target,
|
||||
iter,
|
||||
body,
|
||||
orelse,
|
||||
range: _,
|
||||
}) => Self::AsyncFor(StmtAsyncFor {
|
||||
is_async: *is_async,
|
||||
target: target.into(),
|
||||
iter: iter.into(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
|
@ -1364,18 +1316,12 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
elif_else_clauses: elif_else_clauses.iter().map(Into::into).collect(),
|
||||
}),
|
||||
ast::Stmt::With(ast::StmtWith {
|
||||
is_async,
|
||||
items,
|
||||
body,
|
||||
range: _,
|
||||
}) => Self::With(StmtWith {
|
||||
items: items.iter().map(Into::into).collect(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
}),
|
||||
ast::Stmt::AsyncWith(ast::StmtAsyncWith {
|
||||
items,
|
||||
body,
|
||||
range: _,
|
||||
}) => Self::AsyncWith(StmtAsyncWith {
|
||||
is_async: *is_async,
|
||||
items: items.iter().map(Into::into).collect(),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
}),
|
||||
|
|
|
@ -1,134 +0,0 @@
|
|||
use crate::node::AnyNodeRef;
|
||||
use crate::{
|
||||
Decorator, Expr, Identifier, Parameters, Ranged, StmtAsyncFunctionDef, StmtFunctionDef, Suite,
|
||||
TypeParams,
|
||||
};
|
||||
use ruff_text_size::TextRange;
|
||||
|
||||
/// Enum that represents any python function definition.
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
pub enum AnyFunctionDefinition<'a> {
|
||||
FunctionDefinition(&'a StmtFunctionDef),
|
||||
AsyncFunctionDefinition(&'a StmtAsyncFunctionDef),
|
||||
}
|
||||
|
||||
impl<'a> AnyFunctionDefinition<'a> {
|
||||
pub const fn cast_ref(reference: AnyNodeRef<'a>) -> Option<Self> {
|
||||
match reference {
|
||||
AnyNodeRef::StmtAsyncFunctionDef(definition) => {
|
||||
Some(Self::AsyncFunctionDefinition(definition))
|
||||
}
|
||||
AnyNodeRef::StmtFunctionDef(definition) => Some(Self::FunctionDefinition(definition)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `Some` if this is a [`StmtFunctionDef`] and `None` otherwise.
|
||||
pub const fn as_function_definition(self) -> Option<&'a StmtFunctionDef> {
|
||||
if let Self::FunctionDefinition(definition) = self {
|
||||
Some(definition)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `Some` if this is a [`StmtAsyncFunctionDef`] and `None` otherwise.
|
||||
pub const fn as_async_function_definition(self) -> Option<&'a StmtAsyncFunctionDef> {
|
||||
if let Self::AsyncFunctionDefinition(definition) = self {
|
||||
Some(definition)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the function's name
|
||||
pub const fn name(self) -> &'a Identifier {
|
||||
match self {
|
||||
Self::FunctionDefinition(definition) => &definition.name,
|
||||
Self::AsyncFunctionDefinition(definition) => &definition.name,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the function arguments (parameters).
|
||||
pub fn arguments(self) -> &'a Parameters {
|
||||
match self {
|
||||
Self::FunctionDefinition(definition) => definition.parameters.as_ref(),
|
||||
Self::AsyncFunctionDefinition(definition) => definition.parameters.as_ref(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the function's body
|
||||
pub const fn body(self) -> &'a Suite {
|
||||
match self {
|
||||
Self::FunctionDefinition(definition) => &definition.body,
|
||||
Self::AsyncFunctionDefinition(definition) => &definition.body,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the decorators attributing the function.
|
||||
pub fn decorators(self) -> &'a [Decorator] {
|
||||
match self {
|
||||
Self::FunctionDefinition(definition) => &definition.decorator_list,
|
||||
Self::AsyncFunctionDefinition(definition) => &definition.decorator_list,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn returns(self) -> Option<&'a Expr> {
|
||||
match self {
|
||||
Self::FunctionDefinition(definition) => definition.returns.as_deref(),
|
||||
Self::AsyncFunctionDefinition(definition) => definition.returns.as_deref(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn type_params(self) -> Option<&'a TypeParams> {
|
||||
match self {
|
||||
Self::FunctionDefinition(definition) => definition.type_params.as_ref(),
|
||||
Self::AsyncFunctionDefinition(definition) => definition.type_params.as_ref(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if this is [`Self::AsyncFunctionDefinition`]
|
||||
pub const fn is_async(self) -> bool {
|
||||
matches!(self, Self::AsyncFunctionDefinition(_))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ranged for AnyFunctionDefinition<'_> {
|
||||
fn range(&self) -> TextRange {
|
||||
match self {
|
||||
AnyFunctionDefinition::FunctionDefinition(definition) => definition.range(),
|
||||
AnyFunctionDefinition::AsyncFunctionDefinition(definition) => definition.range(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a StmtFunctionDef> for AnyFunctionDefinition<'a> {
|
||||
fn from(value: &'a StmtFunctionDef) -> Self {
|
||||
Self::FunctionDefinition(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a StmtAsyncFunctionDef> for AnyFunctionDefinition<'a> {
|
||||
fn from(value: &'a StmtAsyncFunctionDef) -> Self {
|
||||
Self::AsyncFunctionDefinition(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<AnyFunctionDefinition<'a>> for AnyNodeRef<'a> {
|
||||
fn from(value: AnyFunctionDefinition<'a>) -> Self {
|
||||
match value {
|
||||
AnyFunctionDefinition::FunctionDefinition(function_def) => {
|
||||
AnyNodeRef::StmtFunctionDef(function_def)
|
||||
}
|
||||
AnyFunctionDefinition::AsyncFunctionDefinition(async_def) => {
|
||||
AnyNodeRef::StmtAsyncFunctionDef(async_def)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a AnyFunctionDefinition<'a>> for AnyNodeRef<'a> {
|
||||
fn from(value: &'a AnyFunctionDefinition<'a>) -> Self {
|
||||
(*value).into()
|
||||
}
|
||||
}
|
|
@ -18,14 +18,11 @@ pub const fn is_compound_statement(stmt: &Stmt) -> bool {
|
|||
matches!(
|
||||
stmt,
|
||||
Stmt::FunctionDef(_)
|
||||
| Stmt::AsyncFunctionDef(_)
|
||||
| Stmt::ClassDef(_)
|
||||
| Stmt::While(_)
|
||||
| Stmt::For(_)
|
||||
| Stmt::AsyncFor(_)
|
||||
| Stmt::Match(_)
|
||||
| Stmt::With(_)
|
||||
| Stmt::AsyncWith(_)
|
||||
| Stmt::If(_)
|
||||
| Stmt::Try(_)
|
||||
| Stmt::TryStar(_)
|
||||
|
@ -321,14 +318,6 @@ where
|
|||
decorator_list,
|
||||
returns,
|
||||
..
|
||||
})
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
parameters,
|
||||
type_params,
|
||||
body,
|
||||
decorator_list,
|
||||
returns,
|
||||
..
|
||||
}) => {
|
||||
parameters
|
||||
.posonlyargs
|
||||
|
@ -439,13 +428,6 @@ where
|
|||
body,
|
||||
orelse,
|
||||
..
|
||||
})
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor {
|
||||
target,
|
||||
iter,
|
||||
body,
|
||||
orelse,
|
||||
..
|
||||
}) => {
|
||||
any_over_expr(target, func)
|
||||
|| any_over_expr(iter, func)
|
||||
|
@ -474,8 +456,7 @@ where
|
|||
|| any_over_body(&clause.body, func)
|
||||
})
|
||||
}
|
||||
Stmt::With(ast::StmtWith { items, body, .. })
|
||||
| Stmt::AsyncWith(ast::StmtAsyncWith { items, body, .. }) => {
|
||||
Stmt::With(ast::StmtWith { items, body, .. }) => {
|
||||
items.iter().any(|with_item| {
|
||||
any_over_expr(&with_item.context_expr, func)
|
||||
|| with_item
|
||||
|
@ -912,7 +893,7 @@ where
|
|||
{
|
||||
fn visit_stmt(&mut self, stmt: &'b Stmt) {
|
||||
match stmt {
|
||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) | Stmt::ClassDef(_) => {
|
||||
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
|
||||
// Don't recurse.
|
||||
}
|
||||
Stmt::Return(stmt) => self.returns.push(stmt),
|
||||
|
@ -941,11 +922,7 @@ where
|
|||
self.raises
|
||||
.push((stmt.range(), exc.as_deref(), cause.as_deref()));
|
||||
}
|
||||
Stmt::ClassDef(_)
|
||||
| Stmt::FunctionDef(_)
|
||||
| Stmt::AsyncFunctionDef(_)
|
||||
| Stmt::Try(_)
|
||||
| Stmt::TryStar(_) => {}
|
||||
Stmt::ClassDef(_) | Stmt::FunctionDef(_) | Stmt::Try(_) | Stmt::TryStar(_) => {}
|
||||
Stmt::If(ast::StmtIf {
|
||||
body,
|
||||
elif_else_clauses,
|
||||
|
@ -958,9 +935,7 @@ where
|
|||
}
|
||||
Stmt::While(ast::StmtWhile { body, .. })
|
||||
| Stmt::With(ast::StmtWith { body, .. })
|
||||
| Stmt::AsyncWith(ast::StmtAsyncWith { body, .. })
|
||||
| Stmt::For(ast::StmtFor { body, .. })
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor { body, .. }) => {
|
||||
| Stmt::For(ast::StmtFor { body, .. }) => {
|
||||
walk_body(self, body);
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||
|
|
|
@ -31,8 +31,7 @@ impl Identifier for Stmt {
|
|||
fn identifier(&self) -> TextRange {
|
||||
match self {
|
||||
Stmt::ClassDef(ast::StmtClassDef { name, .. })
|
||||
| Stmt::FunctionDef(ast::StmtFunctionDef { name, .. })
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { name, .. }) => name.range(),
|
||||
| Stmt::FunctionDef(ast::StmtFunctionDef { name, .. }) => name.range(),
|
||||
_ => self.range(),
|
||||
}
|
||||
}
|
||||
|
@ -85,10 +84,9 @@ pub fn except(handler: &ExceptHandler, source: &str) -> TextRange {
|
|||
.expect("Failed to find `except` token in `ExceptHandler`")
|
||||
}
|
||||
|
||||
/// Return the [`TextRange`] of the `else` token in a `For`, `AsyncFor`, or `While` statement.
|
||||
/// Return the [`TextRange`] of the `else` token in a `For` or `While` statement.
|
||||
pub fn else_(stmt: &Stmt, source: &str) -> Option<TextRange> {
|
||||
let (Stmt::For(ast::StmtFor { body, orelse, .. })
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. })
|
||||
| Stmt::While(ast::StmtWhile { body, orelse, .. })) = stmt
|
||||
else {
|
||||
return None;
|
||||
|
|
|
@ -6,7 +6,6 @@ pub mod call_path;
|
|||
pub mod cast;
|
||||
pub mod comparable;
|
||||
pub mod docstrings;
|
||||
pub mod function;
|
||||
pub mod hashable;
|
||||
pub mod helpers;
|
||||
pub mod identifier;
|
||||
|
|
|
@ -24,7 +24,6 @@ pub enum AnyNode {
|
|||
ModModule(ast::ModModule),
|
||||
ModExpression(ast::ModExpression),
|
||||
StmtFunctionDef(ast::StmtFunctionDef),
|
||||
StmtAsyncFunctionDef(ast::StmtAsyncFunctionDef),
|
||||
StmtClassDef(ast::StmtClassDef),
|
||||
StmtReturn(ast::StmtReturn),
|
||||
StmtDelete(ast::StmtDelete),
|
||||
|
@ -33,11 +32,9 @@ pub enum AnyNode {
|
|||
StmtAugAssign(ast::StmtAugAssign),
|
||||
StmtAnnAssign(ast::StmtAnnAssign),
|
||||
StmtFor(ast::StmtFor),
|
||||
StmtAsyncFor(ast::StmtAsyncFor),
|
||||
StmtWhile(ast::StmtWhile),
|
||||
StmtIf(ast::StmtIf),
|
||||
StmtWith(ast::StmtWith),
|
||||
StmtAsyncWith(ast::StmtAsyncWith),
|
||||
StmtMatch(ast::StmtMatch),
|
||||
StmtRaise(ast::StmtRaise),
|
||||
StmtTry(ast::StmtTry),
|
||||
|
@ -110,7 +107,6 @@ impl AnyNode {
|
|||
pub fn statement(self) -> Option<Stmt> {
|
||||
match self {
|
||||
AnyNode::StmtFunctionDef(node) => Some(Stmt::FunctionDef(node)),
|
||||
AnyNode::StmtAsyncFunctionDef(node) => Some(Stmt::AsyncFunctionDef(node)),
|
||||
AnyNode::StmtClassDef(node) => Some(Stmt::ClassDef(node)),
|
||||
AnyNode::StmtReturn(node) => Some(Stmt::Return(node)),
|
||||
AnyNode::StmtDelete(node) => Some(Stmt::Delete(node)),
|
||||
|
@ -119,11 +115,9 @@ impl AnyNode {
|
|||
AnyNode::StmtAugAssign(node) => Some(Stmt::AugAssign(node)),
|
||||
AnyNode::StmtAnnAssign(node) => Some(Stmt::AnnAssign(node)),
|
||||
AnyNode::StmtFor(node) => Some(Stmt::For(node)),
|
||||
AnyNode::StmtAsyncFor(node) => Some(Stmt::AsyncFor(node)),
|
||||
AnyNode::StmtWhile(node) => Some(Stmt::While(node)),
|
||||
AnyNode::StmtIf(node) => Some(Stmt::If(node)),
|
||||
AnyNode::StmtWith(node) => Some(Stmt::With(node)),
|
||||
AnyNode::StmtAsyncWith(node) => Some(Stmt::AsyncWith(node)),
|
||||
AnyNode::StmtMatch(node) => Some(Stmt::Match(node)),
|
||||
AnyNode::StmtRaise(node) => Some(Stmt::Raise(node)),
|
||||
AnyNode::StmtTry(node) => Some(Stmt::Try(node)),
|
||||
|
@ -230,7 +224,6 @@ impl AnyNode {
|
|||
AnyNode::ModModule(_)
|
||||
| AnyNode::ModExpression(_)
|
||||
| AnyNode::StmtFunctionDef(_)
|
||||
| AnyNode::StmtAsyncFunctionDef(_)
|
||||
| AnyNode::StmtClassDef(_)
|
||||
| AnyNode::StmtReturn(_)
|
||||
| AnyNode::StmtDelete(_)
|
||||
|
@ -239,11 +232,9 @@ impl AnyNode {
|
|||
| AnyNode::StmtAugAssign(_)
|
||||
| AnyNode::StmtAnnAssign(_)
|
||||
| AnyNode::StmtFor(_)
|
||||
| AnyNode::StmtAsyncFor(_)
|
||||
| AnyNode::StmtWhile(_)
|
||||
| AnyNode::StmtIf(_)
|
||||
| AnyNode::StmtWith(_)
|
||||
| AnyNode::StmtAsyncWith(_)
|
||||
| AnyNode::StmtMatch(_)
|
||||
| AnyNode::StmtRaise(_)
|
||||
| AnyNode::StmtTry(_)
|
||||
|
@ -291,7 +282,6 @@ impl AnyNode {
|
|||
AnyNode::ModExpression(node) => Some(Mod::Expression(node)),
|
||||
|
||||
AnyNode::StmtFunctionDef(_)
|
||||
| AnyNode::StmtAsyncFunctionDef(_)
|
||||
| AnyNode::StmtClassDef(_)
|
||||
| AnyNode::StmtReturn(_)
|
||||
| AnyNode::StmtDelete(_)
|
||||
|
@ -300,11 +290,9 @@ impl AnyNode {
|
|||
| AnyNode::StmtAugAssign(_)
|
||||
| AnyNode::StmtAnnAssign(_)
|
||||
| AnyNode::StmtFor(_)
|
||||
| AnyNode::StmtAsyncFor(_)
|
||||
| AnyNode::StmtWhile(_)
|
||||
| AnyNode::StmtIf(_)
|
||||
| AnyNode::StmtWith(_)
|
||||
| AnyNode::StmtAsyncWith(_)
|
||||
| AnyNode::StmtMatch(_)
|
||||
| AnyNode::StmtRaise(_)
|
||||
| AnyNode::StmtTry(_)
|
||||
|
@ -388,7 +376,6 @@ impl AnyNode {
|
|||
AnyNode::ModModule(_)
|
||||
| AnyNode::ModExpression(_)
|
||||
| AnyNode::StmtFunctionDef(_)
|
||||
| AnyNode::StmtAsyncFunctionDef(_)
|
||||
| AnyNode::StmtClassDef(_)
|
||||
| AnyNode::StmtReturn(_)
|
||||
| AnyNode::StmtDelete(_)
|
||||
|
@ -397,11 +384,9 @@ impl AnyNode {
|
|||
| AnyNode::StmtAugAssign(_)
|
||||
| AnyNode::StmtAnnAssign(_)
|
||||
| AnyNode::StmtFor(_)
|
||||
| AnyNode::StmtAsyncFor(_)
|
||||
| AnyNode::StmtWhile(_)
|
||||
| AnyNode::StmtIf(_)
|
||||
| AnyNode::StmtWith(_)
|
||||
| AnyNode::StmtAsyncWith(_)
|
||||
| AnyNode::StmtMatch(_)
|
||||
| AnyNode::StmtRaise(_)
|
||||
| AnyNode::StmtTry(_)
|
||||
|
@ -470,7 +455,6 @@ impl AnyNode {
|
|||
AnyNode::ModModule(_)
|
||||
| AnyNode::ModExpression(_)
|
||||
| AnyNode::StmtFunctionDef(_)
|
||||
| AnyNode::StmtAsyncFunctionDef(_)
|
||||
| AnyNode::StmtClassDef(_)
|
||||
| AnyNode::StmtReturn(_)
|
||||
| AnyNode::StmtDelete(_)
|
||||
|
@ -479,11 +463,9 @@ impl AnyNode {
|
|||
| AnyNode::StmtAugAssign(_)
|
||||
| AnyNode::StmtAnnAssign(_)
|
||||
| AnyNode::StmtFor(_)
|
||||
| AnyNode::StmtAsyncFor(_)
|
||||
| AnyNode::StmtWhile(_)
|
||||
| AnyNode::StmtIf(_)
|
||||
| AnyNode::StmtWith(_)
|
||||
| AnyNode::StmtAsyncWith(_)
|
||||
| AnyNode::StmtMatch(_)
|
||||
| AnyNode::StmtRaise(_)
|
||||
| AnyNode::StmtTry(_)
|
||||
|
@ -577,7 +559,6 @@ impl AnyNode {
|
|||
Self::ModModule(node) => AnyNodeRef::ModModule(node),
|
||||
Self::ModExpression(node) => AnyNodeRef::ModExpression(node),
|
||||
Self::StmtFunctionDef(node) => AnyNodeRef::StmtFunctionDef(node),
|
||||
Self::StmtAsyncFunctionDef(node) => AnyNodeRef::StmtAsyncFunctionDef(node),
|
||||
Self::StmtClassDef(node) => AnyNodeRef::StmtClassDef(node),
|
||||
Self::StmtReturn(node) => AnyNodeRef::StmtReturn(node),
|
||||
Self::StmtDelete(node) => AnyNodeRef::StmtDelete(node),
|
||||
|
@ -586,11 +567,9 @@ impl AnyNode {
|
|||
Self::StmtAugAssign(node) => AnyNodeRef::StmtAugAssign(node),
|
||||
Self::StmtAnnAssign(node) => AnyNodeRef::StmtAnnAssign(node),
|
||||
Self::StmtFor(node) => AnyNodeRef::StmtFor(node),
|
||||
Self::StmtAsyncFor(node) => AnyNodeRef::StmtAsyncFor(node),
|
||||
Self::StmtWhile(node) => AnyNodeRef::StmtWhile(node),
|
||||
Self::StmtIf(node) => AnyNodeRef::StmtIf(node),
|
||||
Self::StmtWith(node) => AnyNodeRef::StmtWith(node),
|
||||
Self::StmtAsyncWith(node) => AnyNodeRef::StmtAsyncWith(node),
|
||||
Self::StmtMatch(node) => AnyNodeRef::StmtMatch(node),
|
||||
Self::StmtRaise(node) => AnyNodeRef::StmtRaise(node),
|
||||
Self::StmtTry(node) => AnyNodeRef::StmtTry(node),
|
||||
|
@ -750,34 +729,6 @@ impl AstNode for ast::StmtFunctionDef {
|
|||
AnyNode::from(self)
|
||||
}
|
||||
}
|
||||
impl AstNode for ast::StmtAsyncFunctionDef {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if let AnyNode::StmtAsyncFunctionDef(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
|
||||
if let AnyNodeRef::StmtAsyncFunctionDef(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn as_any_node_ref(&self) -> AnyNodeRef {
|
||||
AnyNodeRef::from(self)
|
||||
}
|
||||
|
||||
fn into_any_node(self) -> AnyNode {
|
||||
AnyNode::from(self)
|
||||
}
|
||||
}
|
||||
impl AstNode for ast::StmtClassDef {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
|
@ -1002,34 +953,6 @@ impl AstNode for ast::StmtFor {
|
|||
AnyNode::from(self)
|
||||
}
|
||||
}
|
||||
impl AstNode for ast::StmtAsyncFor {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if let AnyNode::StmtAsyncFor(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
|
||||
if let AnyNodeRef::StmtAsyncFor(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn as_any_node_ref(&self) -> AnyNodeRef {
|
||||
AnyNodeRef::from(self)
|
||||
}
|
||||
|
||||
fn into_any_node(self) -> AnyNode {
|
||||
AnyNode::from(self)
|
||||
}
|
||||
}
|
||||
impl AstNode for ast::StmtWhile {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
|
@ -1142,34 +1065,6 @@ impl AstNode for ast::StmtWith {
|
|||
AnyNode::from(self)
|
||||
}
|
||||
}
|
||||
impl AstNode for ast::StmtAsyncWith {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if let AnyNode::StmtAsyncWith(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
|
||||
if let AnyNodeRef::StmtAsyncWith(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn as_any_node_ref(&self) -> AnyNodeRef {
|
||||
AnyNodeRef::from(self)
|
||||
}
|
||||
|
||||
fn into_any_node(self) -> AnyNode {
|
||||
AnyNode::from(self)
|
||||
}
|
||||
}
|
||||
impl AstNode for ast::StmtMatch {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
|
@ -2996,7 +2891,6 @@ impl From<Stmt> for AnyNode {
|
|||
fn from(stmt: Stmt) -> Self {
|
||||
match stmt {
|
||||
Stmt::FunctionDef(node) => AnyNode::StmtFunctionDef(node),
|
||||
Stmt::AsyncFunctionDef(node) => AnyNode::StmtAsyncFunctionDef(node),
|
||||
Stmt::ClassDef(node) => AnyNode::StmtClassDef(node),
|
||||
Stmt::Return(node) => AnyNode::StmtReturn(node),
|
||||
Stmt::Delete(node) => AnyNode::StmtDelete(node),
|
||||
|
@ -3005,11 +2899,9 @@ impl From<Stmt> for AnyNode {
|
|||
Stmt::AugAssign(node) => AnyNode::StmtAugAssign(node),
|
||||
Stmt::AnnAssign(node) => AnyNode::StmtAnnAssign(node),
|
||||
Stmt::For(node) => AnyNode::StmtFor(node),
|
||||
Stmt::AsyncFor(node) => AnyNode::StmtAsyncFor(node),
|
||||
Stmt::While(node) => AnyNode::StmtWhile(node),
|
||||
Stmt::If(node) => AnyNode::StmtIf(node),
|
||||
Stmt::With(node) => AnyNode::StmtWith(node),
|
||||
Stmt::AsyncWith(node) => AnyNode::StmtAsyncWith(node),
|
||||
Stmt::Match(node) => AnyNode::StmtMatch(node),
|
||||
Stmt::Raise(node) => AnyNode::StmtRaise(node),
|
||||
Stmt::Try(node) => AnyNode::StmtTry(node),
|
||||
|
@ -3113,12 +3005,6 @@ impl From<ast::StmtFunctionDef> for AnyNode {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<ast::StmtAsyncFunctionDef> for AnyNode {
|
||||
fn from(node: ast::StmtAsyncFunctionDef) -> Self {
|
||||
AnyNode::StmtAsyncFunctionDef(node)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ast::StmtClassDef> for AnyNode {
|
||||
fn from(node: ast::StmtClassDef) -> Self {
|
||||
AnyNode::StmtClassDef(node)
|
||||
|
@ -3167,12 +3053,6 @@ impl From<ast::StmtFor> for AnyNode {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<ast::StmtAsyncFor> for AnyNode {
|
||||
fn from(node: ast::StmtAsyncFor) -> Self {
|
||||
AnyNode::StmtAsyncFor(node)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ast::StmtWhile> for AnyNode {
|
||||
fn from(node: ast::StmtWhile) -> Self {
|
||||
AnyNode::StmtWhile(node)
|
||||
|
@ -3197,12 +3077,6 @@ impl From<ast::StmtWith> for AnyNode {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<ast::StmtAsyncWith> for AnyNode {
|
||||
fn from(node: ast::StmtAsyncWith) -> Self {
|
||||
AnyNode::StmtAsyncWith(node)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ast::StmtMatch> for AnyNode {
|
||||
fn from(node: ast::StmtMatch) -> Self {
|
||||
AnyNode::StmtMatch(node)
|
||||
|
@ -3588,7 +3462,6 @@ impl Ranged for AnyNode {
|
|||
AnyNode::ModModule(node) => node.range(),
|
||||
AnyNode::ModExpression(node) => node.range(),
|
||||
AnyNode::StmtFunctionDef(node) => node.range(),
|
||||
AnyNode::StmtAsyncFunctionDef(node) => node.range(),
|
||||
AnyNode::StmtClassDef(node) => node.range(),
|
||||
AnyNode::StmtReturn(node) => node.range(),
|
||||
AnyNode::StmtDelete(node) => node.range(),
|
||||
|
@ -3597,11 +3470,9 @@ impl Ranged for AnyNode {
|
|||
AnyNode::StmtAugAssign(node) => node.range(),
|
||||
AnyNode::StmtAnnAssign(node) => node.range(),
|
||||
AnyNode::StmtFor(node) => node.range(),
|
||||
AnyNode::StmtAsyncFor(node) => node.range(),
|
||||
AnyNode::StmtWhile(node) => node.range(),
|
||||
AnyNode::StmtIf(node) => node.range(),
|
||||
AnyNode::StmtWith(node) => node.range(),
|
||||
AnyNode::StmtAsyncWith(node) => node.range(),
|
||||
AnyNode::StmtMatch(node) => node.range(),
|
||||
AnyNode::StmtRaise(node) => node.range(),
|
||||
AnyNode::StmtTry(node) => node.range(),
|
||||
|
@ -3677,7 +3548,6 @@ pub enum AnyNodeRef<'a> {
|
|||
ModModule(&'a ast::ModModule),
|
||||
ModExpression(&'a ast::ModExpression),
|
||||
StmtFunctionDef(&'a ast::StmtFunctionDef),
|
||||
StmtAsyncFunctionDef(&'a ast::StmtAsyncFunctionDef),
|
||||
StmtClassDef(&'a ast::StmtClassDef),
|
||||
StmtReturn(&'a ast::StmtReturn),
|
||||
StmtDelete(&'a ast::StmtDelete),
|
||||
|
@ -3686,11 +3556,9 @@ pub enum AnyNodeRef<'a> {
|
|||
StmtAugAssign(&'a ast::StmtAugAssign),
|
||||
StmtAnnAssign(&'a ast::StmtAnnAssign),
|
||||
StmtFor(&'a ast::StmtFor),
|
||||
StmtAsyncFor(&'a ast::StmtAsyncFor),
|
||||
StmtWhile(&'a ast::StmtWhile),
|
||||
StmtIf(&'a ast::StmtIf),
|
||||
StmtWith(&'a ast::StmtWith),
|
||||
StmtAsyncWith(&'a ast::StmtAsyncWith),
|
||||
StmtMatch(&'a ast::StmtMatch),
|
||||
StmtRaise(&'a ast::StmtRaise),
|
||||
StmtTry(&'a ast::StmtTry),
|
||||
|
@ -3765,7 +3633,6 @@ impl AnyNodeRef<'_> {
|
|||
AnyNodeRef::ModModule(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::ModExpression(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::StmtFunctionDef(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::StmtAsyncFunctionDef(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::StmtClassDef(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::StmtReturn(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::StmtDelete(node) => NonNull::from(*node).cast(),
|
||||
|
@ -3774,11 +3641,9 @@ impl AnyNodeRef<'_> {
|
|||
AnyNodeRef::StmtAugAssign(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::StmtAnnAssign(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::StmtFor(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::StmtAsyncFor(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::StmtWhile(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::StmtIf(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::StmtWith(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::StmtAsyncWith(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::StmtMatch(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::StmtRaise(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::StmtTry(node) => NonNull::from(*node).cast(),
|
||||
|
@ -3859,7 +3724,6 @@ impl AnyNodeRef<'_> {
|
|||
AnyNodeRef::ModModule(_) => NodeKind::ModModule,
|
||||
AnyNodeRef::ModExpression(_) => NodeKind::ModExpression,
|
||||
AnyNodeRef::StmtFunctionDef(_) => NodeKind::StmtFunctionDef,
|
||||
AnyNodeRef::StmtAsyncFunctionDef(_) => NodeKind::StmtAsyncFunctionDef,
|
||||
AnyNodeRef::StmtClassDef(_) => NodeKind::StmtClassDef,
|
||||
AnyNodeRef::StmtReturn(_) => NodeKind::StmtReturn,
|
||||
AnyNodeRef::StmtDelete(_) => NodeKind::StmtDelete,
|
||||
|
@ -3868,11 +3732,9 @@ impl AnyNodeRef<'_> {
|
|||
AnyNodeRef::StmtAugAssign(_) => NodeKind::StmtAugAssign,
|
||||
AnyNodeRef::StmtAnnAssign(_) => NodeKind::StmtAnnAssign,
|
||||
AnyNodeRef::StmtFor(_) => NodeKind::StmtFor,
|
||||
AnyNodeRef::StmtAsyncFor(_) => NodeKind::StmtAsyncFor,
|
||||
AnyNodeRef::StmtWhile(_) => NodeKind::StmtWhile,
|
||||
AnyNodeRef::StmtIf(_) => NodeKind::StmtIf,
|
||||
AnyNodeRef::StmtWith(_) => NodeKind::StmtWith,
|
||||
AnyNodeRef::StmtAsyncWith(_) => NodeKind::StmtAsyncWith,
|
||||
AnyNodeRef::StmtMatch(_) => NodeKind::StmtMatch,
|
||||
AnyNodeRef::StmtRaise(_) => NodeKind::StmtRaise,
|
||||
AnyNodeRef::StmtTry(_) => NodeKind::StmtTry,
|
||||
|
@ -3945,7 +3807,6 @@ impl AnyNodeRef<'_> {
|
|||
pub const fn is_statement(self) -> bool {
|
||||
match self {
|
||||
AnyNodeRef::StmtFunctionDef(_)
|
||||
| AnyNodeRef::StmtAsyncFunctionDef(_)
|
||||
| AnyNodeRef::StmtClassDef(_)
|
||||
| AnyNodeRef::StmtReturn(_)
|
||||
| AnyNodeRef::StmtDelete(_)
|
||||
|
@ -3954,11 +3815,9 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::StmtAugAssign(_)
|
||||
| AnyNodeRef::StmtAnnAssign(_)
|
||||
| AnyNodeRef::StmtFor(_)
|
||||
| AnyNodeRef::StmtAsyncFor(_)
|
||||
| AnyNodeRef::StmtWhile(_)
|
||||
| AnyNodeRef::StmtIf(_)
|
||||
| AnyNodeRef::StmtWith(_)
|
||||
| AnyNodeRef::StmtAsyncWith(_)
|
||||
| AnyNodeRef::StmtMatch(_)
|
||||
| AnyNodeRef::StmtRaise(_)
|
||||
| AnyNodeRef::StmtTry(_)
|
||||
|
@ -4065,7 +3924,6 @@ impl AnyNodeRef<'_> {
|
|||
AnyNodeRef::ModModule(_)
|
||||
| AnyNodeRef::ModExpression(_)
|
||||
| AnyNodeRef::StmtFunctionDef(_)
|
||||
| AnyNodeRef::StmtAsyncFunctionDef(_)
|
||||
| AnyNodeRef::StmtClassDef(_)
|
||||
| AnyNodeRef::StmtReturn(_)
|
||||
| AnyNodeRef::StmtDelete(_)
|
||||
|
@ -4074,11 +3932,9 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::StmtAugAssign(_)
|
||||
| AnyNodeRef::StmtAnnAssign(_)
|
||||
| AnyNodeRef::StmtFor(_)
|
||||
| AnyNodeRef::StmtAsyncFor(_)
|
||||
| AnyNodeRef::StmtWhile(_)
|
||||
| AnyNodeRef::StmtIf(_)
|
||||
| AnyNodeRef::StmtWith(_)
|
||||
| AnyNodeRef::StmtAsyncWith(_)
|
||||
| AnyNodeRef::StmtMatch(_)
|
||||
| AnyNodeRef::StmtRaise(_)
|
||||
| AnyNodeRef::StmtTry(_)
|
||||
|
@ -4125,7 +3981,6 @@ impl AnyNodeRef<'_> {
|
|||
AnyNodeRef::ModModule(_) | AnyNodeRef::ModExpression(_) => true,
|
||||
|
||||
AnyNodeRef::StmtFunctionDef(_)
|
||||
| AnyNodeRef::StmtAsyncFunctionDef(_)
|
||||
| AnyNodeRef::StmtClassDef(_)
|
||||
| AnyNodeRef::StmtReturn(_)
|
||||
| AnyNodeRef::StmtDelete(_)
|
||||
|
@ -4134,11 +3989,9 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::StmtAugAssign(_)
|
||||
| AnyNodeRef::StmtAnnAssign(_)
|
||||
| AnyNodeRef::StmtFor(_)
|
||||
| AnyNodeRef::StmtAsyncFor(_)
|
||||
| AnyNodeRef::StmtWhile(_)
|
||||
| AnyNodeRef::StmtIf(_)
|
||||
| AnyNodeRef::StmtWith(_)
|
||||
| AnyNodeRef::StmtAsyncWith(_)
|
||||
| AnyNodeRef::StmtMatch(_)
|
||||
| AnyNodeRef::StmtRaise(_)
|
||||
| AnyNodeRef::StmtTry(_)
|
||||
|
@ -4222,7 +4075,6 @@ impl AnyNodeRef<'_> {
|
|||
AnyNodeRef::ModModule(_)
|
||||
| AnyNodeRef::ModExpression(_)
|
||||
| AnyNodeRef::StmtFunctionDef(_)
|
||||
| AnyNodeRef::StmtAsyncFunctionDef(_)
|
||||
| AnyNodeRef::StmtClassDef(_)
|
||||
| AnyNodeRef::StmtReturn(_)
|
||||
| AnyNodeRef::StmtDelete(_)
|
||||
|
@ -4231,11 +4083,9 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::StmtAugAssign(_)
|
||||
| AnyNodeRef::StmtAnnAssign(_)
|
||||
| AnyNodeRef::StmtFor(_)
|
||||
| AnyNodeRef::StmtAsyncFor(_)
|
||||
| AnyNodeRef::StmtWhile(_)
|
||||
| AnyNodeRef::StmtIf(_)
|
||||
| AnyNodeRef::StmtWith(_)
|
||||
| AnyNodeRef::StmtAsyncWith(_)
|
||||
| AnyNodeRef::StmtMatch(_)
|
||||
| AnyNodeRef::StmtRaise(_)
|
||||
| AnyNodeRef::StmtTry(_)
|
||||
|
@ -4304,7 +4154,6 @@ impl AnyNodeRef<'_> {
|
|||
AnyNodeRef::ModModule(_)
|
||||
| AnyNodeRef::ModExpression(_)
|
||||
| AnyNodeRef::StmtFunctionDef(_)
|
||||
| AnyNodeRef::StmtAsyncFunctionDef(_)
|
||||
| AnyNodeRef::StmtClassDef(_)
|
||||
| AnyNodeRef::StmtReturn(_)
|
||||
| AnyNodeRef::StmtDelete(_)
|
||||
|
@ -4313,11 +4162,9 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::StmtAugAssign(_)
|
||||
| AnyNodeRef::StmtAnnAssign(_)
|
||||
| AnyNodeRef::StmtFor(_)
|
||||
| AnyNodeRef::StmtAsyncFor(_)
|
||||
| AnyNodeRef::StmtWhile(_)
|
||||
| AnyNodeRef::StmtIf(_)
|
||||
| AnyNodeRef::StmtWith(_)
|
||||
| AnyNodeRef::StmtAsyncWith(_)
|
||||
| AnyNodeRef::StmtMatch(_)
|
||||
| AnyNodeRef::StmtRaise(_)
|
||||
| AnyNodeRef::StmtTry(_)
|
||||
|
@ -4391,13 +4238,10 @@ impl AnyNodeRef<'_> {
|
|||
self,
|
||||
AnyNodeRef::StmtIf(_)
|
||||
| AnyNodeRef::StmtFor(_)
|
||||
| AnyNodeRef::StmtAsyncFor(_)
|
||||
| AnyNodeRef::StmtWhile(_)
|
||||
| AnyNodeRef::StmtWith(_)
|
||||
| AnyNodeRef::StmtAsyncWith(_)
|
||||
| AnyNodeRef::StmtMatch(_)
|
||||
| AnyNodeRef::StmtFunctionDef(_)
|
||||
| AnyNodeRef::StmtAsyncFunctionDef(_)
|
||||
| AnyNodeRef::StmtClassDef(_)
|
||||
| AnyNodeRef::StmtTry(_)
|
||||
| AnyNodeRef::StmtTryStar(_)
|
||||
|
@ -4435,12 +4279,6 @@ impl<'a> From<&'a ast::StmtFunctionDef> for AnyNodeRef<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::StmtAsyncFunctionDef> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a ast::StmtAsyncFunctionDef) -> Self {
|
||||
AnyNodeRef::StmtAsyncFunctionDef(node)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::StmtClassDef> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a ast::StmtClassDef) -> Self {
|
||||
AnyNodeRef::StmtClassDef(node)
|
||||
|
@ -4489,12 +4327,6 @@ impl<'a> From<&'a ast::StmtFor> for AnyNodeRef<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::StmtAsyncFor> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a ast::StmtAsyncFor) -> Self {
|
||||
AnyNodeRef::StmtAsyncFor(node)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::StmtWhile> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a ast::StmtWhile) -> Self {
|
||||
AnyNodeRef::StmtWhile(node)
|
||||
|
@ -4519,12 +4351,6 @@ impl<'a> From<&'a ast::StmtWith> for AnyNodeRef<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::StmtAsyncWith> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a ast::StmtAsyncWith) -> Self {
|
||||
AnyNodeRef::StmtAsyncWith(node)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::StmtMatch> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a ast::StmtMatch) -> Self {
|
||||
AnyNodeRef::StmtMatch(node)
|
||||
|
@ -4864,7 +4690,6 @@ impl<'a> From<&'a Stmt> for AnyNodeRef<'a> {
|
|||
fn from(stmt: &'a Stmt) -> Self {
|
||||
match stmt {
|
||||
Stmt::FunctionDef(node) => AnyNodeRef::StmtFunctionDef(node),
|
||||
Stmt::AsyncFunctionDef(node) => AnyNodeRef::StmtAsyncFunctionDef(node),
|
||||
Stmt::ClassDef(node) => AnyNodeRef::StmtClassDef(node),
|
||||
Stmt::Return(node) => AnyNodeRef::StmtReturn(node),
|
||||
Stmt::Delete(node) => AnyNodeRef::StmtDelete(node),
|
||||
|
@ -4873,11 +4698,9 @@ impl<'a> From<&'a Stmt> for AnyNodeRef<'a> {
|
|||
Stmt::AugAssign(node) => AnyNodeRef::StmtAugAssign(node),
|
||||
Stmt::AnnAssign(node) => AnyNodeRef::StmtAnnAssign(node),
|
||||
Stmt::For(node) => AnyNodeRef::StmtFor(node),
|
||||
Stmt::AsyncFor(node) => AnyNodeRef::StmtAsyncFor(node),
|
||||
Stmt::While(node) => AnyNodeRef::StmtWhile(node),
|
||||
Stmt::If(node) => AnyNodeRef::StmtIf(node),
|
||||
Stmt::With(node) => AnyNodeRef::StmtWith(node),
|
||||
Stmt::AsyncWith(node) => AnyNodeRef::StmtAsyncWith(node),
|
||||
Stmt::Match(node) => AnyNodeRef::StmtMatch(node),
|
||||
Stmt::Raise(node) => AnyNodeRef::StmtRaise(node),
|
||||
Stmt::Try(node) => AnyNodeRef::StmtTry(node),
|
||||
|
@ -5027,7 +4850,6 @@ impl Ranged for AnyNodeRef<'_> {
|
|||
AnyNodeRef::ModModule(node) => node.range(),
|
||||
AnyNodeRef::ModExpression(node) => node.range(),
|
||||
AnyNodeRef::StmtFunctionDef(node) => node.range(),
|
||||
AnyNodeRef::StmtAsyncFunctionDef(node) => node.range(),
|
||||
AnyNodeRef::StmtClassDef(node) => node.range(),
|
||||
AnyNodeRef::StmtReturn(node) => node.range(),
|
||||
AnyNodeRef::StmtDelete(node) => node.range(),
|
||||
|
@ -5036,11 +4858,9 @@ impl Ranged for AnyNodeRef<'_> {
|
|||
AnyNodeRef::StmtAugAssign(node) => node.range(),
|
||||
AnyNodeRef::StmtAnnAssign(node) => node.range(),
|
||||
AnyNodeRef::StmtFor(node) => node.range(),
|
||||
AnyNodeRef::StmtAsyncFor(node) => node.range(),
|
||||
AnyNodeRef::StmtWhile(node) => node.range(),
|
||||
AnyNodeRef::StmtIf(node) => node.range(),
|
||||
AnyNodeRef::StmtWith(node) => node.range(),
|
||||
AnyNodeRef::StmtAsyncWith(node) => node.range(),
|
||||
AnyNodeRef::StmtMatch(node) => node.range(),
|
||||
AnyNodeRef::StmtRaise(node) => node.range(),
|
||||
AnyNodeRef::StmtTry(node) => node.range(),
|
||||
|
@ -5118,7 +4938,6 @@ pub enum NodeKind {
|
|||
ModExpression,
|
||||
ModFunctionType,
|
||||
StmtFunctionDef,
|
||||
StmtAsyncFunctionDef,
|
||||
StmtClassDef,
|
||||
StmtReturn,
|
||||
StmtDelete,
|
||||
|
@ -5127,11 +4946,9 @@ pub enum NodeKind {
|
|||
StmtAugAssign,
|
||||
StmtAnnAssign,
|
||||
StmtFor,
|
||||
StmtAsyncFor,
|
||||
StmtWhile,
|
||||
StmtIf,
|
||||
StmtWith,
|
||||
StmtAsyncWith,
|
||||
StmtMatch,
|
||||
StmtRaise,
|
||||
StmtTry,
|
||||
|
|
|
@ -45,8 +45,6 @@ impl From<ModExpression> for Mod {
|
|||
pub enum Stmt {
|
||||
#[is(name = "function_def_stmt")]
|
||||
FunctionDef(StmtFunctionDef),
|
||||
#[is(name = "async_function_def_stmt")]
|
||||
AsyncFunctionDef(StmtAsyncFunctionDef),
|
||||
#[is(name = "class_def_stmt")]
|
||||
ClassDef(StmtClassDef),
|
||||
#[is(name = "return_stmt")]
|
||||
|
@ -63,16 +61,12 @@ pub enum Stmt {
|
|||
TypeAlias(StmtTypeAlias),
|
||||
#[is(name = "for_stmt")]
|
||||
For(StmtFor),
|
||||
#[is(name = "async_for_stmt")]
|
||||
AsyncFor(StmtAsyncFor),
|
||||
#[is(name = "while_stmt")]
|
||||
While(StmtWhile),
|
||||
#[is(name = "if_stmt")]
|
||||
If(StmtIf),
|
||||
#[is(name = "with_stmt")]
|
||||
With(StmtWith),
|
||||
#[is(name = "async_with_stmt")]
|
||||
AsyncWith(StmtAsyncWith),
|
||||
#[is(name = "match_stmt")]
|
||||
Match(StmtMatch),
|
||||
#[is(name = "raise_stmt")]
|
||||
|
@ -118,10 +112,15 @@ impl From<StmtLineMagic> for Stmt {
|
|||
}
|
||||
}
|
||||
|
||||
/// See also [FunctionDef](https://docs.python.org/3/library/ast.html#ast.FunctionDef)
|
||||
/// See also [FunctionDef](https://docs.python.org/3/library/ast.html#ast.FunctionDef) and
|
||||
/// [AsyncFunctionDef](https://docs.python.org/3/library/ast.html#ast.AsyncFunctionDef).
|
||||
///
|
||||
/// This type differs from the original Python AST, as it collapses the
|
||||
/// synchronous and asynchronous variants into a single type.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtFunctionDef {
|
||||
pub range: TextRange,
|
||||
pub is_async: bool,
|
||||
pub decorator_list: Vec<Decorator>,
|
||||
pub name: Identifier,
|
||||
pub type_params: Option<TypeParams>,
|
||||
|
@ -136,24 +135,6 @@ impl From<StmtFunctionDef> for Stmt {
|
|||
}
|
||||
}
|
||||
|
||||
/// See also [AsyncFunctionDef](https://docs.python.org/3/library/ast.html#ast.AsyncFunctionDef)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtAsyncFunctionDef {
|
||||
pub range: TextRange,
|
||||
pub decorator_list: Vec<Decorator>,
|
||||
pub name: Identifier,
|
||||
pub type_params: Option<TypeParams>,
|
||||
pub parameters: Box<Parameters>,
|
||||
pub returns: Option<Box<Expr>>,
|
||||
pub body: Vec<Stmt>,
|
||||
}
|
||||
|
||||
impl From<StmtAsyncFunctionDef> for Stmt {
|
||||
fn from(payload: StmtAsyncFunctionDef) -> Self {
|
||||
Stmt::AsyncFunctionDef(payload)
|
||||
}
|
||||
}
|
||||
|
||||
/// See also [ClassDef](https://docs.python.org/3/library/ast.html#ast.ClassDef)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtClassDef {
|
||||
|
@ -275,10 +256,15 @@ impl From<StmtAnnAssign> for Stmt {
|
|||
}
|
||||
}
|
||||
|
||||
/// See also [For](https://docs.python.org/3/library/ast.html#ast.For)
|
||||
/// See also [For](https://docs.python.org/3/library/ast.html#ast.For) and
|
||||
/// [AsyncFor](https://docs.python.org/3/library/ast.html#ast.AsyncFor).
|
||||
///
|
||||
/// This type differs from the original Python AST, as it collapses the
|
||||
/// synchronous and asynchronous variants into a single type.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtFor {
|
||||
pub range: TextRange,
|
||||
pub is_async: bool,
|
||||
pub target: Box<Expr>,
|
||||
pub iter: Box<Expr>,
|
||||
pub body: Vec<Stmt>,
|
||||
|
@ -291,23 +277,8 @@ impl From<StmtFor> for Stmt {
|
|||
}
|
||||
}
|
||||
|
||||
/// See also [AsyncFor](https://docs.python.org/3/library/ast.html#ast.AsyncFor)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtAsyncFor {
|
||||
pub range: TextRange,
|
||||
pub target: Box<Expr>,
|
||||
pub iter: Box<Expr>,
|
||||
pub body: Vec<Stmt>,
|
||||
pub orelse: Vec<Stmt>,
|
||||
}
|
||||
|
||||
impl From<StmtAsyncFor> for Stmt {
|
||||
fn from(payload: StmtAsyncFor) -> Self {
|
||||
Stmt::AsyncFor(payload)
|
||||
}
|
||||
}
|
||||
|
||||
/// See also [While](https://docs.python.org/3/library/ast.html#ast.While)
|
||||
/// See also [While](https://docs.python.org/3/library/ast.html#ast.While) and
|
||||
/// [AsyncWhile](https://docs.python.org/3/library/ast.html#ast.AsyncWhile).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtWhile {
|
||||
pub range: TextRange,
|
||||
|
@ -344,10 +315,15 @@ pub struct ElifElseClause {
|
|||
pub body: Vec<Stmt>,
|
||||
}
|
||||
|
||||
/// See also [With](https://docs.python.org/3/library/ast.html#ast.With)
|
||||
/// See also [With](https://docs.python.org/3/library/ast.html#ast.With) and
|
||||
/// [AsyncWith](https://docs.python.org/3/library/ast.html#ast.AsyncWith).
|
||||
///
|
||||
/// This type differs from the original Python AST, as it collapses the
|
||||
/// synchronous and asynchronous variants into a single type.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtWith {
|
||||
pub range: TextRange,
|
||||
pub is_async: bool,
|
||||
pub items: Vec<WithItem>,
|
||||
pub body: Vec<Stmt>,
|
||||
}
|
||||
|
@ -358,20 +334,6 @@ impl From<StmtWith> for Stmt {
|
|||
}
|
||||
}
|
||||
|
||||
/// See also [AsyncWith](https://docs.python.org/3/library/ast.html#ast.AsyncWith)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtAsyncWith {
|
||||
pub range: TextRange,
|
||||
pub items: Vec<WithItem>,
|
||||
pub body: Vec<Stmt>,
|
||||
}
|
||||
|
||||
impl From<StmtAsyncWith> for Stmt {
|
||||
fn from(payload: StmtAsyncWith) -> Self {
|
||||
Stmt::AsyncWith(payload)
|
||||
}
|
||||
}
|
||||
|
||||
/// See also [Match](https://docs.python.org/3/library/ast.html#ast.Match)
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct StmtMatch {
|
||||
|
@ -2599,11 +2561,6 @@ impl Ranged for crate::nodes::StmtFunctionDef {
|
|||
self.range
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::StmtAsyncFunctionDef {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::StmtClassDef {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
|
@ -2644,11 +2601,6 @@ impl Ranged for crate::nodes::StmtFor {
|
|||
self.range
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::StmtAsyncFor {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::StmtWhile {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
|
@ -2669,11 +2621,6 @@ impl Ranged for crate::nodes::StmtWith {
|
|||
self.range
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::StmtAsyncWith {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
}
|
||||
}
|
||||
impl Ranged for crate::nodes::StmtMatch {
|
||||
fn range(&self) -> TextRange {
|
||||
self.range
|
||||
|
@ -2748,7 +2695,6 @@ impl Ranged for crate::Stmt {
|
|||
fn range(&self) -> TextRange {
|
||||
match self {
|
||||
Self::FunctionDef(node) => node.range(),
|
||||
Self::AsyncFunctionDef(node) => node.range(),
|
||||
Self::ClassDef(node) => node.range(),
|
||||
Self::Return(node) => node.range(),
|
||||
Self::Delete(node) => node.range(),
|
||||
|
@ -2757,11 +2703,9 @@ impl Ranged for crate::Stmt {
|
|||
Self::AugAssign(node) => node.range(),
|
||||
Self::AnnAssign(node) => node.range(),
|
||||
Self::For(node) => node.range(),
|
||||
Self::AsyncFor(node) => node.range(),
|
||||
Self::While(node) => node.range(),
|
||||
Self::If(node) => node.range(),
|
||||
Self::With(node) => node.range(),
|
||||
Self::AsyncWith(node) => node.range(),
|
||||
Self::Match(node) => node.range(),
|
||||
Self::Raise(node) => node.range(),
|
||||
Self::Try(node) => node.range(),
|
||||
|
@ -3110,8 +3054,7 @@ mod size_assertions {
|
|||
use static_assertions::assert_eq_size;
|
||||
|
||||
assert_eq_size!(Stmt, [u8; 144]);
|
||||
assert_eq_size!(StmtFunctionDef, [u8; 136]);
|
||||
assert_eq_size!(StmtAsyncFunctionDef, [u8; 136]);
|
||||
assert_eq_size!(StmtFunctionDef, [u8; 144]);
|
||||
assert_eq_size!(StmtClassDef, [u8; 104]);
|
||||
assert_eq_size!(StmtTry, [u8; 104]);
|
||||
assert_eq_size!(Expr, [u8; 80]);
|
||||
|
|
|
@ -32,9 +32,6 @@ pub fn walk_stmt<'a, V: StatementVisitor<'a> + ?Sized>(visitor: &mut V, stmt: &'
|
|||
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
Stmt::For(ast::StmtFor { body, orelse, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
visitor.visit_body(orelse);
|
||||
|
@ -42,10 +39,6 @@ pub fn walk_stmt<'a, V: StatementVisitor<'a> + ?Sized>(visitor: &mut V, stmt: &'
|
|||
Stmt::ClassDef(ast::StmtClassDef { body, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
visitor.visit_body(orelse);
|
||||
}
|
||||
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
visitor.visit_body(orelse);
|
||||
|
@ -63,9 +56,6 @@ pub fn walk_stmt<'a, V: StatementVisitor<'a> + ?Sized>(visitor: &mut V, stmt: &'
|
|||
Stmt::With(ast::StmtWith { body, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => {
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => {
|
||||
for match_case in cases {
|
||||
visitor.visit_match_case(match_case);
|
||||
|
|
|
@ -5,7 +5,6 @@ use crate::{self as ast, ExceptHandler, Stmt, Suite};
|
|||
pub fn suite<'a>(stmt: &'a Stmt, parent: &'a Stmt) -> Option<&'a Suite> {
|
||||
match parent {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { body, .. }) => Some(body),
|
||||
Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. }) => Some(body),
|
||||
Stmt::ClassDef(ast::StmtClassDef { body, .. }) => Some(body),
|
||||
Stmt::For(ast::StmtFor { body, orelse, .. }) => {
|
||||
if body.contains(stmt) {
|
||||
|
@ -16,15 +15,6 @@ pub fn suite<'a>(stmt: &'a Stmt, parent: &'a Stmt) -> Option<&'a Suite> {
|
|||
None
|
||||
}
|
||||
}
|
||||
Stmt::AsyncFor(ast::StmtAsyncFor { body, orelse, .. }) => {
|
||||
if body.contains(stmt) {
|
||||
Some(body)
|
||||
} else if orelse.contains(stmt) {
|
||||
Some(orelse)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Stmt::While(ast::StmtWhile { body, orelse, .. }) => {
|
||||
if body.contains(stmt) {
|
||||
Some(body)
|
||||
|
@ -49,7 +39,6 @@ pub fn suite<'a>(stmt: &'a Stmt, parent: &'a Stmt) -> Option<&'a Suite> {
|
|||
}
|
||||
}
|
||||
Stmt::With(ast::StmtWith { body, .. }) => Some(body),
|
||||
Stmt::AsyncWith(ast::StmtAsyncWith { body, .. }) => Some(body),
|
||||
Stmt::Match(ast::StmtMatch { cases, .. }) => cases
|
||||
.iter()
|
||||
.map(|case| &case.body)
|
||||
|
|
|
@ -128,26 +128,6 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
}
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
parameters,
|
||||
body,
|
||||
decorator_list,
|
||||
returns,
|
||||
type_params,
|
||||
..
|
||||
}) => {
|
||||
for decorator in decorator_list {
|
||||
visitor.visit_decorator(decorator);
|
||||
}
|
||||
if let Some(type_params) = type_params {
|
||||
visitor.visit_type_params(type_params);
|
||||
}
|
||||
visitor.visit_parameters(parameters);
|
||||
for expr in returns {
|
||||
visitor.visit_annotation(expr);
|
||||
}
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
Stmt::ClassDef(ast::StmtClassDef {
|
||||
arguments,
|
||||
body,
|
||||
|
@ -228,18 +208,6 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
visitor.visit_body(body);
|
||||
visitor.visit_body(orelse);
|
||||
}
|
||||
Stmt::AsyncFor(ast::StmtAsyncFor {
|
||||
target,
|
||||
iter,
|
||||
body,
|
||||
orelse,
|
||||
..
|
||||
}) => {
|
||||
visitor.visit_expr(iter);
|
||||
visitor.visit_expr(target);
|
||||
visitor.visit_body(body);
|
||||
visitor.visit_body(orelse);
|
||||
}
|
||||
Stmt::While(ast::StmtWhile {
|
||||
test,
|
||||
body,
|
||||
|
@ -271,12 +239,6 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
}
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
Stmt::AsyncWith(ast::StmtAsyncWith { items, body, .. }) => {
|
||||
for with_item in items {
|
||||
visitor.visit_with_item(with_item);
|
||||
}
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
Stmt::Match(ast::StmtMatch {
|
||||
subject,
|
||||
cases,
|
||||
|
|
|
@ -145,14 +145,6 @@ where
|
|||
returns,
|
||||
type_params,
|
||||
..
|
||||
})
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
parameters,
|
||||
body,
|
||||
decorator_list,
|
||||
returns,
|
||||
type_params,
|
||||
..
|
||||
}) => {
|
||||
for decorator in decorator_list {
|
||||
visitor.visit_decorator(decorator);
|
||||
|
@ -261,13 +253,6 @@ where
|
|||
body,
|
||||
orelse,
|
||||
..
|
||||
})
|
||||
| Stmt::AsyncFor(ast::StmtAsyncFor {
|
||||
target,
|
||||
iter,
|
||||
body,
|
||||
orelse,
|
||||
..
|
||||
}) => {
|
||||
visitor.visit_expr(target);
|
||||
visitor.visit_expr(iter);
|
||||
|
@ -302,11 +287,7 @@ where
|
|||
Stmt::With(ast::StmtWith {
|
||||
items,
|
||||
body,
|
||||
range: _,
|
||||
})
|
||||
| Stmt::AsyncWith(ast::StmtAsyncWith {
|
||||
items,
|
||||
body,
|
||||
is_async: _,
|
||||
range: _,
|
||||
}) => {
|
||||
for with_item in items {
|
||||
|
|
|
@ -23,7 +23,6 @@ mod precedence {
|
|||
pub(crate) const YIELD_FROM: u8 = 7;
|
||||
pub(crate) const IF: u8 = 9;
|
||||
pub(crate) const FOR: u8 = 9;
|
||||
pub(crate) const ASYNC_FOR: u8 = 9;
|
||||
pub(crate) const WHILE: u8 = 9;
|
||||
pub(crate) const RETURN: u8 = 11;
|
||||
pub(crate) const SLICE: u8 = 13;
|
||||
|
@ -204,6 +203,7 @@ impl<'a> Generator<'a> {
|
|||
|
||||
match ast {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
is_async,
|
||||
name,
|
||||
parameters,
|
||||
body,
|
||||
|
@ -220,6 +220,9 @@ impl<'a> Generator<'a> {
|
|||
});
|
||||
}
|
||||
statement!({
|
||||
if *is_async {
|
||||
self.p("async ");
|
||||
}
|
||||
self.p("def ");
|
||||
self.p_id(name);
|
||||
if let Some(type_params) = type_params {
|
||||
|
@ -239,42 +242,6 @@ impl<'a> Generator<'a> {
|
|||
self.newlines(2);
|
||||
}
|
||||
}
|
||||
Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
name,
|
||||
parameters,
|
||||
body,
|
||||
returns,
|
||||
decorator_list,
|
||||
type_params,
|
||||
..
|
||||
}) => {
|
||||
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
|
||||
for decorator in decorator_list {
|
||||
statement!({
|
||||
self.p("@");
|
||||
self.unparse_expr(&decorator.expression, precedence::MAX);
|
||||
});
|
||||
}
|
||||
statement!({
|
||||
self.p("async def ");
|
||||
self.p_id(name);
|
||||
if let Some(type_params) = type_params {
|
||||
self.unparse_type_params(type_params);
|
||||
}
|
||||
self.p("(");
|
||||
self.unparse_parameters(parameters);
|
||||
self.p(")");
|
||||
if let Some(returns) = returns {
|
||||
self.p(" -> ");
|
||||
self.unparse_expr(returns, precedence::MAX);
|
||||
}
|
||||
self.p(":");
|
||||
});
|
||||
self.body(body);
|
||||
if self.indent_depth == 0 {
|
||||
self.newlines(2);
|
||||
}
|
||||
}
|
||||
Stmt::ClassDef(ast::StmtClassDef {
|
||||
name,
|
||||
arguments,
|
||||
|
@ -400,6 +367,7 @@ impl<'a> Generator<'a> {
|
|||
});
|
||||
}
|
||||
Stmt::For(ast::StmtFor {
|
||||
is_async,
|
||||
target,
|
||||
iter,
|
||||
body,
|
||||
|
@ -407,6 +375,9 @@ impl<'a> Generator<'a> {
|
|||
..
|
||||
}) => {
|
||||
statement!({
|
||||
if *is_async {
|
||||
self.p("async ");
|
||||
}
|
||||
self.p("for ");
|
||||
self.unparse_expr(target, precedence::FOR);
|
||||
self.p(" in ");
|
||||
|
@ -421,28 +392,6 @@ impl<'a> Generator<'a> {
|
|||
self.body(orelse);
|
||||
}
|
||||
}
|
||||
Stmt::AsyncFor(ast::StmtAsyncFor {
|
||||
target,
|
||||
iter,
|
||||
body,
|
||||
orelse,
|
||||
..
|
||||
}) => {
|
||||
statement!({
|
||||
self.p("async for ");
|
||||
self.unparse_expr(target, precedence::ASYNC_FOR);
|
||||
self.p(" in ");
|
||||
self.unparse_expr(iter, precedence::MAX);
|
||||
self.p(":");
|
||||
});
|
||||
self.body(body);
|
||||
if !orelse.is_empty() {
|
||||
statement!({
|
||||
self.p("else:");
|
||||
});
|
||||
self.body(orelse);
|
||||
}
|
||||
}
|
||||
Stmt::While(ast::StmtWhile {
|
||||
test,
|
||||
body,
|
||||
|
@ -490,21 +439,17 @@ impl<'a> Generator<'a> {
|
|||
self.body(&clause.body);
|
||||
}
|
||||
}
|
||||
Stmt::With(ast::StmtWith { items, body, .. }) => {
|
||||
Stmt::With(ast::StmtWith {
|
||||
is_async,
|
||||
items,
|
||||
body,
|
||||
..
|
||||
}) => {
|
||||
statement!({
|
||||
self.p("with ");
|
||||
let mut first = true;
|
||||
for item in items {
|
||||
self.p_delim(&mut first, ", ");
|
||||
self.unparse_with_item(item);
|
||||
if *is_async {
|
||||
self.p("async ");
|
||||
}
|
||||
self.p(":");
|
||||
});
|
||||
self.body(body);
|
||||
}
|
||||
Stmt::AsyncWith(ast::StmtAsyncWith { items, body, .. }) => {
|
||||
statement!({
|
||||
self.p("async with ");
|
||||
self.p("with ");
|
||||
let mut first = true;
|
||||
for item in items {
|
||||
self.p_delim(&mut first, ", ");
|
||||
|
|
|
@ -67,9 +67,7 @@ pub(super) fn place_comment<'a>(
|
|||
handle_module_level_own_line_comment_before_class_or_function_comment(comment, locator)
|
||||
}
|
||||
AnyNodeRef::WithItem(_) => handle_with_item_comment(comment, locator),
|
||||
AnyNodeRef::StmtFunctionDef(_) | AnyNodeRef::StmtAsyncFunctionDef(_) => {
|
||||
handle_leading_function_with_decorators_comment(comment)
|
||||
}
|
||||
AnyNodeRef::StmtFunctionDef(_) => handle_leading_function_with_decorators_comment(comment),
|
||||
AnyNodeRef::StmtClassDef(class_def) => {
|
||||
handle_leading_class_with_decorators_comment(comment, class_def)
|
||||
}
|
||||
|
@ -178,7 +176,6 @@ fn handle_end_of_line_comment_around_body<'a>(
|
|||
fn is_first_statement_in_body(statement: AnyNodeRef, has_body: AnyNodeRef) -> bool {
|
||||
match has_body {
|
||||
AnyNodeRef::StmtFor(ast::StmtFor { body, orelse, .. })
|
||||
| AnyNodeRef::StmtAsyncFor(ast::StmtAsyncFor { body, orelse, .. })
|
||||
| AnyNodeRef::StmtWhile(ast::StmtWhile { body, orelse, .. }) => {
|
||||
are_same_optional(statement, body.first())
|
||||
|| are_same_optional(statement, orelse.first())
|
||||
|
@ -208,7 +205,6 @@ fn is_first_statement_in_body(statement: AnyNodeRef, has_body: AnyNodeRef) -> bo
|
|||
body, ..
|
||||
})
|
||||
| AnyNodeRef::StmtFunctionDef(ast::StmtFunctionDef { body, .. })
|
||||
| AnyNodeRef::StmtAsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. })
|
||||
| AnyNodeRef::StmtClassDef(ast::StmtClassDef { body, .. }) => {
|
||||
are_same_optional(statement, body.first())
|
||||
}
|
||||
|
@ -773,9 +769,7 @@ fn handle_module_level_own_line_comment_before_class_or_function_comment<'a>(
|
|||
// ... where the following is a function or class statement.
|
||||
if !matches!(
|
||||
following,
|
||||
AnyNodeRef::StmtAsyncFunctionDef(_)
|
||||
| AnyNodeRef::StmtFunctionDef(_)
|
||||
| AnyNodeRef::StmtClassDef(_)
|
||||
AnyNodeRef::StmtFunctionDef(_) | AnyNodeRef::StmtClassDef(_)
|
||||
) {
|
||||
return CommentPlacement::Default(comment);
|
||||
}
|
||||
|
@ -1408,10 +1402,8 @@ where
|
|||
fn last_child_in_body(node: AnyNodeRef) -> Option<AnyNodeRef> {
|
||||
let body = match node {
|
||||
AnyNodeRef::StmtFunctionDef(ast::StmtFunctionDef { body, .. })
|
||||
| AnyNodeRef::StmtAsyncFunctionDef(ast::StmtAsyncFunctionDef { body, .. })
|
||||
| AnyNodeRef::StmtClassDef(ast::StmtClassDef { body, .. })
|
||||
| AnyNodeRef::StmtWith(ast::StmtWith { body, .. })
|
||||
| AnyNodeRef::StmtAsyncWith(ast::StmtAsyncWith { body, .. })
|
||||
| AnyNodeRef::MatchCase(MatchCase { body, .. })
|
||||
| AnyNodeRef::ExceptHandlerExceptHandler(ast::ExceptHandlerExceptHandler {
|
||||
body, ..
|
||||
|
@ -1424,7 +1416,6 @@ fn last_child_in_body(node: AnyNodeRef) -> Option<AnyNodeRef> {
|
|||
}) => elif_else_clauses.last().map_or(body, |clause| &clause.body),
|
||||
|
||||
AnyNodeRef::StmtFor(ast::StmtFor { body, orelse, .. })
|
||||
| AnyNodeRef::StmtAsyncFor(ast::StmtAsyncFor { body, orelse, .. })
|
||||
| AnyNodeRef::StmtWhile(ast::StmtWhile { body, orelse, .. }) => {
|
||||
if orelse.is_empty() {
|
||||
body
|
||||
|
@ -1477,7 +1468,6 @@ fn last_child_in_body(node: AnyNodeRef) -> Option<AnyNodeRef> {
|
|||
fn is_first_statement_in_alternate_body(statement: AnyNodeRef, has_body: AnyNodeRef) -> bool {
|
||||
match has_body {
|
||||
AnyNodeRef::StmtFor(ast::StmtFor { orelse, .. })
|
||||
| AnyNodeRef::StmtAsyncFor(ast::StmtAsyncFor { orelse, .. })
|
||||
| AnyNodeRef::StmtWhile(ast::StmtWhile { orelse, .. }) => {
|
||||
are_same_optional(statement, orelse.first())
|
||||
}
|
||||
|
|
|
@ -46,7 +46,6 @@ impl NeedsParentheses for ExprNamedExpr {
|
|||
|| parent.is_with_item()
|
||||
|| parent.is_stmt_delete()
|
||||
|| parent.is_stmt_for()
|
||||
|| parent.is_stmt_async_for()
|
||||
{
|
||||
OptionalParentheses::Always
|
||||
} else {
|
||||
|
|
|
@ -108,42 +108,6 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtFunctionDef {
|
|||
}
|
||||
}
|
||||
|
||||
impl FormatRule<ast::StmtAsyncFunctionDef, PyFormatContext<'_>>
|
||||
for crate::statement::stmt_async_function_def::FormatStmtAsyncFunctionDef
|
||||
{
|
||||
#[inline]
|
||||
fn fmt(&self, node: &ast::StmtAsyncFunctionDef, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
FormatNodeRule::<ast::StmtAsyncFunctionDef>::fmt(self, node, f)
|
||||
}
|
||||
}
|
||||
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::StmtAsyncFunctionDef {
|
||||
type Format<'a> = FormatRefWithRule<
|
||||
'a,
|
||||
ast::StmtAsyncFunctionDef,
|
||||
crate::statement::stmt_async_function_def::FormatStmtAsyncFunctionDef,
|
||||
PyFormatContext<'ast>,
|
||||
>;
|
||||
fn format(&self) -> Self::Format<'_> {
|
||||
FormatRefWithRule::new(
|
||||
self,
|
||||
crate::statement::stmt_async_function_def::FormatStmtAsyncFunctionDef::default(),
|
||||
)
|
||||
}
|
||||
}
|
||||
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtAsyncFunctionDef {
|
||||
type Format = FormatOwnedWithRule<
|
||||
ast::StmtAsyncFunctionDef,
|
||||
crate::statement::stmt_async_function_def::FormatStmtAsyncFunctionDef,
|
||||
PyFormatContext<'ast>,
|
||||
>;
|
||||
fn into_format(self) -> Self::Format {
|
||||
FormatOwnedWithRule::new(
|
||||
self,
|
||||
crate::statement::stmt_async_function_def::FormatStmtAsyncFunctionDef::default(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl FormatRule<ast::StmtClassDef, PyFormatContext<'_>>
|
||||
for crate::statement::stmt_class_def::FormatStmtClassDef
|
||||
{
|
||||
|
@ -424,42 +388,6 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtFor {
|
|||
}
|
||||
}
|
||||
|
||||
impl FormatRule<ast::StmtAsyncFor, PyFormatContext<'_>>
|
||||
for crate::statement::stmt_async_for::FormatStmtAsyncFor
|
||||
{
|
||||
#[inline]
|
||||
fn fmt(&self, node: &ast::StmtAsyncFor, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
FormatNodeRule::<ast::StmtAsyncFor>::fmt(self, node, f)
|
||||
}
|
||||
}
|
||||
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::StmtAsyncFor {
|
||||
type Format<'a> = FormatRefWithRule<
|
||||
'a,
|
||||
ast::StmtAsyncFor,
|
||||
crate::statement::stmt_async_for::FormatStmtAsyncFor,
|
||||
PyFormatContext<'ast>,
|
||||
>;
|
||||
fn format(&self) -> Self::Format<'_> {
|
||||
FormatRefWithRule::new(
|
||||
self,
|
||||
crate::statement::stmt_async_for::FormatStmtAsyncFor::default(),
|
||||
)
|
||||
}
|
||||
}
|
||||
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtAsyncFor {
|
||||
type Format = FormatOwnedWithRule<
|
||||
ast::StmtAsyncFor,
|
||||
crate::statement::stmt_async_for::FormatStmtAsyncFor,
|
||||
PyFormatContext<'ast>,
|
||||
>;
|
||||
fn into_format(self) -> Self::Format {
|
||||
FormatOwnedWithRule::new(
|
||||
self,
|
||||
crate::statement::stmt_async_for::FormatStmtAsyncFor::default(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl FormatRule<ast::StmtWhile, PyFormatContext<'_>>
|
||||
for crate::statement::stmt_while::FormatStmtWhile
|
||||
{
|
||||
|
@ -554,42 +482,6 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtWith {
|
|||
}
|
||||
}
|
||||
|
||||
impl FormatRule<ast::StmtAsyncWith, PyFormatContext<'_>>
|
||||
for crate::statement::stmt_async_with::FormatStmtAsyncWith
|
||||
{
|
||||
#[inline]
|
||||
fn fmt(&self, node: &ast::StmtAsyncWith, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
FormatNodeRule::<ast::StmtAsyncWith>::fmt(self, node, f)
|
||||
}
|
||||
}
|
||||
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::StmtAsyncWith {
|
||||
type Format<'a> = FormatRefWithRule<
|
||||
'a,
|
||||
ast::StmtAsyncWith,
|
||||
crate::statement::stmt_async_with::FormatStmtAsyncWith,
|
||||
PyFormatContext<'ast>,
|
||||
>;
|
||||
fn format(&self) -> Self::Format<'_> {
|
||||
FormatRefWithRule::new(
|
||||
self,
|
||||
crate::statement::stmt_async_with::FormatStmtAsyncWith::default(),
|
||||
)
|
||||
}
|
||||
}
|
||||
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::StmtAsyncWith {
|
||||
type Format = FormatOwnedWithRule<
|
||||
ast::StmtAsyncWith,
|
||||
crate::statement::stmt_async_with::FormatStmtAsyncWith,
|
||||
PyFormatContext<'ast>,
|
||||
>;
|
||||
fn into_format(self) -> Self::Format {
|
||||
FormatOwnedWithRule::new(
|
||||
self,
|
||||
crate::statement::stmt_async_with::FormatStmtAsyncWith::default(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl FormatRule<ast::StmtMatch, PyFormatContext<'_>>
|
||||
for crate::statement::stmt_match::FormatStmtMatch
|
||||
{
|
||||
|
|
|
@ -5,9 +5,6 @@ use ruff_python_ast::Stmt;
|
|||
pub(crate) mod stmt_ann_assign;
|
||||
pub(crate) mod stmt_assert;
|
||||
pub(crate) mod stmt_assign;
|
||||
pub(crate) mod stmt_async_for;
|
||||
pub(crate) mod stmt_async_function_def;
|
||||
pub(crate) mod stmt_async_with;
|
||||
pub(crate) mod stmt_aug_assign;
|
||||
pub(crate) mod stmt_break;
|
||||
pub(crate) mod stmt_class_def;
|
||||
|
@ -40,7 +37,6 @@ impl FormatRule<Stmt, PyFormatContext<'_>> for FormatStmt {
|
|||
fn fmt(&self, item: &Stmt, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
match item {
|
||||
Stmt::FunctionDef(x) => x.format().fmt(f),
|
||||
Stmt::AsyncFunctionDef(x) => x.format().fmt(f),
|
||||
Stmt::ClassDef(x) => x.format().fmt(f),
|
||||
Stmt::Return(x) => x.format().fmt(f),
|
||||
Stmt::Delete(x) => x.format().fmt(f),
|
||||
|
@ -48,11 +44,9 @@ impl FormatRule<Stmt, PyFormatContext<'_>> for FormatStmt {
|
|||
Stmt::AugAssign(x) => x.format().fmt(f),
|
||||
Stmt::AnnAssign(x) => x.format().fmt(f),
|
||||
Stmt::For(x) => x.format().fmt(f),
|
||||
Stmt::AsyncFor(x) => x.format().fmt(f),
|
||||
Stmt::While(x) => x.format().fmt(f),
|
||||
Stmt::If(x) => x.format().fmt(f),
|
||||
Stmt::With(x) => x.format().fmt(f),
|
||||
Stmt::AsyncWith(x) => x.format().fmt(f),
|
||||
Stmt::Match(x) => x.format().fmt(f),
|
||||
Stmt::Raise(x) => x.format().fmt(f),
|
||||
Stmt::Try(x) => x.format().fmt(f),
|
||||
|
|
|
@ -1,23 +0,0 @@
|
|||
use crate::prelude::*;
|
||||
use crate::statement::stmt_for::AnyStatementFor;
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::FormatResult;
|
||||
use ruff_python_ast::StmtAsyncFor;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatStmtAsyncFor;
|
||||
|
||||
impl FormatNodeRule<StmtAsyncFor> for FormatStmtAsyncFor {
|
||||
fn fmt_fields(&self, item: &StmtAsyncFor, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
AnyStatementFor::from(item).fmt(f)
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &StmtAsyncFor,
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
use ruff_python_ast::StmtAsyncFunctionDef;
|
||||
|
||||
use ruff_python_ast::function::AnyFunctionDefinition;
|
||||
|
||||
use crate::prelude::*;
|
||||
use crate::FormatNodeRule;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatStmtAsyncFunctionDef;
|
||||
|
||||
impl FormatNodeRule<StmtAsyncFunctionDef> for FormatStmtAsyncFunctionDef {
|
||||
fn fmt_fields(&self, item: &StmtAsyncFunctionDef, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
AnyFunctionDefinition::from(item).format().fmt(f)
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &StmtAsyncFunctionDef,
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled by `AnyFunctionDef`
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
use crate::prelude::*;
|
||||
use crate::statement::stmt_with::AnyStatementWith;
|
||||
use crate::FormatNodeRule;
|
||||
use ruff_python_ast::StmtAsyncWith;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatStmtAsyncWith;
|
||||
|
||||
impl FormatNodeRule<StmtAsyncWith> for FormatStmtAsyncWith {
|
||||
fn fmt_fields(&self, item: &StmtAsyncWith, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
AnyStatementWith::from(item).fmt(f)
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &StmtAsyncWith,
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
Ok(())
|
||||
}
|
||||
}
|
|
@ -1,13 +1,12 @@
|
|||
use ruff_formatter::{format_args, write, Buffer, FormatResult};
|
||||
use ruff_python_ast::{Expr, Ranged, Stmt, StmtFor};
|
||||
|
||||
use crate::comments::{leading_alternate_branch_comments, trailing_comments};
|
||||
use crate::expression::expr_tuple::TupleParentheses;
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::prelude::*;
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::{format_args, write, Buffer, FormatResult};
|
||||
use ruff_python_ast::node::AnyNodeRef;
|
||||
use ruff_python_ast::{Expr, Ranged, Stmt, StmtAsyncFor, StmtFor, Suite};
|
||||
use ruff_text_size::TextRange;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ExprTupleWithoutParentheses<'a>(&'a Expr);
|
||||
|
@ -27,85 +26,19 @@ impl Format<PyFormatContext<'_>> for ExprTupleWithoutParentheses<'_> {
|
|||
#[derive(Default)]
|
||||
pub struct FormatStmtFor;
|
||||
|
||||
pub(super) enum AnyStatementFor<'a> {
|
||||
For(&'a StmtFor),
|
||||
AsyncFor(&'a StmtAsyncFor),
|
||||
}
|
||||
|
||||
impl<'a> AnyStatementFor<'a> {
|
||||
const fn is_async(&self) -> bool {
|
||||
matches!(self, AnyStatementFor::AsyncFor(_))
|
||||
}
|
||||
|
||||
fn target(&self) -> &Expr {
|
||||
match self {
|
||||
AnyStatementFor::For(stmt) => &stmt.target,
|
||||
AnyStatementFor::AsyncFor(stmt) => &stmt.target,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::iter_not_returning_iterator)]
|
||||
fn iter(&self) -> &Expr {
|
||||
match self {
|
||||
AnyStatementFor::For(stmt) => &stmt.iter,
|
||||
AnyStatementFor::AsyncFor(stmt) => &stmt.iter,
|
||||
}
|
||||
}
|
||||
|
||||
fn body(&self) -> &Suite {
|
||||
match self {
|
||||
AnyStatementFor::For(stmt) => &stmt.body,
|
||||
AnyStatementFor::AsyncFor(stmt) => &stmt.body,
|
||||
}
|
||||
}
|
||||
|
||||
fn orelse(&self) -> &Suite {
|
||||
match self {
|
||||
AnyStatementFor::For(stmt) => &stmt.orelse,
|
||||
AnyStatementFor::AsyncFor(stmt) => &stmt.orelse,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Ranged for AnyStatementFor<'_> {
|
||||
fn range(&self) -> TextRange {
|
||||
match self {
|
||||
AnyStatementFor::For(stmt) => stmt.range(),
|
||||
AnyStatementFor::AsyncFor(stmt) => stmt.range(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a StmtFor> for AnyStatementFor<'a> {
|
||||
fn from(value: &'a StmtFor) -> Self {
|
||||
AnyStatementFor::For(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a StmtAsyncFor> for AnyStatementFor<'a> {
|
||||
fn from(value: &'a StmtAsyncFor) -> Self {
|
||||
AnyStatementFor::AsyncFor(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&AnyStatementFor<'a>> for AnyNodeRef<'a> {
|
||||
fn from(value: &AnyStatementFor<'a>) -> Self {
|
||||
match value {
|
||||
AnyStatementFor::For(stmt) => AnyNodeRef::StmtFor(stmt),
|
||||
AnyStatementFor::AsyncFor(stmt) => AnyNodeRef::StmtAsyncFor(stmt),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for AnyStatementFor<'_> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let target = self.target();
|
||||
let iter = self.iter();
|
||||
let body = self.body();
|
||||
let orelse = self.orelse();
|
||||
impl FormatNodeRule<StmtFor> for FormatStmtFor {
|
||||
fn fmt_fields(&self, item: &StmtFor, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let StmtFor {
|
||||
is_async,
|
||||
target,
|
||||
iter,
|
||||
body,
|
||||
orelse,
|
||||
range: _,
|
||||
} = item;
|
||||
|
||||
let comments = f.context().comments().clone();
|
||||
let dangling_comments = comments.dangling_comments(self);
|
||||
let dangling_comments = comments.dangling_comments(item);
|
||||
let body_start = body.first().map_or(iter.end(), Stmt::start);
|
||||
let or_else_comments_start =
|
||||
dangling_comments.partition_point(|comment| comment.slice().end() < body_start);
|
||||
|
@ -116,15 +49,14 @@ impl Format<PyFormatContext<'_>> for AnyStatementFor<'_> {
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
self.is_async()
|
||||
.then_some(format_args![text("async"), space()]),
|
||||
is_async.then_some(format_args![text("async"), space()]),
|
||||
text("for"),
|
||||
space(),
|
||||
ExprTupleWithoutParentheses(target),
|
||||
space(),
|
||||
text("in"),
|
||||
space(),
|
||||
maybe_parenthesize_expression(iter, self, Parenthesize::IfBreaks),
|
||||
maybe_parenthesize_expression(iter, item, Parenthesize::IfBreaks),
|
||||
text(":"),
|
||||
trailing_comments(trailing_condition_comments),
|
||||
block_indent(&body.format())
|
||||
|
@ -153,12 +85,6 @@ impl Format<PyFormatContext<'_>> for AnyStatementFor<'_> {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl FormatNodeRule<StmtFor> for FormatStmtFor {
|
||||
fn fmt_fields(&self, item: &StmtFor, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
AnyStatementFor::from(item).fmt(f)
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(&self, _node: &StmtFor, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
|
|
|
@ -1,11 +1,8 @@
|
|||
use ruff_formatter::write;
|
||||
use ruff_python_ast::{Ranged, StmtFunctionDef};
|
||||
|
||||
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule};
|
||||
use ruff_python_ast::function::AnyFunctionDefinition;
|
||||
use ruff_python_trivia::lines_after_ignoring_trivia;
|
||||
|
||||
use crate::comments::{leading_comments, trailing_comments};
|
||||
|
||||
use crate::expression::parentheses::{optional_parentheses, Parentheses};
|
||||
use crate::prelude::*;
|
||||
use crate::statement::suite::SuiteKind;
|
||||
|
@ -16,24 +13,6 @@ pub struct FormatStmtFunctionDef;
|
|||
|
||||
impl FormatNodeRule<StmtFunctionDef> for FormatStmtFunctionDef {
|
||||
fn fmt_fields(&self, item: &StmtFunctionDef, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
AnyFunctionDefinition::from(item).format().fmt(f)
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &StmtFunctionDef,
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled by `AnyFunctionDef`
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatAnyFunctionDef;
|
||||
|
||||
impl FormatRule<AnyFunctionDefinition<'_>, PyFormatContext<'_>> for FormatAnyFunctionDef {
|
||||
fn fmt(&self, item: &AnyFunctionDefinition<'_>, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let comments = f.context().comments().clone();
|
||||
|
||||
let dangling_comments = comments.dangling_comments(item);
|
||||
|
@ -43,9 +22,9 @@ impl FormatRule<AnyFunctionDefinition<'_>, PyFormatContext<'_>> for FormatAnyFun
|
|||
let (leading_definition_comments, trailing_definition_comments) =
|
||||
dangling_comments.split_at(trailing_definition_comments_start);
|
||||
|
||||
if let Some(last_decorator) = item.decorators().last() {
|
||||
if let Some(last_decorator) = item.decorator_list.last() {
|
||||
f.join_with(hard_line_break())
|
||||
.entries(item.decorators().iter().formatted())
|
||||
.entries(item.decorator_list.iter().formatted())
|
||||
.finish()?;
|
||||
|
||||
if leading_definition_comments.is_empty() {
|
||||
|
@ -69,21 +48,19 @@ impl FormatRule<AnyFunctionDefinition<'_>, PyFormatContext<'_>> for FormatAnyFun
|
|||
}
|
||||
}
|
||||
|
||||
if item.is_async() {
|
||||
if item.is_async {
|
||||
write!(f, [text("async"), space()])?;
|
||||
}
|
||||
|
||||
let name = item.name();
|
||||
write!(f, [text("def"), space(), item.name.format()])?;
|
||||
|
||||
write!(f, [text("def"), space(), name.format()])?;
|
||||
|
||||
if let Some(type_params) = item.type_params() {
|
||||
if let Some(type_params) = item.type_params.as_ref() {
|
||||
write!(f, [type_params.format()])?;
|
||||
}
|
||||
|
||||
write!(f, [item.arguments().format()])?;
|
||||
write!(f, [item.parameters.format()])?;
|
||||
|
||||
if let Some(return_annotation) = item.returns() {
|
||||
if let Some(return_annotation) = item.returns.as_ref() {
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
|
@ -102,33 +79,17 @@ impl FormatRule<AnyFunctionDefinition<'_>, PyFormatContext<'_>> for FormatAnyFun
|
|||
[
|
||||
text(":"),
|
||||
trailing_comments(trailing_definition_comments),
|
||||
block_indent(&item.body().format().with_options(SuiteKind::Function))
|
||||
block_indent(&item.body.format().with_options(SuiteKind::Function))
|
||||
]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'def, 'ast> AsFormat<PyFormatContext<'ast>> for AnyFunctionDefinition<'def> {
|
||||
type Format<'a> = FormatRefWithRule<
|
||||
'a,
|
||||
AnyFunctionDefinition<'def>,
|
||||
FormatAnyFunctionDef,
|
||||
PyFormatContext<'ast>,
|
||||
> where Self: 'a;
|
||||
|
||||
fn format(&self) -> Self::Format<'_> {
|
||||
FormatRefWithRule::new(self, FormatAnyFunctionDef)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'def, 'ast> IntoFormat<PyFormatContext<'ast>> for AnyFunctionDefinition<'def> {
|
||||
type Format = FormatOwnedWithRule<
|
||||
AnyFunctionDefinition<'def>,
|
||||
FormatAnyFunctionDef,
|
||||
PyFormatContext<'ast>,
|
||||
>;
|
||||
|
||||
fn into_format(self) -> Self::Format {
|
||||
FormatOwnedWithRule::new(self, FormatAnyFunctionDef)
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_node: &StmtFunctionDef,
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
use ruff_python_ast::{Ranged, StmtAsyncWith, StmtWith, Suite, WithItem};
|
||||
use ruff_text_size::TextRange;
|
||||
|
||||
use ruff_formatter::{format_args, write, FormatError};
|
||||
use ruff_python_ast::node::AnyNodeRef;
|
||||
use ruff_python_ast::{Ranged, StmtWith};
|
||||
use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
|
||||
use ruff_text_size::TextRange;
|
||||
|
||||
use crate::comments::trailing_comments;
|
||||
use crate::expression::parentheses::{
|
||||
|
@ -12,81 +10,29 @@ use crate::expression::parentheses::{
|
|||
use crate::prelude::*;
|
||||
use crate::FormatNodeRule;
|
||||
|
||||
pub(super) enum AnyStatementWith<'a> {
|
||||
With(&'a StmtWith),
|
||||
AsyncWith(&'a StmtAsyncWith),
|
||||
}
|
||||
#[derive(Default)]
|
||||
pub struct FormatStmtWith;
|
||||
|
||||
impl<'a> AnyStatementWith<'a> {
|
||||
const fn is_async(&self) -> bool {
|
||||
matches!(self, AnyStatementWith::AsyncWith(_))
|
||||
}
|
||||
|
||||
fn items(&self) -> &[WithItem] {
|
||||
match self {
|
||||
AnyStatementWith::With(with) => with.items.as_slice(),
|
||||
AnyStatementWith::AsyncWith(with) => with.items.as_slice(),
|
||||
}
|
||||
}
|
||||
|
||||
fn body(&self) -> &Suite {
|
||||
match self {
|
||||
AnyStatementWith::With(with) => &with.body,
|
||||
AnyStatementWith::AsyncWith(with) => &with.body,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Ranged for AnyStatementWith<'_> {
|
||||
fn range(&self) -> TextRange {
|
||||
match self {
|
||||
AnyStatementWith::With(with) => with.range(),
|
||||
AnyStatementWith::AsyncWith(with) => with.range(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a StmtWith> for AnyStatementWith<'a> {
|
||||
fn from(value: &'a StmtWith) -> Self {
|
||||
AnyStatementWith::With(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a StmtAsyncWith> for AnyStatementWith<'a> {
|
||||
fn from(value: &'a StmtAsyncWith) -> Self {
|
||||
AnyStatementWith::AsyncWith(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&AnyStatementWith<'a>> for AnyNodeRef<'a> {
|
||||
fn from(value: &AnyStatementWith<'a>) -> Self {
|
||||
match value {
|
||||
AnyStatementWith::With(with) => AnyNodeRef::StmtWith(with),
|
||||
AnyStatementWith::AsyncWith(with) => AnyNodeRef::StmtAsyncWith(with),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for AnyStatementWith<'_> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
impl FormatNodeRule<StmtWith> for FormatStmtWith {
|
||||
fn fmt_fields(&self, item: &StmtWith, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let comments = f.context().comments().clone();
|
||||
let dangling_comments = comments.dangling_comments(self);
|
||||
let dangling_comments = comments.dangling_comments(item);
|
||||
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
self.is_async()
|
||||
item.is_async
|
||||
.then_some(format_args![text("async"), space()]),
|
||||
text("with"),
|
||||
space()
|
||||
]
|
||||
)?;
|
||||
|
||||
if are_with_items_parenthesized(self, f.context())? {
|
||||
if are_with_items_parenthesized(item, f.context())? {
|
||||
optional_parentheses(&format_with(|f| {
|
||||
let mut joiner = f.join_comma_separated(self.body().first().unwrap().start());
|
||||
let mut joiner = f.join_comma_separated(item.body.first().unwrap().start());
|
||||
|
||||
for item in self.items() {
|
||||
for item in &item.items {
|
||||
joiner.entry_with_line_separator(
|
||||
item,
|
||||
&item.format(),
|
||||
|
@ -98,7 +44,7 @@ impl Format<PyFormatContext<'_>> for AnyStatementWith<'_> {
|
|||
.fmt(f)?;
|
||||
} else {
|
||||
f.join_with(format_args![text(","), space()])
|
||||
.entries(self.items().iter().formatted())
|
||||
.entries(item.items.iter().formatted())
|
||||
.finish()?;
|
||||
}
|
||||
|
||||
|
@ -107,18 +53,20 @@ impl Format<PyFormatContext<'_>> for AnyStatementWith<'_> {
|
|||
[
|
||||
text(":"),
|
||||
trailing_comments(dangling_comments),
|
||||
block_indent(&self.body().format())
|
||||
block_indent(&item.body.format())
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(&self, _node: &StmtWith, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn are_with_items_parenthesized(
|
||||
with: &AnyStatementWith,
|
||||
context: &PyFormatContext,
|
||||
) -> FormatResult<bool> {
|
||||
fn are_with_items_parenthesized(with: &StmtWith, context: &PyFormatContext) -> FormatResult<bool> {
|
||||
let first_with_item = with
|
||||
.items()
|
||||
.items
|
||||
.first()
|
||||
.ok_or(FormatError::syntax_error("Expected at least one with item"))?;
|
||||
let before_first_with_item = TextRange::new(with.start(), first_with_item.start());
|
||||
|
@ -145,17 +93,3 @@ fn are_with_items_parenthesized(
|
|||
None => Ok(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatStmtWith;
|
||||
|
||||
impl FormatNodeRule<StmtWith> for FormatStmtWith {
|
||||
fn fmt_fields(&self, item: &StmtWith, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
AnyStatementWith::from(item).fmt(f)
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(&self, _node: &StmtWith, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -210,10 +210,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
|
|||
|
||||
/// Returns `true` if a [`Stmt`] is a class or function definition.
|
||||
const fn is_class_or_function_definition(stmt: &Stmt) -> bool {
|
||||
matches!(
|
||||
stmt,
|
||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) | Stmt::ClassDef(_)
|
||||
)
|
||||
matches!(stmt, Stmt::FunctionDef(_) | Stmt::ClassDef(_))
|
||||
}
|
||||
|
||||
/// Returns `true` if a [`Stmt`] is an import.
|
||||
|
|
|
@ -858,11 +858,7 @@ ForStatement: ast::Stmt = {
|
|||
.end();
|
||||
let target = Box::new(set_context(target, ast::ExprContext::Store));
|
||||
let iter = Box::new(iter);
|
||||
if is_async.is_some() {
|
||||
ast::Stmt::AsyncFor(ast::StmtAsyncFor { target, iter, body, orelse, range: (location..end_location).into() })
|
||||
} else {
|
||||
ast::Stmt::For(ast::StmtFor { target, iter, body, orelse, range: (location..end_location).into() })
|
||||
}
|
||||
ast::Stmt::For(ast::StmtFor { target, iter, body, orelse, is_async: is_async.is_some(), range: (location..end_location).into() })
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -975,11 +971,7 @@ ExceptClause: ast::ExceptHandler = {
|
|||
WithStatement: ast::Stmt = {
|
||||
<location:@L> <is_async:"async"?> "with" <items:WithItems> ":" <body:Suite> => {
|
||||
let end_location = body.last().unwrap().end();
|
||||
if is_async.is_some() {
|
||||
ast::StmtAsyncWith { items, body, range: (location..end_location).into() }.into()
|
||||
} else {
|
||||
ast::StmtWith { items, body, range: (location..end_location).into() }.into()
|
||||
}
|
||||
ast::StmtWith { items, body, is_async: is_async.is_some(), range: (location..end_location).into() }.into()
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -1014,11 +1006,7 @@ FuncDef: ast::Stmt = {
|
|||
let args = Box::new(args);
|
||||
let returns = r.map(Box::new);
|
||||
let end_location = body.last().unwrap().end();
|
||||
if is_async.is_some() {
|
||||
ast::StmtAsyncFunctionDef { name, parameters:args, body, decorator_list, returns, type_params, range: (location..end_location).into() }.into()
|
||||
} else {
|
||||
ast::StmtFunctionDef { name, parameters:args, body, decorator_list, returns, type_params, range: (location..end_location).into() }.into()
|
||||
}
|
||||
ast::StmtFunctionDef { name, parameters:args, body, decorator_list, returns, type_params, is_async: is_async.is_some(), range: (location..end_location).into() }.into()
|
||||
},
|
||||
};
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// auto-generated: "lalrpop 0.20.0"
|
||||
// sha3: f99d8cb29227bfbe1fa07719f655304a9a93fd4715726687ef40c091adbdbad5
|
||||
// sha3: d713a7771107f8c20353ce5e890fba004b3c5491f513d28e9348a49cd510c59b
|
||||
use num_bigint::BigInt;
|
||||
use ruff_text_size::TextSize;
|
||||
use ruff_python_ast::{self as ast, Ranged, MagicKind};
|
||||
|
@ -33081,11 +33081,7 @@ fn __action147<
|
|||
.end();
|
||||
let target = Box::new(set_context(target, ast::ExprContext::Store));
|
||||
let iter = Box::new(iter);
|
||||
if is_async.is_some() {
|
||||
ast::Stmt::AsyncFor(ast::StmtAsyncFor { target, iter, body, orelse, range: (location..end_location).into() })
|
||||
} else {
|
||||
ast::Stmt::For(ast::StmtFor { target, iter, body, orelse, range: (location..end_location).into() })
|
||||
}
|
||||
ast::Stmt::For(ast::StmtFor { target, iter, body, orelse, is_async: is_async.is_some(), range: (location..end_location).into() })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -33306,11 +33302,7 @@ fn __action155<
|
|||
{
|
||||
{
|
||||
let end_location = body.last().unwrap().end();
|
||||
if is_async.is_some() {
|
||||
ast::StmtAsyncWith { items, body, range: (location..end_location).into() }.into()
|
||||
} else {
|
||||
ast::StmtWith { items, body, range: (location..end_location).into() }.into()
|
||||
}
|
||||
ast::StmtWith { items, body, is_async: is_async.is_some(), range: (location..end_location).into() }.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -33407,11 +33399,7 @@ fn __action161<
|
|||
let args = Box::new(args);
|
||||
let returns = r.map(Box::new);
|
||||
let end_location = body.last().unwrap().end();
|
||||
if is_async.is_some() {
|
||||
ast::StmtAsyncFunctionDef { name, parameters:args, body, decorator_list, returns, type_params, range: (location..end_location).into() }.into()
|
||||
} else {
|
||||
ast::StmtFunctionDef { name, parameters:args, body, decorator_list, returns, type_params, range: (location..end_location).into() }.into()
|
||||
}
|
||||
ast::StmtFunctionDef { name, parameters:args, body, decorator_list, returns, type_params, is_async: is_async.is_some(), range: (location..end_location).into() }.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ expression: parse_ast
|
|||
For(
|
||||
StmtFor {
|
||||
range: 0..24,
|
||||
is_async: false,
|
||||
target: Name(
|
||||
ExprName {
|
||||
range: 4..5,
|
||||
|
|
|
@ -6,6 +6,7 @@ expression: parse_ast
|
|||
With(
|
||||
StmtWith {
|
||||
range: 0..17,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 5..11,
|
||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 0..23,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "f",
|
||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 0..29,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "f",
|
||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 0..13,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "f",
|
||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 0..13,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "f",
|
||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 0..32,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "f",
|
||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 0..38,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "f",
|
||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 0..42,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "f",
|
||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 0..52,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "f",
|
||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 0..20,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "f",
|
||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 0..26,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "f",
|
||||
|
|
|
@ -7,6 +7,7 @@ Ok(
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 0..20,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "f",
|
||||
|
|
|
@ -6,6 +6,7 @@ expression: parse_ast
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 0..34,
|
||||
is_async: false,
|
||||
decorator_list: [
|
||||
Decorator {
|
||||
range: 0..13,
|
||||
|
|
|
@ -125,6 +125,7 @@ Module(
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 566..626,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "foo",
|
||||
|
@ -199,6 +200,7 @@ Module(
|
|||
For(
|
||||
StmtFor {
|
||||
range: 701..727,
|
||||
is_async: false,
|
||||
target: Name(
|
||||
ExprName {
|
||||
range: 705..706,
|
||||
|
|
|
@ -38,6 +38,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 18..44,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "__init__",
|
||||
|
@ -78,6 +79,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 46..98,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "method_with_default",
|
||||
|
|
|
@ -6,6 +6,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 0..20,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "func",
|
||||
|
@ -53,6 +54,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 22..53,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "func",
|
||||
|
@ -132,6 +134,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 55..91,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "func",
|
||||
|
@ -219,6 +222,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 93..138,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "func",
|
||||
|
@ -321,6 +325,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 140..171,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "func",
|
||||
|
@ -393,6 +398,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 173..230,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "func",
|
||||
|
@ -496,6 +502,7 @@ expression: "parse_suite(source, \"<test>\").unwrap()"
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 232..273,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "func",
|
||||
|
|
|
@ -6,6 +6,7 @@ expression: parse_ast
|
|||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 1..49,
|
||||
is_async: false,
|
||||
decorator_list: [],
|
||||
name: Identifier {
|
||||
id: "args_to_tuple",
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
---
|
||||
source: crates/ruff_python_parser/src/parser.rs
|
||||
expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||
expression: "parse_suite(source, \"<test>\").unwrap()"
|
||||
---
|
||||
[
|
||||
With(
|
||||
StmtWith {
|
||||
range: 0..12,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 5..6,
|
||||
|
@ -33,6 +34,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 13..30,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 18..24,
|
||||
|
@ -68,6 +70,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 31..46,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 36..37,
|
||||
|
@ -108,6 +111,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 47..72,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 52..58,
|
||||
|
@ -164,6 +168,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 73..97,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 78..91,
|
||||
|
@ -214,6 +219,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 98..127,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 103..121,
|
||||
|
@ -272,6 +278,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 128..141,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 133..135,
|
||||
|
@ -297,6 +304,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 142..160,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 147..154,
|
||||
|
@ -330,6 +338,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 161..175,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 167..168,
|
||||
|
@ -357,6 +366,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 176..195,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 181..189,
|
||||
|
@ -392,6 +402,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 196..211,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 202..203,
|
||||
|
@ -419,6 +430,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 212..232,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 217..226,
|
||||
|
@ -462,6 +474,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 233..250,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 239..243,
|
||||
|
@ -502,6 +515,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 251..273,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 256..267,
|
||||
|
@ -554,6 +568,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 274..290,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 279..284,
|
||||
|
@ -593,6 +608,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 291..312,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 296..306,
|
||||
|
@ -640,6 +656,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 313..331,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 318..325,
|
||||
|
@ -688,6 +705,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 332..355,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 337..349,
|
||||
|
@ -744,6 +762,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 356..375,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 361..369,
|
||||
|
@ -783,6 +802,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 376..400,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 381..394,
|
||||
|
@ -830,6 +850,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 401..428,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 406..422,
|
||||
|
@ -898,6 +919,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 429..461,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 434..455,
|
||||
|
@ -974,6 +996,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 462..481,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 468..474,
|
||||
|
@ -1009,6 +1032,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 482..502,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 488..494,
|
||||
|
@ -1044,6 +1068,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 503..530,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 509..515,
|
||||
|
@ -1100,6 +1125,7 @@ expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
|||
With(
|
||||
StmtWith {
|
||||
range: 531..559,
|
||||
is_async: false,
|
||||
items: [
|
||||
WithItem {
|
||||
range: 537..543,
|
||||
|
|
|
@ -178,8 +178,7 @@ impl ModuleSource<'_> {
|
|||
|
||||
pub(crate) fn function_visibility(stmt: &Stmt) -> Visibility {
|
||||
match stmt {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. })
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { name, .. }) => {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. }) => {
|
||||
if name.starts_with('_') {
|
||||
Visibility::Private
|
||||
} else {
|
||||
|
@ -191,52 +190,45 @@ pub(crate) fn function_visibility(stmt: &Stmt) -> Visibility {
|
|||
}
|
||||
|
||||
pub(crate) fn method_visibility(stmt: &Stmt) -> Visibility {
|
||||
match stmt {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
name,
|
||||
decorator_list,
|
||||
..
|
||||
let Stmt::FunctionDef(ast::StmtFunctionDef {
|
||||
name,
|
||||
decorator_list,
|
||||
..
|
||||
}) = stmt
|
||||
else {
|
||||
panic!("Found non-FunctionDef in method_visibility")
|
||||
};
|
||||
|
||||
// Is this a setter or deleter?
|
||||
if decorator_list.iter().any(|decorator| {
|
||||
collect_call_path(&decorator.expression).is_some_and(|call_path| {
|
||||
call_path.as_slice() == [name, "setter"] || call_path.as_slice() == [name, "deleter"]
|
||||
})
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
name,
|
||||
decorator_list,
|
||||
..
|
||||
}) => {
|
||||
// Is this a setter or deleter?
|
||||
if decorator_list.iter().any(|decorator| {
|
||||
collect_call_path(&decorator.expression).is_some_and(|call_path| {
|
||||
call_path.as_slice() == [name, "setter"]
|
||||
|| call_path.as_slice() == [name, "deleter"]
|
||||
})
|
||||
}) {
|
||||
return Visibility::Private;
|
||||
}
|
||||
|
||||
// Is the method non-private?
|
||||
if !name.starts_with('_') {
|
||||
return Visibility::Public;
|
||||
}
|
||||
|
||||
// Is this a magic method?
|
||||
if name.starts_with("__") && name.ends_with("__") {
|
||||
return Visibility::Public;
|
||||
}
|
||||
|
||||
Visibility::Private
|
||||
}
|
||||
_ => panic!("Found non-FunctionDef in method_visibility"),
|
||||
}) {
|
||||
return Visibility::Private;
|
||||
}
|
||||
|
||||
// Is the method non-private?
|
||||
if !name.starts_with('_') {
|
||||
return Visibility::Public;
|
||||
}
|
||||
|
||||
// Is this a magic method?
|
||||
if name.starts_with("__") && name.ends_with("__") {
|
||||
return Visibility::Public;
|
||||
}
|
||||
|
||||
Visibility::Private
|
||||
}
|
||||
|
||||
pub(crate) fn class_visibility(stmt: &Stmt) -> Visibility {
|
||||
match stmt {
|
||||
Stmt::ClassDef(ast::StmtClassDef { name, .. }) => {
|
||||
if name.starts_with('_') {
|
||||
Visibility::Private
|
||||
} else {
|
||||
Visibility::Public
|
||||
}
|
||||
}
|
||||
_ => panic!("Found non-ClassDef in function_visibility"),
|
||||
let Stmt::ClassDef(ast::StmtClassDef { name, .. }) = stmt else {
|
||||
panic!("Found non-ClassDef in class_visibility")
|
||||
};
|
||||
|
||||
if name.starts_with('_') {
|
||||
Visibility::Private
|
||||
} else {
|
||||
Visibility::Public
|
||||
}
|
||||
}
|
||||
|
|
|
@ -84,7 +84,6 @@ impl<'a> Member<'a> {
|
|||
pub fn name(&self) -> Option<&'a str> {
|
||||
match &self.stmt {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. })
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { name, .. })
|
||||
| Stmt::ClassDef(ast::StmtClassDef { name, .. }) => Some(name),
|
||||
_ => None,
|
||||
}
|
||||
|
|
|
@ -80,7 +80,7 @@ impl<'a> StatementVisitor<'a> for GlobalsVisitor<'a> {
|
|||
self.0.insert(name.as_str(), *range);
|
||||
}
|
||||
}
|
||||
Stmt::FunctionDef(_) | Stmt::AsyncFunctionDef(_) | Stmt::ClassDef(_) => {
|
||||
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => {
|
||||
// Don't recurse.
|
||||
}
|
||||
_ => walk_stmt(self, stmt),
|
||||
|
|
|
@ -478,7 +478,7 @@ impl<'a> SemanticModel<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
seen_function |= scope.kind.is_any_function();
|
||||
seen_function |= scope.kind.is_function();
|
||||
import_starred = import_starred || scope.uses_star_imports();
|
||||
}
|
||||
|
||||
|
@ -540,7 +540,7 @@ impl<'a> SemanticModel<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
seen_function |= scope.kind.is_any_function();
|
||||
seen_function |= scope.kind.is_function();
|
||||
}
|
||||
|
||||
None
|
||||
|
@ -1015,11 +1015,8 @@ impl<'a> SemanticModel<'a> {
|
|||
/// Return `true` if the model is in an async context.
|
||||
pub fn in_async_context(&self) -> bool {
|
||||
for scope in self.current_scopes() {
|
||||
if scope.kind.is_async_function() {
|
||||
return true;
|
||||
}
|
||||
if scope.kind.is_function() {
|
||||
return false;
|
||||
if let ScopeKind::Function(ast::StmtFunctionDef { is_async, .. }) = scope.kind {
|
||||
return *is_async;
|
||||
}
|
||||
}
|
||||
false
|
||||
|
|
|
@ -178,19 +178,12 @@ bitflags! {
|
|||
pub enum ScopeKind<'a> {
|
||||
Class(&'a ast::StmtClassDef),
|
||||
Function(&'a ast::StmtFunctionDef),
|
||||
AsyncFunction(&'a ast::StmtAsyncFunctionDef),
|
||||
Generator,
|
||||
Module,
|
||||
Type,
|
||||
Lambda(&'a ast::ExprLambda),
|
||||
}
|
||||
|
||||
impl ScopeKind<'_> {
|
||||
pub const fn is_any_function(&self) -> bool {
|
||||
matches!(self, ScopeKind::Function(_) | ScopeKind::AsyncFunction(_))
|
||||
}
|
||||
}
|
||||
|
||||
/// Id uniquely identifying a scope in a program.
|
||||
///
|
||||
/// Using a `u32` is sufficient because Ruff only supports parsing documents with a size of max `u32::max`
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue