You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've implemented a special syntax error in the case that an elif follows an else. See here.
ifi%3==0:
print("divisible by 3")
else:
print("not divisible by 3")
elifi%2==0:
print("divisible by 2")
# SyntaxError: elif not allowed after else
This is currently the extent of the new behavior. If possible, I would be interested to implement behavior like this:
ifisinstance(i, int):
ifi%3==0:
print("divisible by 3")
else:
print("not divisible by 3")
elifisinstance(i, str):
print("i is a string")
# SyntaxError: elif not allowed after else. Perhaps the elif is at the wrong indentation level?
This would be even more informative for beginners, but I'm not sure if the parser supports that level of state. In particular, we wouldn't want the latter syntax error (the one that suggests that the indentation level is wrong) to be raised if the invalid elif were within a for block.
I like the new error message! But, looks like Perhaps the elif is at the wrong indentation level? part would be really hard to generate, if even possible.
Please, send a PR with the first message for now :)
I just spoke to @pablogsal about the stateful behaviour, and he confirmed that it's not possible; the behavior that is possible is already merged, so we can close this.
Activity
sobolevn commentedon Feb 9, 2025
I like the new error message! But, looks like
Perhaps the elif is at the wrong indentation level?part would be really hard to generate, if even possible.Please, send a PR with the first message for now :)
gh-129858: Special syntax error for `elif` block after `else` (#129902)
StanFromIreland commentedon May 10, 2025
The first part was merged, is it time to close this?
swfarnsworth commentedon May 15, 2025
I just spoke to @pablogsal about the stateful behaviour, and he confirmed that it's not possible; the behavior that is possible is already merged, so we can close this.