Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F401 - update documentation and deprecate ignore_init_module_imports #11436

Merged
merged 20 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
e78fee6
docs for updated F401 behavior #11168,#11314
plredmond May 15, 2024
c9ac09b
cargo insta review -- accept doc changes
plredmond May 15, 2024
fb1ac97
wording improvements in after zanie's comments
plredmond May 15, 2024
7706561
cargo insta review -- accept documentation change
plredmond May 15, 2024
36b4331
add deprecation warning for option lint.ignore_init_module_imports in…
plredmond May 20, 2024
63a9676
add module member name field to UnusedImport violation; redundant-ali…
plredmond May 20, 2024
f656ca4
cargo insta review -- accept change in diff for incorrect fix title
plredmond May 20, 2024
1bfa7b1
restore deprecated behavior for f401 when linter.ignore_init_module_i…
plredmond May 20, 2024
4f0b79e
tests for f401 in stable w/o deprecated option
plredmond May 20, 2024
3202de8
tests for f401 in stable w/ deprecated option: emit unsafe fixes to r…
plredmond May 20, 2024
97d1526
add deprecation message for `ignore-init-module-imports` in settings …
plredmond May 20, 2024
8d3ffbd
cargo insta review -- accept changed documentation
plredmond May 20, 2024
249d848
cargo dev generate-all
plredmond May 20, 2024
b6871c9
tweak boolean condition
plredmond May 20, 2024
715da2e
correct documentation formatting
plredmond May 20, 2024
21fe25e
cargo insta review -- accept documentation change
plredmond May 20, 2024
26f80c1
emit the deprecation warning if the option is set, regardless of valu…
plredmond May 21, 2024
2be489f
improve wording of deprecation warning by using zanie's suggestion
plredmond May 21, 2024
943ee25
list only the option, not information about it, in the docstring of a…
plredmond May 21, 2024
6028415
cargo insta review -- accept change in documentation
plredmond May 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions crates/ruff/tests/snapshots/integration_test__rule_f401.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,32 @@ marking it as unused, as in:
from module import member as member
```

Within `__init__.py` re-export a symbol with an `__all__` declaration containing a string
matching the symbol, as in:
plredmond marked this conversation as resolved.
Show resolved Hide resolved

```python
# __init__.py
import some_module

__all__ = [ "some_module"]
```

## Fix safety

When `ignore_init_module_imports` is disabled, fixes can remove for unused imports in `__init__` files.
These fixes are considered unsafe because they can change the public interface.
Fixes to remove unused imports are safe, except in `__init__.py` files, where they could
change the public interface. Fixes in `__init__.py` files are currently gated within preview
mode.
plredmond marked this conversation as resolved.
Show resolved Hide resolved

In preview mode, `__init__.py` files containing unused first party imports will have
safe fixes to add those imports `__all__` if there is exactly one `__all__` declaration
(otherwise the fixes recommend conversion to "redundant" import aliases).
Unused standard library and third-party imports in `__init__.py` have unsafe fixes to remove
the import statement.
plredmond marked this conversation as resolved.
Show resolved Hide resolved

## Example
```python
import numpy as np # unused import


def area(radius):
return 3.14 * radius**2
```
Expand All @@ -64,9 +80,6 @@ else:
print("numpy is not installed")
```

## Options
- `lint.ignore-init-module-imports`

## References
- [Python documentation: `import`](https://docs.python.org/3/reference/simple_stmts.html#the-import-statement)
- [Python documentation: `importlib.util.find_spec`](https://docs.python.org/3/library/importlib.html#importlib.util.find_spec)
Expand Down
25 changes: 19 additions & 6 deletions crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,32 @@ enum UnusedImportContext {
/// from module import member as member
plredmond marked this conversation as resolved.
Show resolved Hide resolved
/// ```
///
/// Within `__init__.py` re-export a symbol with an `__all__` declaration containing a string
/// matching the symbol, as in:
///
/// ```python
/// # __init__.py
/// import some_module
///
/// __all__ = [ "some_module"]
/// ```
///
/// ## Fix safety
///
/// When `ignore_init_module_imports` is disabled, fixes can remove for unused imports in `__init__` files.
/// These fixes are considered unsafe because they can change the public interface.
/// Fixes to remove unused imports are safe, except in `__init__.py` files, where they could
/// change the public interface. Fixes in `__init__.py` files are currently gated within preview
/// mode.
///
/// In preview mode, `__init__.py` files containing unused first party imports will have
/// safe fixes to add those imports `__all__` if there is exactly one `__all__` declaration
/// (otherwise the fixes recommend conversion to "redundant" import aliases).
/// Unused standard library and third-party imports in `__init__.py` have unsafe fixes to remove
/// the import statement.
///
/// ## Example
/// ```python
/// import numpy as np # unused import
///
///
/// def area(radius):
/// return 3.14 * radius**2
/// ```
Expand All @@ -76,9 +92,6 @@ enum UnusedImportContext {
/// print("numpy is not installed")
/// ```
///
/// ## Options
/// - `lint.ignore-init-module-imports`
///
/// ## References
/// - [Python documentation: `import`](https://docs.python.org/3/reference/simple_stmts.html#the-import-statement)
/// - [Python documentation: `importlib.util.find_spec`](https://docs.python.org/3/library/importlib.html#importlib.util.find_spec)
Expand Down
Loading