-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e78fee6
docs for updated F401 behavior #11168,#11314
plredmond c9ac09b
cargo insta review -- accept doc changes
plredmond fb1ac97
wording improvements in after zanie's comments
plredmond 7706561
cargo insta review -- accept documentation change
plredmond 36b4331
add deprecation warning for option lint.ignore_init_module_imports in…
plredmond 63a9676
add module member name field to UnusedImport violation; redundant-ali…
plredmond f656ca4
cargo insta review -- accept change in diff for incorrect fix title
plredmond 1bfa7b1
restore deprecated behavior for f401 when linter.ignore_init_module_i…
plredmond 4f0b79e
tests for f401 in stable w/o deprecated option
plredmond 3202de8
tests for f401 in stable w/ deprecated option: emit unsafe fixes to r…
plredmond 97d1526
add deprecation message for `ignore-init-module-imports` in settings …
plredmond 8d3ffbd
cargo insta review -- accept changed documentation
plredmond 249d848
cargo dev generate-all
plredmond b6871c9
tweak boolean condition
plredmond 715da2e
correct documentation formatting
plredmond 21fe25e
cargo insta review -- accept documentation change
plredmond 26f80c1
emit the deprecation warning if the option is set, regardless of valu…
plredmond 2be489f
improve wording of deprecation warning by using zanie's suggestion
plredmond 943ee25
list only the option, not information about it, in the docstring of a…
plredmond 6028415
cargo insta review -- accept change in documentation
plredmond File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
restore deprecated behavior for f401 when linter.ignore_init_module_i…
…mports=false: emit unsafe fixes to remove unused imports even in __init__.py
- Loading branch information
commit 1bfa7b1ba18214b22dd9fd4f60f6b9e09b296cbc
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ enum UnusedImportContext { | |
Init { | ||
first_party: bool, | ||
dunder_all_count: usize, | ||
ignore_init_module_imports: bool, | ||
}, | ||
} | ||
|
||
|
@@ -91,6 +92,10 @@ enum UnusedImportContext { | |
/// print("numpy is not installed") | ||
/// ``` | ||
/// | ||
/// ## Options | ||
/// - Deprecated `lint.ignore-init-module-imports` to `true`. When set to `false`, unused imports | ||
/// in `__init__.py` files are removed (unsafe). | ||
/// | ||
/// ## 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) | ||
|
@@ -140,11 +145,13 @@ impl Violation for UnusedImport { | |
Some(UnusedImportContext::Init { | ||
first_party: true, | ||
dunder_all_count: 1, | ||
ignore_init_module_imports: true, | ||
}) => Some(format!("Add unused import `{binding}` to __all__")), | ||
|
||
Some(UnusedImportContext::Init { | ||
first_party: true, | ||
dunder_all_count: 0, | ||
ignore_init_module_imports: true, | ||
}) => Some(format!("Use an explicit re-export: `{module} as {module}`")), | ||
|
||
_ => Some(if *multiple { | ||
|
@@ -257,7 +264,8 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut | |
} | ||
|
||
let in_init = checker.path().ends_with("__init__.py"); | ||
let fix_init = checker.settings.preview.is_enabled(); | ||
let fix_init = !checker.settings.ignore_init_module_imports; | ||
let preview_mode = checker.settings.preview.is_enabled(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These three lines look fishy because I'd originally replaced this let fix_init = !checker.settings.ignore_init_module_imports; with let fix_init = checker.settings.preview.is_enabled(); which was perhaps lazy. In this PR I'm restoring the old behavior, and so gave |
||
let dunder_all_exprs = find_dunder_all_exprs(checker.semantic()); | ||
|
||
// Generate a diagnostic for every import, but share fixes across all imports within the same | ||
|
@@ -288,6 +296,7 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut | |
checker, | ||
), | ||
dunder_all_count: dunder_all_exprs.len(), | ||
ignore_init_module_imports: !fix_init, | ||
}) | ||
} else { | ||
None | ||
|
@@ -301,7 +310,7 @@ pub(crate) fn unused_import(checker: &Checker, scope: &Scope, diagnostics: &mut | |
first_party: true, | ||
.. | ||
}) | ||
) | ||
) && preview_mode | ||
}); | ||
|
||
// generate fixes that are shared across bindings in the statement | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This field is required to make fix titles follow the "old" behavior in
__init__.py
files. When we finally remove the optionignore_init_module_imports
then this fieldignore_init_module_imports
can be deleted too.