Skip to content

False positive F401 due to %%ipytest line in notebook #13718

@tovrstra

Description

@tovrstra

Keywords to find existing issue: F401 and ipytest. The following is very similar, but still different, it seems: #10454

A minimal working example would be a Jupyter notebook with the following two cells:

import ipytest
import pytest

ipytest.autoconfig()
%%ipytest

def test_simple():
    with pytest.raises(TypeError):
        print(1 + "a")

Ruff will report F401 (unused-import) for the line import pytest.

To reproduce the issue, create a notebook with the above cells, called bug.ipynb and run the command:

ruff check demos/bug.ipynb --fix

I've tested with with ruff 0.6.3 and 0.6.9. I have the following in my pyproject.toml, but I don't think these settings matter:

[tool.ruff]
line-length = 100
target-version = "py311"

[tool.ruff.lint]
select = ["E", "F", "UP", "B", "I", "PGH", "PL", "RUF"]
ignore = ["E741", "PLR2004", "PLR0913", "PLR0912", "PLW2901", "PLR0915"]

As a workaround, one can (obviously) add a comment # noqa: F401 to the import line

When removing the line %%ipytest, the problem goes away, but it that doesn't help because the line is doing something useful.

Given the easy workaround, this is probably not urgent.

By the way, thanks for creating Ruff!

Metadata

Metadata

Assignees

No one assigned

    Labels

    notebookRelated to (Jupyter) notebooks

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions