nbformat
nbformat copied to clipboard
Should read and reads handle the case where `as_version` is not specified?
Since the nbformat information is part of the notebook document, when not specified, could nbformat use that instead?
The structure of a notebook object in memory is part of the nbformat API, so the as_version parameter exists to separate that from the contents of a file on disk. There's very deliberately no default, because the obvious default (don't convert it at all) means that the API depends on what's in the file you're loading.
E.g. with a previous version, lots of code did things like this:
nb = nbformat.read('some_notebook.ipynb')
for cell in nb.worksheets[0].cells:
...
All that code broke when we revised the notebook format and got rid of worksheets. If the as_version mechanism had been in place at the time, notebooks would have been converted back to the old structure and the code would continue working. Then the developer can update that code to the new API whenever suits them.
Just chiming in here because I was just thinking the same thing. I agree that "explicit is better than implicit" when it comes to making assumptions about the notebook version. That said, I can say that I have never used anything other than passing nbf.NO_CONVERT. From a user's perspective, it feels strange because I probably don't even realize that there are "versions" of the notebook specification.
I don't know what's the right way to satisfy both perspectives because I agree it's also confusing to read a notebook and then get unexpected behavior, but I just wanted to add a vote that I also feel typing "NO_CONVERT" every time feels cumbersome (and is being designed around by tools like Jupytext: https://jupytext.readthedocs.io/en/latest/using-library.html)
I second @choldgraf's comment and also request making NO_CONVERT the default. In my opinion, good defaults are meant to make code cleaner and more concise under the most common use cases. They're not expected to cover all edge cases or bugs caused by users not taking them into account.