Skip to content
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

Return NothingChanged if non-Python cell magic is detected, to avoid tokenize error #2630

Merged
merged 13 commits into from Nov 29, 2021
6 changes: 6 additions & 0 deletions CHANGES.md
@@ -1,5 +1,11 @@
# Change Log

## Unreleased

### _Black_

- Fixed non-Python cell magics sometimes failing due to indentation (#2630)
Copy link
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed in 1e8a0b1


## 21.11b1

### _Black_
Expand Down
1 change: 1 addition & 0 deletions docs/faq.md
Expand Up @@ -47,6 +47,7 @@ _Black_ is timid about formatting Jupyter Notebooks. Cells containing any of the
following will not be formatted:

- automagics (e.g. `pip install black`)
- non-Python cell magics (e.g. `%%writeline`)
- multiline magics, e.g.:

```python
Expand Down
7 changes: 6 additions & 1 deletion src/black/__init__.py
Expand Up @@ -57,6 +57,7 @@
remove_trailing_semicolon,
put_trailing_semicolon_back,
TRANSFORMED_MAGICS,
NON_PYTHON_CELL_MAGICS,
jupyter_dependencies_are_installed,
)

Expand Down Expand Up @@ -943,7 +944,9 @@ def format_file_contents(src_contents: str, *, fast: bool, mode: Mode) -> FileCo


def validate_cell(src: str) -> None:
"""Check that cell does not already contain TransformerManager transformations.
"""Check that cell does not already contain TransformerManager transformations,
or non-Python cell magics, which might cause tokenizer_rt to break because of
indentations.

If a cell contains ``!ls``, then it'll be transformed to
``get_ipython().system('ls')``. However, if the cell originally contained
Expand All @@ -959,6 +962,8 @@ def validate_cell(src: str) -> None:
"""
if any(transformed_magic in src for transformed_magic in TRANSFORMED_MAGICS):
raise NothingChanged
if any("%%" + cell_magic in src for cell_magic in NON_PYTHON_CELL_MAGICS):
raise NothingChanged


def format_cell(src: str, *, fast: bool, mode: Mode) -> str:
Expand Down
1 change: 1 addition & 0 deletions tests/test_ipynb.py
Expand Up @@ -102,6 +102,7 @@ def test_magic(src: str, expected: str) -> None:
(
"%%bash\n2+2",
"%%html --isolated\n2+2",
"%%writefile e.txt\n meh\n meh",
),
)
def test_non_python_magics(src: str) -> None:
Expand Down