-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[syntax-errors] Improve error message and range for pre-PEP-614 decorator syntax errors #16581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# parse_options: { "target-version": "3.8" } | ||
async def foo(): | ||
@await bar | ||
def baz(): ... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# parse_options: { "target-version": "3.8" } | ||
@{3: 3} | ||
def bar(): ... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# parse_options: { "target-version": "3.8" } | ||
@3.14 | ||
def bar(): ... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# parse_options: { "target-version": "3.9" } | ||
async def foo(): | ||
@await bar | ||
def baz(): ... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
--- | ||
source: crates/ruff_python_parser/tests/fixtures.rs | ||
input_file: crates/ruff_python_parser/resources/inline/err/decorator_await_expression_py38.py | ||
--- | ||
## AST | ||
|
||
``` | ||
Module( | ||
ModModule { | ||
range: 0..96, | ||
body: [ | ||
FunctionDef( | ||
StmtFunctionDef { | ||
range: 45..95, | ||
is_async: true, | ||
decorator_list: [], | ||
name: Identifier { | ||
id: Name("foo"), | ||
range: 55..58, | ||
}, | ||
type_params: None, | ||
parameters: Parameters { | ||
range: 58..60, | ||
posonlyargs: [], | ||
args: [], | ||
vararg: None, | ||
kwonlyargs: [], | ||
kwarg: None, | ||
}, | ||
returns: None, | ||
body: [ | ||
FunctionDef( | ||
StmtFunctionDef { | ||
range: 66..95, | ||
is_async: false, | ||
decorator_list: [ | ||
Decorator { | ||
range: 66..76, | ||
expression: Await( | ||
ExprAwait { | ||
range: 67..76, | ||
value: Name( | ||
ExprName { | ||
range: 73..76, | ||
id: Name("bar"), | ||
ctx: Load, | ||
}, | ||
), | ||
}, | ||
), | ||
}, | ||
], | ||
name: Identifier { | ||
id: Name("baz"), | ||
range: 85..88, | ||
}, | ||
type_params: None, | ||
parameters: Parameters { | ||
range: 88..90, | ||
posonlyargs: [], | ||
args: [], | ||
vararg: None, | ||
kwonlyargs: [], | ||
kwarg: None, | ||
}, | ||
returns: None, | ||
body: [ | ||
Expr( | ||
StmtExpr { | ||
range: 92..95, | ||
value: EllipsisLiteral( | ||
ExprEllipsisLiteral { | ||
range: 92..95, | ||
}, | ||
), | ||
}, | ||
), | ||
], | ||
}, | ||
), | ||
], | ||
}, | ||
), | ||
], | ||
}, | ||
) | ||
``` | ||
## Unsupported Syntax Errors | ||
|
||
| | ||
1 | # parse_options: { "target-version": "3.8" } | ||
2 | async def foo(): | ||
3 | @await bar | ||
| ^^^^^^^^^ Syntax Error: Cannot use `await` expression outside function-call arguments in a decorator on Python 3.8 (syntax was added in Python 3.9) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually find the "outside function-call arguments in a decorator" part a bit confusing because it made me think that this is only allowed in function-call arguments. I'd possibly remove it and keep it simply "Cannot use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see. So you interpreted "outside function-call arguments" as meaning "If I add parentheses, it will be valid syntax"? Hmm, I'm not sure how to clarify that :/ adding the parentheses doesn't make it a function call -- but I can see how a beginner might find the language confusing. Would you find "outside call expression arguments" any clearer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the late reply, what I thought it could be interpreted is related to the function that's being decorated and not the decorator itself. But, I think what you've is fine as well. We can iterate if anyone finds it confusing. |
||
4 | def baz(): ... | ||
| |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
--- | ||
source: crates/ruff_python_parser/tests/fixtures.rs | ||
input_file: crates/ruff_python_parser/resources/inline/err/decorator_dict_literal_py38.py | ||
--- | ||
## AST | ||
|
||
``` | ||
Module( | ||
ModModule { | ||
range: 0..68, | ||
body: [ | ||
FunctionDef( | ||
StmtFunctionDef { | ||
range: 45..67, | ||
is_async: false, | ||
decorator_list: [ | ||
Decorator { | ||
range: 45..52, | ||
expression: Dict( | ||
ExprDict { | ||
range: 46..52, | ||
items: [ | ||
DictItem { | ||
key: Some( | ||
NumberLiteral( | ||
ExprNumberLiteral { | ||
range: 47..48, | ||
value: Int( | ||
3, | ||
), | ||
}, | ||
), | ||
), | ||
value: NumberLiteral( | ||
ExprNumberLiteral { | ||
range: 50..51, | ||
value: Int( | ||
3, | ||
), | ||
}, | ||
), | ||
}, | ||
], | ||
}, | ||
), | ||
}, | ||
], | ||
name: Identifier { | ||
id: Name("bar"), | ||
range: 57..60, | ||
}, | ||
type_params: None, | ||
parameters: Parameters { | ||
range: 60..62, | ||
posonlyargs: [], | ||
args: [], | ||
vararg: None, | ||
kwonlyargs: [], | ||
kwarg: None, | ||
}, | ||
returns: None, | ||
body: [ | ||
Expr( | ||
StmtExpr { | ||
range: 64..67, | ||
value: EllipsisLiteral( | ||
ExprEllipsisLiteral { | ||
range: 64..67, | ||
}, | ||
), | ||
}, | ||
), | ||
], | ||
}, | ||
), | ||
], | ||
}, | ||
) | ||
``` | ||
## Unsupported Syntax Errors | ||
|
||
| | ||
1 | # parse_options: { "target-version": "3.8" } | ||
2 | @{3: 3} | ||
| ^^^^^^ Syntax Error: Cannot use a dict literal outside function-call arguments in a decorator on Python 3.8 (syntax was added in Python 3.9) | ||
3 | def bar(): ... | ||
| |
Uh oh!
There was an error while loading. Please reload this page.