From 40e0c249feb1dc9cd7c7489be8c5aa6e52b6440d Mon Sep 17 00:00:00 2001 From: Steve C Date: Wed, 29 Nov 2023 01:57:52 -0500 Subject: [PATCH] allow internal private imports --- .../fixtures/pylint/import_private_name.py | 2 + .../rules/pylint/rules/import_private_name.rs | 35 ++-- ...tests__PLC2701_import_private_name.py.snap | 190 +++++++++--------- ...s__PLC2701_import_private_name.py.snap.new | 126 ------------ 4 files changed, 121 insertions(+), 232 deletions(-) delete mode 100644 crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2701_import_private_name.py.snap.new diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/import_private_name.py b/crates/ruff_linter/resources/test/fixtures/pylint/import_private_name.py index 5ae18246f70f2..5826f82df3ea4 100644 --- a/crates/ruff_linter/resources/test/fixtures/pylint/import_private_name.py +++ b/crates/ruff_linter/resources/test/fixtures/pylint/import_private_name.py @@ -1,5 +1,7 @@ from __future__ import annotations # Ok +from .internal import _private # Ok + import _private # PLC2701 import _private as _p # PLC2701 from _private import _private # PLC2701 diff --git a/crates/ruff_linter/src/rules/pylint/rules/import_private_name.rs b/crates/ruff_linter/src/rules/pylint/rules/import_private_name.rs index 1249f58832e6b..8f203cf36963e 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/import_private_name.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/import_private_name.rs @@ -54,22 +54,22 @@ pub(crate) fn import_private_name(checker: &mut Checker, stmt: &Stmt) { } } } - Stmt::ImportFrom(ast::StmtImportFrom { names, module, .. }) => { - for alias in names { - if alias.name.as_str().starts_with('_') { - checker.diagnostics.push(Diagnostic::new( - ImportPrivateName { - symbol_type: SymbolType::Object, - }, - alias.name.range(), - )); - } - } - + Stmt::ImportFrom(ast::StmtImportFrom { + names, + module, + level, + .. + }) => { if let Some(identifier) = module { if identifier == "__future__" { return; } + + if level.is_some_and(|level| level > 0) { + // allow relative private imports, common in libs + return; + } + if identifier.starts_with('_') { checker.diagnostics.push(Diagnostic::new( ImportPrivateName { @@ -86,6 +86,17 @@ pub(crate) fn import_private_name(checker: &mut Checker, stmt: &Stmt) { )); } } + + for alias in names { + if alias.name.as_str().starts_with('_') { + checker.diagnostics.push(Diagnostic::new( + ImportPrivateName { + symbol_type: SymbolType::Object, + }, + alias.name.range(), + )); + } + } } _ => {} } diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2701_import_private_name.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2701_import_private_name.py.snap index 7e513304adc12..57997c50bc04c 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2701_import_private_name.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2701_import_private_name.py.snap @@ -1,123 +1,125 @@ --- source: crates/ruff_linter/src/rules/pylint/mod.rs --- -import_private_name.py:1:8: PLC2701 Imported private module +import_private_name.py:5:8: PLC2701 Imported private module | -1 | import _private +3 | from .internal import _private # Ok +4 | +5 | import _private # PLC2701 | ^^^^^^^^ PLC2701 -2 | import _private as _p -3 | from _private import _private +6 | import _private as _p # PLC2701 +7 | from _private import _private # PLC2701 | -import_private_name.py:2:8: PLC2701 Imported private module +import_private_name.py:6:8: PLC2701 Imported private module | -1 | import _private -2 | import _private as _p +5 | import _private # PLC2701 +6 | import _private as _p # PLC2701 | ^^^^^^^^ PLC2701 -3 | from _private import _private -4 | from lib import _private +7 | from _private import _private # PLC2701 +8 | from lib import _private # PLC2701 | -import_private_name.py:3:6: PLC2701 Imported private module +import_private_name.py:7:6: PLC2701 Imported private module | -1 | import _private -2 | import _private as _p -3 | from _private import _private +5 | import _private # PLC2701 +6 | import _private as _p # PLC2701 +7 | from _private import _private # PLC2701 | ^^^^^^^^ PLC2701 -4 | from lib import _private -5 | from lib import _private as _p +8 | from lib import _private # PLC2701 +9 | from lib import _private as _p # PLC2701 | -import_private_name.py:3:22: PLC2701 Imported private object +import_private_name.py:7:22: PLC2701 Imported private object | -1 | import _private -2 | import _private as _p -3 | from _private import _private +5 | import _private # PLC2701 +6 | import _private as _p # PLC2701 +7 | from _private import _private # PLC2701 | ^^^^^^^^ PLC2701 -4 | from lib import _private -5 | from lib import _private as _p +8 | from lib import _private # PLC2701 +9 | from lib import _private as _p # PLC2701 | -import_private_name.py:4:17: PLC2701 Imported private object - | -2 | import _private as _p -3 | from _private import _private -4 | from lib import _private - | ^^^^^^^^ PLC2701 -5 | from lib import _private as _p -6 | from lib._private import _private - | +import_private_name.py:8:17: PLC2701 Imported private object + | + 6 | import _private as _p # PLC2701 + 7 | from _private import _private # PLC2701 + 8 | from lib import _private # PLC2701 + | ^^^^^^^^ PLC2701 + 9 | from lib import _private as _p # PLC2701 +10 | from lib._private import _private # PLC2701 + | -import_private_name.py:5:17: PLC2701 Imported private object - | -3 | from _private import _private -4 | from lib import _private -5 | from lib import _private as _p - | ^^^^^^^^ PLC2701 -6 | from lib._private import _private -7 | from lib._private import _private as _p - | +import_private_name.py:9:17: PLC2701 Imported private object + | + 7 | from _private import _private # PLC2701 + 8 | from lib import _private # PLC2701 + 9 | from lib import _private as _p # PLC2701 + | ^^^^^^^^ PLC2701 +10 | from lib._private import _private # PLC2701 +11 | from lib._private import _private as _p # PLC2701 + | -import_private_name.py:6:6: PLC2701 Imported from private module - | -4 | from lib import _private -5 | from lib import _private as _p -6 | from lib._private import _private - | ^^^^^^^^^^^^ PLC2701 -7 | from lib._private import _private as _p -8 | from lib._private import _private, _private as _p - | +import_private_name.py:10:6: PLC2701 Imported from private module + | + 8 | from lib import _private # PLC2701 + 9 | from lib import _private as _p # PLC2701 +10 | from lib._private import _private # PLC2701 + | ^^^^^^^^^^^^ PLC2701 +11 | from lib._private import _private as _p # PLC2701 +12 | from lib._private import _private, _private as _p # PLC2701 + | -import_private_name.py:6:26: PLC2701 Imported private object - | -4 | from lib import _private -5 | from lib import _private as _p -6 | from lib._private import _private - | ^^^^^^^^ PLC2701 -7 | from lib._private import _private as _p -8 | from lib._private import _private, _private as _p - | +import_private_name.py:10:26: PLC2701 Imported private object + | + 8 | from lib import _private # PLC2701 + 9 | from lib import _private as _p # PLC2701 +10 | from lib._private import _private # PLC2701 + | ^^^^^^^^ PLC2701 +11 | from lib._private import _private as _p # PLC2701 +12 | from lib._private import _private, _private as _p # PLC2701 + | -import_private_name.py:7:6: PLC2701 Imported from private module - | -5 | from lib import _private as _p -6 | from lib._private import _private -7 | from lib._private import _private as _p - | ^^^^^^^^^^^^ PLC2701 -8 | from lib._private import _private, _private as _p - | +import_private_name.py:11:6: PLC2701 Imported from private module + | + 9 | from lib import _private as _p # PLC2701 +10 | from lib._private import _private # PLC2701 +11 | from lib._private import _private as _p # PLC2701 + | ^^^^^^^^^^^^ PLC2701 +12 | from lib._private import _private, _private as _p # PLC2701 + | -import_private_name.py:7:26: PLC2701 Imported private object - | -5 | from lib import _private as _p -6 | from lib._private import _private -7 | from lib._private import _private as _p - | ^^^^^^^^ PLC2701 -8 | from lib._private import _private, _private as _p - | +import_private_name.py:11:26: PLC2701 Imported private object + | + 9 | from lib import _private as _p # PLC2701 +10 | from lib._private import _private # PLC2701 +11 | from lib._private import _private as _p # PLC2701 + | ^^^^^^^^ PLC2701 +12 | from lib._private import _private, _private as _p # PLC2701 + | -import_private_name.py:8:6: PLC2701 Imported from private module - | -6 | from lib._private import _private -7 | from lib._private import _private as _p -8 | from lib._private import _private, _private as _p - | ^^^^^^^^^^^^ PLC2701 - | +import_private_name.py:12:6: PLC2701 Imported from private module + | +10 | from lib._private import _private # PLC2701 +11 | from lib._private import _private as _p # PLC2701 +12 | from lib._private import _private, _private as _p # PLC2701 + | ^^^^^^^^^^^^ PLC2701 + | -import_private_name.py:8:26: PLC2701 Imported private object - | -6 | from lib._private import _private -7 | from lib._private import _private as _p -8 | from lib._private import _private, _private as _p - | ^^^^^^^^ PLC2701 - | +import_private_name.py:12:26: PLC2701 Imported private object + | +10 | from lib._private import _private # PLC2701 +11 | from lib._private import _private as _p # PLC2701 +12 | from lib._private import _private, _private as _p # PLC2701 + | ^^^^^^^^ PLC2701 + | -import_private_name.py:8:36: PLC2701 Imported private object - | -6 | from lib._private import _private -7 | from lib._private import _private as _p -8 | from lib._private import _private, _private as _p - | ^^^^^^^^ PLC2701 - | +import_private_name.py:12:36: PLC2701 Imported private object + | +10 | from lib._private import _private # PLC2701 +11 | from lib._private import _private as _p # PLC2701 +12 | from lib._private import _private, _private as _p # PLC2701 + | ^^^^^^^^ PLC2701 + | diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2701_import_private_name.py.snap.new b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2701_import_private_name.py.snap.new deleted file mode 100644 index cb99e9534c36a..0000000000000 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLC2701_import_private_name.py.snap.new +++ /dev/null @@ -1,126 +0,0 @@ ---- -source: crates/ruff_linter/src/rules/pylint/mod.rs -assertion_line: 172 ---- -import_private_name.py:3:8: PLC2701 Imported private module - | -1 | from __future__ import annotations # Ok -2 | -3 | import _private # PLC2701 - | ^^^^^^^^ PLC2701 -4 | import _private as _p # PLC2701 -5 | from _private import _private # PLC2701 - | - -import_private_name.py:4:8: PLC2701 Imported private module - | -3 | import _private # PLC2701 -4 | import _private as _p # PLC2701 - | ^^^^^^^^ PLC2701 -5 | from _private import _private # PLC2701 -6 | from lib import _private # PLC2701 - | - -import_private_name.py:5:6: PLC2701 Imported private module - | -3 | import _private # PLC2701 -4 | import _private as _p # PLC2701 -5 | from _private import _private # PLC2701 - | ^^^^^^^^ PLC2701 -6 | from lib import _private # PLC2701 -7 | from lib import _private as _p # PLC2701 - | - -import_private_name.py:5:22: PLC2701 Imported private object - | -3 | import _private # PLC2701 -4 | import _private as _p # PLC2701 -5 | from _private import _private # PLC2701 - | ^^^^^^^^ PLC2701 -6 | from lib import _private # PLC2701 -7 | from lib import _private as _p # PLC2701 - | - -import_private_name.py:6:17: PLC2701 Imported private object - | -4 | import _private as _p # PLC2701 -5 | from _private import _private # PLC2701 -6 | from lib import _private # PLC2701 - | ^^^^^^^^ PLC2701 -7 | from lib import _private as _p # PLC2701 -8 | from lib._private import _private # PLC2701 - | - -import_private_name.py:7:17: PLC2701 Imported private object - | -5 | from _private import _private # PLC2701 -6 | from lib import _private # PLC2701 -7 | from lib import _private as _p # PLC2701 - | ^^^^^^^^ PLC2701 -8 | from lib._private import _private # PLC2701 -9 | from lib._private import _private as _p # PLC2701 - | - -import_private_name.py:8:6: PLC2701 Imported from private module - | - 6 | from lib import _private # PLC2701 - 7 | from lib import _private as _p # PLC2701 - 8 | from lib._private import _private # PLC2701 - | ^^^^^^^^^^^^ PLC2701 - 9 | from lib._private import _private as _p # PLC2701 -10 | from lib._private import _private, _private as _p # PLC2701 - | - -import_private_name.py:8:26: PLC2701 Imported private object - | - 6 | from lib import _private # PLC2701 - 7 | from lib import _private as _p # PLC2701 - 8 | from lib._private import _private # PLC2701 - | ^^^^^^^^ PLC2701 - 9 | from lib._private import _private as _p # PLC2701 -10 | from lib._private import _private, _private as _p # PLC2701 - | - -import_private_name.py:9:6: PLC2701 Imported from private module - | - 7 | from lib import _private as _p # PLC2701 - 8 | from lib._private import _private # PLC2701 - 9 | from lib._private import _private as _p # PLC2701 - | ^^^^^^^^^^^^ PLC2701 -10 | from lib._private import _private, _private as _p # PLC2701 - | - -import_private_name.py:9:26: PLC2701 Imported private object - | - 7 | from lib import _private as _p # PLC2701 - 8 | from lib._private import _private # PLC2701 - 9 | from lib._private import _private as _p # PLC2701 - | ^^^^^^^^ PLC2701 -10 | from lib._private import _private, _private as _p # PLC2701 - | - -import_private_name.py:10:6: PLC2701 Imported from private module - | - 8 | from lib._private import _private # PLC2701 - 9 | from lib._private import _private as _p # PLC2701 -10 | from lib._private import _private, _private as _p # PLC2701 - | ^^^^^^^^^^^^ PLC2701 - | - -import_private_name.py:10:26: PLC2701 Imported private object - | - 8 | from lib._private import _private # PLC2701 - 9 | from lib._private import _private as _p # PLC2701 -10 | from lib._private import _private, _private as _p # PLC2701 - | ^^^^^^^^ PLC2701 - | - -import_private_name.py:10:36: PLC2701 Imported private object - | - 8 | from lib._private import _private # PLC2701 - 9 | from lib._private import _private as _p # PLC2701 -10 | from lib._private import _private, _private as _p # PLC2701 - | ^^^^^^^^ PLC2701 - | - -