mirror of
				https://github.com/astral-sh/ruff.git
				synced 2025-11-03 21:24:29 +00:00 
			
		
		
		
	[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
					
				
					 5 changed files with 3 additions and 47 deletions
				
			
		| 
						 | 
				
			
			@ -200,13 +200,6 @@ pub(crate) const fn is_allow_nested_roots_enabled(settings: &LinterSettings) ->
 | 
			
		|||
    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
 | 
			
		||||
pub(crate) const fn is_optional_as_none_in_union_enabled(settings: &LinterSettings) -> bool {
 | 
			
		||||
    settings.preview.is_enabled()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -420,19 +420,6 @@ mod tests {
 | 
			
		|||
        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]
 | 
			
		||||
    fn import_outside_top_level_with_banned() -> Result<()> {
 | 
			
		||||
        let diagnostics = test_path(
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,13 +4,11 @@ use ruff_macros::{ViolationMetadata, derive_message_formats};
 | 
			
		|||
use ruff_text_size::Ranged;
 | 
			
		||||
 | 
			
		||||
use crate::checkers::ast::Checker;
 | 
			
		||||
use crate::preview::is_ignore_init_files_in_useless_alias_enabled;
 | 
			
		||||
use crate::{Edit, Fix, FixAvailability, Violation};
 | 
			
		||||
 | 
			
		||||
/// ## What it does
 | 
			
		||||
/// Checks for import aliases that do not rename the original package.
 | 
			
		||||
///
 | 
			
		||||
/// In [preview] this rule does not apply in `__init__.py` files.
 | 
			
		||||
/// This rule does not apply in `__init__.py` files.
 | 
			
		||||
///
 | 
			
		||||
/// ## Why is this bad?
 | 
			
		||||
/// The import alias is redundant and should be removed to avoid confusion.
 | 
			
		||||
| 
						 | 
				
			
			@ -35,8 +33,6 @@ use crate::{Edit, Fix, FixAvailability, Violation};
 | 
			
		|||
/// ```python
 | 
			
		||||
/// import numpy
 | 
			
		||||
/// ```
 | 
			
		||||
///
 | 
			
		||||
/// [preview]: https://docs.astral.sh/ruff/preview/
 | 
			
		||||
#[derive(ViolationMetadata)]
 | 
			
		||||
pub(crate) struct UselessImportAlias {
 | 
			
		||||
    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.
 | 
			
		||||
    if checker.path().ends_with("__init__.py")
 | 
			
		||||
        && is_ignore_init_files_in_useless_alias_enabled(checker.settings())
 | 
			
		||||
    {
 | 
			
		||||
    if checker.path().ends_with("__init__.py") {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,18 +1,4 @@
 | 
			
		|||
---
 | 
			
		||||
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…
	
	Add table
		Add a link
		
	
		Reference in a new issue