Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/ruff_python_ast/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4390,6 +4390,16 @@ impl AnyNodeRef<'_> {
| AnyNodeRef::StmtTryStar(_)
)
}

/// In our AST, only some alternative branches are represented as a node. This has historical
/// reasons, e.g. we added a node for elif/else in if statements which was not originally
/// present in the parser.
pub const fn is_alternative_branch_with_node(self) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the meaning of the with_node suffix? Should the function also return true for ElifElseClauses?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does!

matches!(
self,
AnyNodeRef::ExceptHandlerExceptHandler(_) | AnyNodeRef::ElifElseClause(_)
)
}
}

impl<'a> From<&'a ast::ModModule> for AnyNodeRef<'a> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,9 @@
a.askjdfahdlskjflsajfadhsaf.akjdsf.aksjdlfadhaljsashdfljaf.askjdflhasfdlashdlfaskjfd.asdkjfksahdfkjafs
)

x6 = (
# Check assumption with enclosing nodes
a.b
)


Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# Leading starred comment
* # Trailing star comment
[
# Leading value commnt
# Leading value comment
[What, i, this, s, very, long, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]
] # trailing value comment
)

call(
# Leading starred comment
* ( # Leading value commnt
* ( # Leading value comment
[What, i, this, s, very, long, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]
) # trailing value comment
)
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,16 @@ def f():
pass

# comment

if True:
def f2():
pass
# 1
else:
def f2():
pass
# 2

if True: print("a") # 1
elif True: print("b") # 2
else: print("c") # 3
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,23 @@
print(1) # issue7208
except A:
pass

try:
f() # end-of-line last comment
except RuntimeError:
raise

try:
def f2():
pass
# a
except:
def f2():
pass
# b

try: pass # a
except ZeroDivisionError: pass # b
except: pass # b
else: pass # d
finally: pass # c
Loading