Skip to content

F401 false positive (imported but unused) #2044

@dllu

Description

@dllu
from functools import reduce

import click

if click.confirm("do you want to disable reduce?", default=False):

    def reduce(f, d):  # noqa: F811
        return d


print(reduce(lambda x, y: x + y, [1, 2, 3, 4, 5]))

It is supposed to work like this:

snippets » python w0611.py
do you want to disable reduce? [y/N]:
15
snippets » python w0611.py
do you want to disable reduce? [y/N]: y
[1, 2, 3, 4, 5]

Here is what ruff outputs:

> ruff w0611.py
w0611.py:1:23: F401 `functools.reduce` imported but unused
Found 1 error(s).
1 potentially fixable with the --fix option.

If we apply the fix, then the code no longer works if the user presses "N" to not disable reduce.

> ruff --version
ruff 0.0.224

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions