mirror of https://github.com/astral-sh/ruff
[`pylint`] Stabilize ignoring `__init__.py` for `useless-import-alias` (`PLC0414`) (#20271)
Stabilizes change from #18400. Removed gating, updated docs, updated tests.
This commit is contained in:
parent
4bda9dad68
commit
5dec37fbaf
|
|
@ -200,13 +200,6 @@ pub(crate) const fn is_allow_nested_roots_enabled(settings: &LinterSettings) ->
|
||||||
settings.preview.is_enabled()
|
settings.preview.is_enabled()
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://github.com/astral-sh/ruff/pull/18400
|
|
||||||
pub(crate) const fn is_ignore_init_files_in_useless_alias_enabled(
|
|
||||||
settings: &LinterSettings,
|
|
||||||
) -> bool {
|
|
||||||
settings.preview.is_enabled()
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://github.com/astral-sh/ruff/pull/18572
|
// https://github.com/astral-sh/ruff/pull/18572
|
||||||
pub(crate) const fn is_optional_as_none_in_union_enabled(settings: &LinterSettings) -> bool {
|
pub(crate) const fn is_optional_as_none_in_union_enabled(settings: &LinterSettings) -> bool {
|
||||||
settings.preview.is_enabled()
|
settings.preview.is_enabled()
|
||||||
|
|
|
||||||
|
|
@ -420,19 +420,6 @@ mod tests {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn preview_useless_import_alias() -> Result<()> {
|
|
||||||
let diagnostics = test_path(
|
|
||||||
Path::new("pylint/import_aliasing_2/__init__.py"),
|
|
||||||
&LinterSettings {
|
|
||||||
preview: PreviewMode::Enabled,
|
|
||||||
..LinterSettings::for_rule(Rule::UselessImportAlias)
|
|
||||||
},
|
|
||||||
)?;
|
|
||||||
assert_diagnostics!(diagnostics);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn import_outside_top_level_with_banned() -> Result<()> {
|
fn import_outside_top_level_with_banned() -> Result<()> {
|
||||||
let diagnostics = test_path(
|
let diagnostics = test_path(
|
||||||
|
|
|
||||||
|
|
@ -4,13 +4,11 @@ use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||||
use ruff_text_size::Ranged;
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::preview::is_ignore_init_files_in_useless_alias_enabled;
|
|
||||||
use crate::{Edit, Fix, FixAvailability, Violation};
|
use crate::{Edit, Fix, FixAvailability, Violation};
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
/// Checks for import aliases that do not rename the original package.
|
/// Checks for import aliases that do not rename the original package.
|
||||||
///
|
/// This rule does not apply in `__init__.py` files.
|
||||||
/// In [preview] this rule does not apply in `__init__.py` files.
|
|
||||||
///
|
///
|
||||||
/// ## Why is this bad?
|
/// ## Why is this bad?
|
||||||
/// The import alias is redundant and should be removed to avoid confusion.
|
/// The import alias is redundant and should be removed to avoid confusion.
|
||||||
|
|
@ -35,8 +33,6 @@ use crate::{Edit, Fix, FixAvailability, Violation};
|
||||||
/// ```python
|
/// ```python
|
||||||
/// import numpy
|
/// import numpy
|
||||||
/// ```
|
/// ```
|
||||||
///
|
|
||||||
/// [preview]: https://docs.astral.sh/ruff/preview/
|
|
||||||
#[derive(ViolationMetadata)]
|
#[derive(ViolationMetadata)]
|
||||||
pub(crate) struct UselessImportAlias {
|
pub(crate) struct UselessImportAlias {
|
||||||
required_import_conflict: bool,
|
required_import_conflict: bool,
|
||||||
|
|
@ -73,9 +69,7 @@ pub(crate) fn useless_import_alias(checker: &Checker, alias: &Alias) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// A re-export in __init__.py is probably intentional.
|
// A re-export in __init__.py is probably intentional.
|
||||||
if checker.path().ends_with("__init__.py")
|
if checker.path().ends_with("__init__.py") {
|
||||||
&& is_ignore_init_files_in_useless_alias_enabled(checker.settings())
|
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,4 @@
|
||||||
---
|
---
|
||||||
source: crates/ruff_linter/src/rules/pylint/mod.rs
|
source: crates/ruff_linter/src/rules/pylint/mod.rs
|
||||||
---
|
---
|
||||||
PLC0414 [*] Import alias does not rename original package
|
|
||||||
--> __init__.py:1:8
|
|
||||||
|
|
|
||||||
1 | import collections as collections
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
2 | from collections import OrderedDict as OrderedDict
|
|
||||||
3 | from . import foo as foo
|
|
||||||
|
|
|
||||||
help: Remove import alias
|
|
||||||
- import collections as collections
|
|
||||||
1 + import collections
|
|
||||||
2 | from collections import OrderedDict as OrderedDict
|
|
||||||
3 | from . import foo as foo
|
|
||||||
4 | from .foo import bar as bar
|
|
||||||
note: This is an unsafe fix and may change runtime behavior
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
---
|
|
||||||
source: crates/ruff_linter/src/rules/pylint/mod.rs
|
|
||||||
---
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue