Repository navigation
Conversation
Black masks the magics in a notebook cell by running IPython's TransformerManager and turning the get_ipython() calls back into magics. IPython also rewrites syntax that this can't put back as written: `df.head?` and `?df.head` become the same call, `str.*find*?` becomes a `psearch` call, `>>>` and `In [1]:` prompts are removed and the autocall escapes are turned into calls. Black then wrote `?df.head`, `%psearch str.*find*` or the code without its prompts into the cell. Check that putting the masked magics back gives the original cell, ignoring whitespace that IPython strips anyway, and leave the cell unchanged otherwise. This also covers the multi-line magics that the old line count check caught. `a = b??` is left alone again, as it was before IPython 8.3, so its test no longer depends on the IPython version.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
To format a notebook cell with magics in it, Black runs IPython's
TransformerManageron the cell, masks theget_ipython()calls it produces and turns them back into magics afterwards. IPython also rewrites some syntax that can't be turned back into what was written, and Black then put IPython's version into the cell:pd.read_csv?/df.head??, became?pd.read_csv/??df.head(and%pinfo objbecame?obj)str.*find*?became%psearch str.*find*>>>orIn [1]:session lost its prompts/f aand,f a bbecamef(a)andf("a", "b")Reproduction on main (
9485ad4), same with 26.10.1:IPython runs both versions the same way, but Black shouldn't rewrite what the user typed, and
len?orstr.*find*?cells are common in teaching notebooks. With this change the file is left unchanged.mask_cellnow checks that putting the masked magics back gives the original cell, ignoring indentation and surrounding whitespace (IPython strips those anyway), and raisesNothingChangedif it doesn't, like Black already does for multi-line magics. This replaces the old line count check, which only caught multi-line magics. Help before the object (?obj), other line and cell magics and indented cells are formatted as before.test_set_inputexpecteda = b??to become??b, which drops thea =. That expectation was set in #3008 when IPython 8.3 changed how it transforms the line; before that, the cell was left alone. It's left alone again now on every IPython version, so the version skip and theIPythonimport it needed are removed.Tests:
test_rewritten_ipython_syntax_noopcovers help and source after the object, a wildcard search, both prompt styles and the two autocall escapes. It fails on main.tox -e pypasses (639 passed, 1 skipped, plus 96 jupyter tests), and so dotox -e run_selfandpre-commit run -a. The notebook tests also pass with IPython 8.2.0. Formatting the 2,176 code cells of 218 notebooks from other projects gives identical output on main and on this branch.Checklist - did you ...
--previewstyle, following the stability policy? (No style change, Black now leaves these cells alone.)CHANGES.mdif necessary?