From c603f0e74972fd1edbc9d713cb917936a6a155de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 28 Jun 2022 20:30:35 +0300 Subject: [PATCH] Don't report `import-private-name` for relative imports (#7079) Closes #7078 --- doc/whatsnew/2/2.14/full.rst | 4 ++++ pylint/extensions/private_import.py | 2 ++ tests/functional/ext/private_import/private_import.py | 7 ++++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/doc/whatsnew/2/2.14/full.rst b/doc/whatsnew/2/2.14/full.rst index 77d8bf9d71..74177ea1e3 100644 --- a/doc/whatsnew/2/2.14/full.rst +++ b/doc/whatsnew/2/2.14/full.rst @@ -18,6 +18,10 @@ Release date: TBA Closes #6965 +* Don't report ``import-private-name`` for relative imports. + + Closes #7078 + What's New in Pylint 2.14.3? ---------------------------- diff --git a/pylint/extensions/private_import.py b/pylint/extensions/private_import.py index 37005fefa1..53e285ac34 100644 --- a/pylint/extensions/private_import.py +++ b/pylint/extensions/private_import.py @@ -250,6 +250,8 @@ def same_root_dir( """Does the node's file's path contain the base name of `import_mod_name`?""" if not import_mod_name: # from . import ... return True + if node.level: # from .foo import ..., from ..bar import ... + return True base_import_package = import_mod_name.split(".")[0] diff --git a/tests/functional/ext/private_import/private_import.py b/tests/functional/ext/private_import/private_import.py index 7b64a47f1d..917e01c9c2 100644 --- a/tests/functional/ext/private_import/private_import.py +++ b/tests/functional/ext/private_import/private_import.py @@ -1,7 +1,7 @@ """Tests for import-private-name.""" # pylint: disable=unused-import, missing-docstring, reimported, import-error, wrong-import-order # pylint: disable=no-name-in-module, multiple-imports, ungrouped-imports, misplaced-future -# pylint: disable=wrong-import-position +# pylint: disable=wrong-import-position, relative-beyond-top-level # Basic cases from _world import hello # [import-private-name] @@ -132,3 +132,8 @@ def get_example(self): class Example: def save(self): return self + + +# Treat relative imports as internal +from .other_file import _private +from ..parent import _private