Skip to content

Conversation

@swfarnsworth
Copy link
Contributor

@swfarnsworth swfarnsworth commented Feb 9, 2025

Previously, having an elif block after an else block would raise a standard syntax error. This PR implements a special syntax error saying "elif not allowed after else".

I compiled cpython with this version of the parser and confirmed that it doesn't conflict with other special syntax or indentation errors.

Previously, having an elif block after an else block would raise a standard syntax error.
@swfarnsworth
Copy link
Contributor Author

@pablogsal I see that you've been tagged for review. Please see the associated issue for discussion about whether an even more sophisticated error message is possible.

@picnixz
Copy link
Member

picnixz commented Feb 9, 2025

I compiled cpython with this version of the parser and confirmed that it doesn't conflict with other special syntax or indentation errors.

Please add a test. There should be tests in some test_syntax.py file or test_parser.py files (I don't remember where we put them). Just Ctrl+F some error message to find the file.

@swfarnsworth
Copy link
Contributor Author

@picnixz No problem--will do.

@swfarnsworth
Copy link
Contributor Author

I've implemented a test as requested, though I'll hold off on committing or pushing it until we decide on the exact wording of the error message.

@ghost
Copy link

ghost commented Feb 11, 2025

All commit authors signed the Contributor License Agreement.
CLA signed

@swfarnsworth
Copy link
Contributor Author

Those last two commits should appear as being from this GitHub account. Let me see if I can delete them and try again.

@picnixz
Copy link
Member

picnixz commented Feb 11, 2025

In general, we avoid force-pushing, but you can do it in this case and amend the commit author and put the correct email address. (Not sure if it could help in this though)

@swfarnsworth
Copy link
Contributor Author

The force push seems to have fixed the problem without any issues.

The usernames for my work and personal email differ by one letter, which is occasionally inconvenient.

@picnixz
Copy link
Member

picnixz commented Feb 12, 2025

  • For the detection issue, you should just merge main into that branch with the Update branch button!
  • A NEWS entry and a What's New entry could be added in order to indicate that we have an improved message now. I don't have a good wording here though so maybe @hugovk may have some suggestion?

@swfarnsworth
Copy link
Contributor Author

We can draw inspiration from here, I suppose?

@swfarnsworth
Copy link
Contributor Author

What about this? I'm not sure if the blocks need something more realistic than pass.

elif statements that follow an else block now have a specific error message.

>>> if word.startswith('a'):
...     pass
... else:
...     pass
... elif word.startswith('b'):
...     pass
File "<stdin>", line 5
  elif word.startswith('b'):
  ^^^^
SyntaxError: 'elif' block follows an 'else' block

@picnixz
Copy link
Member

picnixz commented Feb 12, 2025

We can draw inspiration from here, I suppose?

Yes that's what I had in mind.

@picnixz
Copy link
Member

picnixz commented Feb 12, 2025

Another possibility is:

>>> if who == "me":
...     print("This is me!")
... else:
...     print("This is not me!")
... elif who is None:
...     print("Who is it?")
File "<stdin>", line 5
  elif who is None:
  ^^^^
SyntaxError: 'elif' block follows an 'else' block

@swfarnsworth
Copy link
Contributor Author

I like that, but I think I'll do "It's me!" and "It's not me!" in honor of Elphaba,

@picnixz
Copy link
Member

picnixz commented Feb 12, 2025

I like that, but I think I'll do "It's me!" and "It's not me!" in honor of Elphaba,

Let's go for this one :)

@swfarnsworth
Copy link
Contributor Author

We also get a subtle homage to Britney :)

Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

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

I've updated the PR from main which will fix the unrelated "computed changes" error.

swfarnsworth and others added 3 commits February 14, 2025 10:48