Revert "[ty] Fix completion in decorator without class or function definition" (#22176)

This commit is contained in:
Micha Reiser
2025-12-24 17:27:21 +01:00
committed by GitHub
parent adef89eb7c
commit eef403f6cf
6 changed files with 22 additions and 165 deletions

View File

@@ -2914,27 +2914,21 @@ impl<'src> Parser<'src> {
self.current_token_range(),
);
let range = self.node_range(start);
ast::StmtFunctionDef {
node_index: Default::default(),
range,
is_async: false,
decorator_list: decorators,
name: ast::Identifier {
id: Name::empty(),
range: self.missing_node_range(),
node_index: AtomicNodeIndex::NONE,
},
type_params: None,
parameters: Box::new(ast::Parameters {
range: self.missing_node_range(),
..ast::Parameters::default()
}),
returns: None,
body: vec![],
}
.into()
// TODO(dhruvmanila): It seems that this recovery drops all the parsed
// decorators. Maybe we could convert them into statement expression
// with a flag indicating that this expression is part of a decorator.
// It's only possible to keep them if it's a function or class definition.
// We could possibly keep them if there's indentation error:
//
// ```python
// @decorator
// @decorator
// def foo(): ...
// ```
//
// Or, parse it as a binary expression where the left side is missing.
// We would need to convert each decorator into a binary expression.
self.parse_statement()
}
}
}

View File

@@ -1,5 +1,6 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/decorator_missing_expression.py
---
## AST
@@ -9,44 +10,6 @@ Module(
node_index: NodeIndex(None),
range: 0..51,
body: [
FunctionDef(
StmtFunctionDef {
node_index: NodeIndex(None),
range: 0..4,
is_async: false,
decorator_list: [
Decorator {
range: 0..4,
node_index: NodeIndex(None),
expression: Name(
ExprName {
node_index: NodeIndex(None),
range: 1..4,
id: Name("def"),
ctx: Load,
},
),
},
],
name: Identifier {
id: Name(""),
range: 4..4,
node_index: NodeIndex(None),
},
type_params: None,
parameters: Parameters {
range: 4..4,
node_index: NodeIndex(None),
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [],
},
),
AnnAssign(
StmtAnnAssign {
node_index: NodeIndex(None),

View File

@@ -1,5 +1,6 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/decorator_unexpected_token.py
---
## AST
@@ -9,44 +10,6 @@ Module(
node_index: NodeIndex(None),
range: 0..34,
body: [
FunctionDef(
StmtFunctionDef {
node_index: NodeIndex(None),
range: 0..4,
is_async: false,
decorator_list: [
Decorator {
range: 0..4,
node_index: NodeIndex(None),
expression: Name(
ExprName {
node_index: NodeIndex(None),
range: 1..4,
id: Name("foo"),
ctx: Load,
},
),
},
],
name: Identifier {
id: Name(""),
range: 4..4,
node_index: NodeIndex(None),
},
type_params: None,
parameters: Parameters {
range: 4..4,
node_index: NodeIndex(None),
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [],
},
),
With(
StmtWith {
node_index: NodeIndex(None),
@@ -83,44 +46,6 @@ Module(
],
},
),
FunctionDef(
StmtFunctionDef {
node_index: NodeIndex(None),
range: 23..27,
is_async: false,
decorator_list: [
Decorator {
range: 23..27,
node_index: NodeIndex(None),
expression: Name(
ExprName {
node_index: NodeIndex(None),
range: 24..27,
id: Name("foo"),
ctx: Load,
},
),
},
],
name: Identifier {
id: Name(""),
range: 27..27,
node_index: NodeIndex(None),
},
type_params: None,
parameters: Parameters {
range: 27..27,
node_index: NodeIndex(None),
posonlyargs: [],
args: [],
vararg: None,
kwonlyargs: [],
kwarg: None,
},
returns: None,
body: [],
},
),
Assign(
StmtAssign {
node_index: NodeIndex(None),