You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create the minimal jupyter notebook, which will contain one cell with the function ands a comment, containing question mark;
def example_fn(param):
"""
param: str what to print?
"""
print(param)
Close the file and run
black-nb Repro.ipynb
The error will be shown:
error: cannot format /path/to/nb/Repro.ipynb: INTERNAL ERROR: Black produced code that is not equivalent to the source on pass 1. Please report a bug on https://github.com/psf/black/issues. This diff might be helpful: ${TEMP}/blk_y4fjze4u.log
The diff will show that at the second pass extra whitespaces were added:
--- src
+++ dst
@@ -23,11 +23,11 @@
body=
Expr(
value=
Constant(
value=
- '###MAGIC### param: str what to print?', # str
+ '###MAGIC### param: str what to print?', # str
) # /Constant
) # /Expr
Expr(
value=
Call(
If the question mark will be removed, the notebook will pass
contains_magic makes no consideration for whether or not it's looking at a string. This explains why the issue doesn't happen if you remove the question mark. I'm not familiar with jupyter, so I'm not sure if this is intended behavior or not.
As a consequence of 1, black-nb will add a magic comment block to the beginning of the line after 0 spaces, which is not appropriate for a docstring. Black will correctly try to indent it, adding 4 spaces. When the magic is removed, those 4 spaces remain, which accounts for the diff you are seeing.
The text was updated successfully, but these errors were encountered:
I think this is a bit tricky to fix. Our contains_magic function works line by line, but in order to know whether the current line is inside a multi-line comment we would need to do something fancier with the AST, which is what black does.
As HassanAbouelela notes in the black issue, I would recommend using black to format notebooks since it now supports it natively.
Copied from the thread
Repro steps:
The error will be shown:
error: cannot format /path/to/nb/Repro.ipynb: INTERNAL ERROR: Black produced code that is not equivalent to the source on pass 1. Please report a bug on https://github.com/psf/black/issues. This diff might be helpful: ${TEMP}/blk_y4fjze4u.log
The diff will show that at the second pass extra whitespaces were added:
If the question mark will be removed, the notebook will pass
Example notebook:
contains_magic
makes no consideration for whether or not it's looking at a string. This explains why the issue doesn't happen if you remove the question mark. I'm not familiar with jupyter, so I'm not sure if this is intended behavior or not.As a consequence of 1, black-nb will add a magic comment block to the beginning of the line after 0 spaces, which is not appropriate for a docstring. Black will correctly try to indent it, adding 4 spaces. When the magic is removed, those 4 spaces remain, which accounts for the diff you are seeing.
The text was updated successfully, but these errors were encountered: