Skip to content

allowed-unused-imports doesn't work with relative imports #15705

@dhduvall

Description

@dhduvall

Description

If an "unused" import is done with a relative path, allowed-unused-imports can't be configured to to allow it:

$ uv init --lib --name mod
$ cat >> pyproject.toml <<EOF

[tool.ruff]
lint.pyflakes.allowed-unused-imports = [
  "mod.foo.bar",
]
EOF
$ echo "from .foo import bar" > src/mod/junk.py
$ echo "bar = None" > src/mod/foo.py
$ uvx ruff check src
src/mod/junk.py:1:18: F401 [*] `.foo.bar` imported but unused
  |
1 | from .foo import bar
  |                  ^^^ F401
  |
  = help: Remove unused import: `.foo.bar`

Found 1 error.
[*] 1 fixable with the `--fix` option.
$ uvx ruff version
ruff 0.9.3 (90589372d 2025-01-23)

I was unable to find a spelling of mod.foo.bar that would work—none of .foo.bar, foo.bar, foo work.
If junk.py contains from mod.foo import bar (i.e., absolute path for the import), ruff allows the import as expected.

Metadata

Metadata

Assignees

Labels

configurationRelated to settings and configurationruleImplementing or modifying a lint rule

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions