nbQA-dev / nbQA

Run ruff, isort, pyupgrade, mypy, pylint, flake8, and more on Jupyter Notebooks
https://nbqa.readthedocs.io/en/latest/index.html
MIT License
1.01k stars 39 forks source link

Invalid line number for reporting diagnostics at first line #821

Open dhruvmanila opened 1 year ago

dhruvmanila commented 1 year ago

Jupyter notebook content

import math
import random

Cell count: 1

Command

$ nbqa --nbqa-shell ruff /path/to/notebook.ipynb --select=ALL

OR

$ nbqa --nbqa-shell flake8 /path/to/notebook.ipynb

Output

Note: Output is truncated to only keep the relevant lines

Ruff:

...
/Users/dhruv/playground/python/ruff_test.ipynb:cell_1:0:1: D100 Missing docstring in public module
/Users/dhruv/playground/python/ruff_test.ipynb:cell_1:2:8: F401 [*] `random` imported but unused
...

Flake8:

/Users/dhruv/playground/python/ruff_test.ipynb:cell_1:0:1: D100 Missing docstring in public module
/Users/dhruv/playground/python/ruff_test.ipynb:cell_1:2:1: F401 'random' imported but unused

Notice that D100 is reported at line 0 while F401 is reported at line 2. The former suggests that it's 0-indexed while the latter suggests it's 1-indexed. This is happening because of the CODE_SEPARATOR inserted between each code cells:

# %%NBQA-CELL-SEP8e47a4
import math
import random

MarcoGorelli commented 1 year ago

thanks for the report - could you show what happens when you run these tools on a Python script with the same content (no nbqa involved)?

dhruvmanila commented 1 year ago

Sure!

$ ruff check --select=ALL t.py                  
t.py:1:1: D100 Missing docstring in public module
t.py:1:8: F401 [*] `math` imported but unused
t.py:2:8: F401 [*] `random` imported but unused
Found 3 errors.
[*] 2 potentially fixable with the --fix option.

$ flake8 t.py       
t.py:1:1: D100 Missing docstring in public module
t.py:1:1: F401 'math' imported but unused
t.py:2:1: F401 'random' imported but unused