Return NothingChanged if non-Python cell magic is detected, to avoid tokenize error#2630
Conversation
Let `validate_cell()` also catch non-Python cell magics, as the bodies of these might contain invalid indentation (such as `%%writefile`) and this could cause TransformerManager to break.
this should be a valid arbitrary writefile, but before it failed via the tokenizer.
MarcoGorelli
left a comment
There was a problem hiding this comment.
Thanks @danielsparing for this PR!
I'm not a black maintainer, but I did make the Jupyter PR, so am leaving a couple of comments:
1 - does this mean that lines
black/src/black/handle_ipynb_magics.py
Lines 233 to 234 in 9a73bb8
can be removed?
- Perhaps this should be done the other way round? I.e. rather than checking if the magic name is in a blocklist, and skipping the cell if that's the case,
blackshould check if the magic name is in an allow list, and only format the cell if it is. Thoughts on this?
and remove later unnecessary check
as we are now allowlisting python cell magics and %t would be a user-defined custom one
|
@MarcoGorelli Thanks for the quick review! I agree that the allowlist is cleaner. This also necessitates something I should have done earlier, to exactly check for the first word in the cell for the cell magic. This does mean that I needed to remove the test with I also removed the lines you mentioned. What do you think? |
MarcoGorelli
left a comment
There was a problem hiding this comment.
Thanks for updating @danielsparing !
Generally looks good to me, tagging @JelleZijlstra and @ichard26 (who reviewed the original PR extensively) for further comments
|
|
||
| ### _Black_ | ||
|
|
||
| - Fixed non-Python cell magics sometimes failing due to indentation (#2630) |
There was a problem hiding this comment.
I think this can be made more explicit - can you make it clearer what the behaviour was before, and what it is now, so it's clear that black is now intentionally more timid about processing cell magics?
MarcoGorelli
left a comment
There was a problem hiding this comment.
Awesome - just got a minor comment left, will handover to the black maintainers now
as `Black` does not support Python2 anymore
Co-authored-by: Marco Edward Gorelli <marcogorelli@protonmail.com>
|
Thank you so much for your contribution! This project is only possible by contributions like these 🖤. Seriously, the core team barely has any experience with Jupyter notebooks so we rely on third-party contributions to keep this feature of Black alive and well. You're awesome, @danielsparing - congrats on your first PR to psf/black 🎉 I'm also going to take this moment to thank the defacto core dev on black's jupyter support: @MarcoGorelli! Y'all are great :) |
Description
Fixes #2627 , a non-Python cell magic such as
%%writelinecan legitimately contain "incorrect" indentation, however this causestokenize-rtto return an error. To avoid this,validate_cellshould early detect cell magics (just like it detectsTransformerManagertransformations).Test added too, in the shape of a "badly indented"
%%writefilewithintest_non_python_magics.Checklist - did you ...