|
| 1 | +--- |
| 2 | +source: crates/ruff/tests/integration_test.rs |
| 3 | +info: |
| 4 | + program: ruff |
| 5 | + args: |
| 6 | + - rule |
| 7 | + - F401 |
| 8 | + - "--output-format" |
| 9 | + - text |
| 10 | +--- |
| 11 | +success: true |
| 12 | +exit_code: 0 |
| 13 | +----- stdout ----- |
| 14 | +# unused-import (F401) |
| 15 | + |
| 16 | +Derived from the **Pyflakes** linter. |
| 17 | + |
| 18 | +Fix is sometimes available. |
| 19 | + |
| 20 | +## What it does |
| 21 | +Checks for unused imports. |
| 22 | + |
| 23 | +## Why is this bad? |
| 24 | +Unused imports add a performance overhead at runtime, and risk creating |
| 25 | +import cycles. They also increase the cognitive load of reading the code. |
| 26 | + |
| 27 | +If an import statement is used to check for the availability or existence |
| 28 | +of a module, consider using `importlib.util.find_spec` instead. |
| 29 | + |
| 30 | +If an import statement is used to re-export a symbol as part of a module's |
| 31 | +public interface, consider using a "redundant" import alias, which |
| 32 | +instructs Ruff (and other tools) to respect the re-export, and avoid |
| 33 | +marking it as unused, as in: |
| 34 | + |
| 35 | +```python |
| 36 | +from module import member as member |
| 37 | +``` |
| 38 | + |
| 39 | +Alternatively, you can use `__all__` to declare a symbol as part of the module's |
| 40 | +interface, as in: |
| 41 | + |
| 42 | +```python |
| 43 | +# __init__.py |
| 44 | +import some_module |
| 45 | + |
| 46 | +__all__ = ["some_module"] |
| 47 | +``` |
| 48 | + |
| 49 | +## Preview |
| 50 | +When [preview] is enabled (and certain simplifying assumptions |
| 51 | +are met), we analyze all import statements for a given module |
| 52 | +when determining whether an import is used, rather than simply |
| 53 | +the last of these statements. This can result in both different and |
| 54 | +more import statements being marked as unused. |
| 55 | + |
| 56 | +For example, if a module consists of |
| 57 | + |
| 58 | +```python |
| 59 | +import a |
| 60 | +import a.b |
| 61 | +``` |
| 62 | + |
| 63 | +then both statements are marked as unused under [preview], whereas |
| 64 | +only the second is marked as unused under stable behavior. |
| 65 | + |
| 66 | +As another example, if a module consists of |
| 67 | + |
| 68 | +```python |
| 69 | +import a.b |
| 70 | +import a |
| 71 | + |
| 72 | +a.b.foo() |
| 73 | +``` |
| 74 | + |
| 75 | +then a diagnostic will only be emitted for the first line under [preview], |
| 76 | +whereas a diagnostic would only be emitted for the second line under |
| 77 | +stable behavior. |
| 78 | + |
| 79 | +Note that this behavior is somewhat subjective and is designed |
| 80 | +to conform to the developer's intuition rather than Python's actual |
| 81 | +execution. To wit, the statement `import a.b` automatically executes |
| 82 | +`import a`, so in some sense `import a` is _always_ redundant |
| 83 | +in the presence of `import a.b`. |
| 84 | + |
| 85 | + |
| 86 | +## Fix safety |
| 87 | + |
| 88 | +Fixes to remove unused imports are safe, except in `__init__.py` files. |
| 89 | + |
| 90 | +Applying fixes to `__init__.py` files is currently in preview. The fix offered depends on the |
| 91 | +type of the unused import. Ruff will suggest a safe fix to export first-party imports with |
| 92 | +either a redundant alias or, if already present in the file, an `__all__` entry. If multiple |
| 93 | +`__all__` declarations are present, Ruff will not offer a fix. Ruff will suggest an unsafe fix |
| 94 | +to remove third-party and standard library imports -- the fix is unsafe because the module's |
| 95 | +interface changes. |
| 96 | + |
| 97 | +See [this FAQ section](https://docs.astral.sh/ruff/faq/#how-does-ruff-determine-which-of-my-imports-are-first-party-third-party-etc) |
| 98 | +for more details on how Ruff |
| 99 | +determines whether an import is first or third-party. |
| 100 | + |
| 101 | +## Example |
| 102 | + |
| 103 | +```python |
| 104 | +import numpy as np # unused import |
| 105 | + |
| 106 | + |
| 107 | +def area(radius): |
| 108 | + return 3.14 * radius**2 |
| 109 | +``` |
| 110 | + |
| 111 | +Use instead: |
| 112 | + |
| 113 | +```python |
| 114 | +def area(radius): |
| 115 | + return 3.14 * radius**2 |
| 116 | +``` |
| 117 | + |
| 118 | +To check the availability of a module, use `importlib.util.find_spec`: |
| 119 | + |
| 120 | +```python |
| 121 | +from importlib.util import find_spec |
| 122 | + |
| 123 | +if find_spec("numpy") is not None: |
| 124 | + print("numpy is installed") |
| 125 | +else: |
| 126 | + print("numpy is not installed") |
| 127 | +``` |
| 128 | + |
| 129 | +## Options |
| 130 | +- `lint.ignore-init-module-imports` |
| 131 | +- `lint.pyflakes.allowed-unused-imports` |
| 132 | + |
| 133 | +## References |
| 134 | +- [Python documentation: `import`](https://docs.python.org/3/reference/simple_stmts.html#the-import-statement) |
| 135 | +- [Python documentation: `importlib.util.find_spec`](https://docs.python.org/3/library/importlib.html#importlib.util.find_spec) |
| 136 | +- [Typing documentation: interface conventions](https://typing.python.org/en/latest/spec/distributing.html#library-interface-public-and-private-symbols) |
| 137 | + |
| 138 | +[preview]: https://docs.astral.sh/ruff/preview/ |
| 139 | + |
| 140 | +----- stderr ----- |
0 commit comments