Databricks "Format document" not adding newline to end of file

67 views Asked by At

I'm using Databricks Runtime 13.3 LTS ML and for some reason the "Format document" black formatting is not adding a newline to the end of (python) file.

However, running same version of black locally, the newline is added.

Is there a way to force Databricks to add the newline?

1

There are 1 answers

0
JayashankarGS On

Actually, the newline is added to the file when you format the document, but in Databricks, when you check, it seems like a newline is not added.

Below is the code in .py in local.

from typing import Union
from fastapi import FastAPI

app = FastAPI()
@app.get("/")
def read_root():
    return {"Hello": "World"}
@app.get("/items/")
def read_item(item_id: int, q: Union[str, None] = None):
    return {"item_id": item_id, "q": q}

After re-formatting, you will see 15 lines, and the 15th is the newline.

enter image description here

But the same thing in Databricks shows 14 lines like below after re-formatting.

enter image description here

This is because whenever you calculate the number of lines, it gives a count of 1 less than expected, as it only counts when it reads the \n character. In line 15, there is no \n character, so it gives 14, but when you check it in an IDE, you will see newline. However, internally, the newline is added in both Databricks and locally.