-
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
formatter(Jupyter): ending semicolons valid in Jupyter #8254
Comments
Thanks! |
Re line magic, it's solved #8204, sorry for the churn! |
Great! Probably should have checked main. So just simicolons left. |
Related: #7300 |
Is my assumption correct that this only applies to expression statements or are there other statements for which semicolons need to be preserved? |
IPython has several modes. If you set:
Then either IIRC |
Note, there's now a helper function to extract the trailing semicolon. We can use it in the assignment (including augmented and annotated) and expression statement formatting ruff/crates/ruff_python_formatter/src/statement/mod.rs Lines 89 to 107 in 2c84f91
|
Black's implementation for reference: https://github.com/psf/black/blob/c54c213d6a3132986feede0cf0525f5bae5b43d6/src/black/handle_ipynb_magics.py#L73-L102. It seems to be removing them for all cases. |
The function after that puts them back - not sure why you'd need to remove it and then put it back, since the normal formatter also removes them - just checking to see if it's there and then putting it back seems like it would also work. Anyway, what cases are not covered by assignments and expressions? Blocks can't end lines, for example. Simicolons aren't needed for import statements - I wonder if black keeps them, actually. :) |
Nice find. Removing them and adding them back sounds complicated. We can just preserve them 😄 |
## Summary This PR updates the formatter to preserve trailing semicolon for Jupyter Notebooks. The motivation behind the change is that semicolons in notebooks are typically used to hide the output, for example when plotting. This is highlighted in the linked issue. The conditions required as to when the trailing semicolon should be preserved are: 1. It should be a top-level statement which is last in the module. 2. For statement, it can be either assignment, annotated assignment, or augmented assignment. Here, the target should only be a single identifier i.e., multiple assignments or tuple unpacking isn't considered. 3. For expression, it can be any. ## Test Plan Add a new integration test in `ruff_cli`. The test notebook basically acts as a document as to which trailing semicolons are to be preserved. fixes: #8254
See scientific-python/cookie#299. Waiting on astral-sh/ruff#8254. --------- Signed-off-by: Henry Schreiner <[email protected]>
The Jupyter formatter has a couple of issues when running on notebooks.
One is it removes semicolons at the end of lines.
black[jupyter]
does not remove semicolons at the end of lines when running on a notebook, since it's used to mark output as hidden, for example when plotting.plt.plot();
will show the plot but not print out the repr for the Artists that the plot method returns.Edit: On the last line of a cell. Related: #7300 which is for the linter. But that's rule based, so it's easy to turn it off for
*.ipynb
files, while there isn't a workaround for the formatter, making it a blocker.(Fixed in main) The other is that it can't read a notebook that black and ruff-lint are able to read. The error is:
I'm guessing it's choking on a line magic, perhaps? The file does have this line:
Which is the only
%
in the file.The text was updated successfully, but these errors were encountered: