jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.68k stars 4.93k forks source link

This f-string is valid but Notebook doesn't understand how to format it #5310

Open CDWimmer opened 4 years ago

CDWimmer commented 4 years ago

Image attached. the line print(f"max count: {highest_breed} | min count: {lowest_breed} | average: {np.mean((breed_table['Count'])):.1f}") is completely valid and runs without error, however when being displayed in a Jupyter Notebook code cell it mis-colours the ending bracket and when you press enter/return at the end of the line it automatically indents, as if to say the print brackets are incomplete.

Adding another f-string below also has the same issue, so I guess this relates to F-strings being poorly interpreted by Jupyter.

image

I've a 9th jan installation through Anaconda on Ubuntu 18.04 LTS.

CDWimmer commented 4 years ago

I've narrowed it down a little further. Its the case where inner code within {curly brackets} contains a string, e.g. print(f"Dictionary Value: some_dict['some_key']")

Jupyter's text formatter seems to forget it was already inside a string (started by the first speech mark), starts a new one upon seeing the apostrophe, closes that with the second apostrophe as it should do, then opens a new one with the final speech mark. Thus it believes anything thereafter should be a string and it doesn't think the print function's bracket is closed. It then forgets all about it on the next line, besides automatically indenting due to the open bracket.