-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Introduce SuiteStatement
#5326
Comments
@konstin Sorry for the misunderstanding. I'm excited that you want to take this on! These are the Index: ast/Python.asdl
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/ast/Python.asdl b/ast/Python.asdl
--- a/ast/Python.asdl (revision caf6ebcefa80ce835aa689f392000e5cfd18f93d)
+++ b/ast/Python.asdl (date 1687530270508)
@@ -9,16 +9,16 @@
| FunctionType(expr* argtypes, expr returns)
stmt = FunctionDef(identifier name, arguments args,
- stmt* body, decorator* decorator_list, expr? returns,
+ stmt body, decorator* decorator_list, expr? returns,
string? type_comment)
| AsyncFunctionDef(identifier name, arguments args,
- stmt* body, decorator* decorator_list, expr? returns,
+ stmt body, decorator* decorator_list, expr? returns,
string? type_comment)
| ClassDef(identifier name,
expr* bases,
keyword* keywords,
- stmt* body,
+ stmt body,
decorator* decorator_list)
| Return(expr? value)
@@ -29,18 +29,18 @@
| AnnAssign(expr target, expr annotation, expr? value, int simple)
-- use 'orelse' because else is a keyword in target languages
- | For(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)
- | AsyncFor(expr target, expr iter, stmt* body, stmt* orelse, string? type_comment)
- | While(expr test, stmt* body, stmt* orelse)
- | If(expr test, stmt* body, stmt* orelse)
- | With(withitem* items, stmt* body, string? type_comment)
- | AsyncWith(withitem* items, stmt* body, string? type_comment)
+ | For(expr target, expr iter, stmt body, stmt? orelse, string? type_comment)
+ | AsyncFor(expr target, expr iter, stmt body, stmt? orelse, string? type_comment)
+ | While(expr test, stmt body, stmt? orelse)
+ | If(expr test, stmt body, stmt? orelse)
+ | With(withitem* items, stmt body, string? type_comment)
+ | AsyncWith(withitem* items, stmt body, string? type_comment)
| Match(expr subject, match_case* cases)
| Raise(expr? exc, expr? cause)
- | Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody)
- | TryStar(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody)
+ | Try(stmt body, excepthandler* handlers, stmt? orelse, stmt? finalbody)
+ | TryStar(stmt body, excepthandler* handlers, stmt? orelse, stmt? finalbody)
| Assert(expr test, expr? msg)
| Import(alias* names)
@@ -50,6 +50,7 @@
| Nonlocal(identifier* names)
| Expr(expr value)
| Pass | Break | Continue
+ | Suite(stmt* statements)
-- col_offset is the byte offset in the utf8 string the parser uses
attributes (int lineno, int col_offset, int? end_lineno, int? end_col_offset)
@@ -106,7 +107,7 @@
comprehension = (expr target, expr iter, expr* ifs, int is_async)
- excepthandler = ExceptHandler(expr? type, identifier? name, stmt* body)
+ excepthandler = ExceptHandler(expr? type, identifier? name, stmt body)
attributes (int lineno, int col_offset, int? end_lineno, int? end_col_offset)
arguments = (arg* posonlyargs, arg* args, arg? vararg, arg* kwonlyargs,
@@ -125,7 +126,7 @@
withitem = (expr context_expr, expr? optional_vars)
- match_case = (pattern pattern, expr? guard, stmt* body)
+ match_case = (pattern pattern, expr? guard, stmt body)
pattern = MatchValue(expr value)
| MatchSingleton(constant value)
You may want to base your work on top of astral-sh/RustPython-Parser#15 |
This would be nice to have but isn't something that we need to ship the formatter. Closing for now |
I'd still like to have this, but as a general not-linter-limited improvement that could remove so many |
I'm now tracking this in #6183 (comment) |
RustPython's AST structure for the following two programs is identical (the ranges differ)
This is problematic because the formatter incorrectly assumes that the nested
if
in the second program is anelif
and collapses the nested if. This isn't something a formatter should do. This representation does make sense for an interpreter. It's actually a neat little optimisation that the parser performs.The solution for this is to change
Suite
from aVec<Statement>
to its ownSuiteStatement
AST node and change theorelse
type (and any other field that represents a body) fromSuite
toOption<Stmt>
.This would change the representation of the first program to:
and of the second program:
which is unambiguous
This requires changes to:
Alternatives
unparse
of the linterThe text was updated successfully, but these errors were encountered: