astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
31.24k stars 1.04k forks source link

[BUG] Ruff raises "undefined name" error in notebooks with objects imported atop #8304

Closed baggiponte closed 10 months ago

baggiponte commented 11 months ago

A project I maintain, functime, uses ruff to lint the notebooks. I just ran ruff on this notebook and I get a slew of errors of imported objects (e.g. pl) that are flagged as undefined variables:

``` docs/notebooks/evaluation.ipynb:cell 6:2:5: F821 Undefined name `pl` docs/notebooks/evaluation.ipynb:cell 6:4:19: F821 Undefined name `train_test_split` docs/notebooks/evaluation.ipynb:cell 8:2:5: F821 Undefined name `pl` docs/notebooks/evaluation.ipynb:cell 8:3:5: F821 Undefined name `pl` docs/notebooks/evaluation.ipynb:cell 8:4:5: F821 Undefined name `pl` docs/notebooks/evaluation.ipynb:cell 10:1:16: F821 Undefined name `snaive` docs/notebooks/evaluation.ipynb:cell 12:1:9: F821 Undefined name `rank_point_forecasts` docs/notebooks/evaluation.ipynb:cell 13:2:10: F821 Undefined name `plot_forecasts` docs/notebooks/evaluation.ipynb:cell 13:3:21: F821 Undefined name `pl` docs/notebooks/evaluation.ipynb:cell 13:4:32: F821 Undefined name `pl` docs/notebooks/evaluation.ipynb:cell 15:1:9: F821 Undefined name `rank_point_forecasts` docs/notebooks/evaluation.ipynb:cell 16:2:10: F821 Undefined name `plot_forecasts` docs/notebooks/evaluation.ipynb:cell 16:3:21: F821 Undefined name `pl` docs/notebooks/evaluation.ipynb:cell 16:4:32: F821 Undefined name `pl` docs/notebooks/evaluation.ipynb:cell 18:2:14: F821 Undefined name `lightgbm` docs/notebooks/evaluation.ipynb:cell 18:2:61: F821 Undefined name `detrend` docs/notebooks/evaluation.ipynb:cell 20:1:9: F821 Undefined name `rank_point_forecasts` docs/notebooks/evaluation.ipynb:cell 21:2:10: F821 Undefined name `plot_forecasts` docs/notebooks/evaluation.ipynb:cell 21:3:21: F821 Undefined name `pl` docs/notebooks/evaluation.ipynb:cell 21:4:27: F821 Undefined name `pl` docs/notebooks/evaluation.ipynb:cell 23:1:9: F821 Undefined name `rank_point_forecasts` docs/notebooks/evaluation.ipynb:cell 24:2:10: F821 Undefined name `plot_forecasts` docs/notebooks/evaluation.ipynb:cell 24:3:21: F821 Undefined name `pl` docs/notebooks/evaluation.ipynb:cell 24:4:27: F821 Undefined name `pl` docs/notebooks/evaluation.ipynb:cell 26:1:9: F821 Undefined name `rank_residuals` docs/notebooks/evaluation.ipynb:cell 27:2:10: F821 Undefined name `plot_residuals` docs/notebooks/evaluation.ipynb:cell 27:3:30: F821 Undefined name `pl` docs/notebooks/evaluation.ipynb:cell 29:1:9: F821 Undefined name `rank_residuals` docs/notebooks/evaluation.ipynb:cell 30:2:10: F821 Undefined name `plot_residuals` docs/notebooks/evaluation.ipynb:cell 30:3:30: F821 Undefined name `pl` docs/notebooks/evaluation.ipynb:cell 32:1:10: F821 Undefined name `plot_fva` docs/notebooks/evaluation.ipynb:cell 34:1:10: F821 Undefined name `plot_comet` ```

Our configs:

[tool.ruff]
include = ["*.py", "*.pyi", "**/pyproject.toml", "*.ipynb"]
select = [
    "E",  # pycodestyle errors
    "W",  # pycodestyle warnings
    "F",  # pyflakes
    "I",  # isort
    "B",  # flake8-bugbear
    "UP", # pyupgrade
    "I",  # isort
]
ignore = [
    "E501", # line too long, handled by black
    "B008", # do not perform function calls in argument defaults
    "B905", # `zip()` without an explicit `strict=` parameter
]

[tool.ruff.pyupgrade]
# Preserve types, even if a file imports `from __future__ import annotations`.
keep-runtime-typing = true
charliermarsh commented 11 months ago

\cc @dhruvmanila

dhruvmanila commented 10 months ago

This is happening because the cell containing the imports has a cell magic defined:

%%capture
import polars as pl

# ...

We ignore cell containing cell magics as they apply to the entire cell. The reasoning behind that is mentioned here: https://github.com/astral-sh/ruff/issues/7908#issuecomment-1756749556

Separately, I'm not sure if %%capture is really useful in a cell containing imports as there probably won't be any output when running the cell.

I'll close this considering this as expected but feel free to ask any further questions or clarification required.

baggiponte commented 10 months ago

Ops totally right, sorry @dhruvmanila. Thank you for the prompt answers!

dhruvmanila commented 10 months ago

No problem, happy to help :)