0

My team uses a bigquery specific iPython magic command to query and return data from BigQuery: https://cloud.google.com/python/docs/reference/bigquery/latest/magics

We also use jupytext to convert jupyter notebooks into percent formatted notebooks.

Black cannot parse the BigQuery iPython magic commands. Even when using # fmt: on/off or #fmt: skip as suggested in the comments and https://stackoverflow.com/a/58584557/7350094

black has cannot parse the following:

# %%
%%bigquery df
SELECT * FROM schema.table_name
error: cannot format filename.py: Cannot parse 2:0 %%bigquery df
  • Did you try to add comment tags to have Black skip the section causing you problems, see [here](https://stackoverflow.com/a/58584557/8508004) for an example? (And if you did, it should have been mentioned in the post, see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask).) There's also [`# fmt: skip`](https://stackoverflow.com/questions/58584413/black-formatter-ignore-specific-multi-line-code#comment123224606_58584557). However, Jupyter magics in general were trouble, see [here](https://github.com/psf/black/issues/774). Maybe this is better as an issue post there? – Wayne May 10 '23 at 16:53
  • Or does [nb-black](https://github.com/dnanhkhoa/nb_black) work? Or allow altering to handle what you need? – Wayne May 10 '23 at 16:58
  • Thanks for the suggestion. I just tried # fmt: on/off/skip do not work, I still get the same error. The file itself is not a Jupyter notebook, it is a percent formatted notebook converted from a Jupyter notebook (or edited as a plain text file thereafter). We want to use black, or something else, in our CI pipeline to standardise syntax. – Anil Sharma May 13 '23 at 10:14
  • I know it might be a pain to add steps but can you convert back to a notebook and then run black with configuration where you add the magic you need. [Here](https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html). Under the CLI reference there is `--python-cell-magics TEXT` that says "Useful for formatting cells with custom python magics." Then you can use Jupytext to go back to the percent formatted again? It still might be good to start with an issue post in black pointing to here. And maybe even one in Jupytext pointing at both those places? It seems addressable. – Wayne May 13 '23 at 15:30

0 Answers0