From a6d7ffc4679b0ad2258cf6c31c6dfbeeb2b331ef Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 12 Feb 2020 09:18:24 +0100 Subject: [PATCH] Do not add the current directory to `sys.path` any longer Adding the current directory to `sys.path` can also mean that we're going to load modules having the same name as stdlib modules or astroid modules, which can break pylint. We were doing this since 4becf6f9e596b45401680c4947e2d92c953d5e08, but there was indication on why we were doing that. --- pylint/lint.py | 8 ++++++-- tests/unittest_lint.py | 18 +++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/pylint/lint.py b/pylint/lint.py index 578e847ff2..1960e544b1 100644 --- a/pylint/lint.py +++ b/pylint/lint.py @@ -1475,11 +1475,15 @@ def fix_import_path(args): """ orig = list(sys.path) changes = [] + seen = set() + cwd = os.getcwd() for arg in args: path = _get_python_path(arg) - if path not in changes: + if path not in seen and path != cwd: changes.append(path) - sys.path[:] = changes + ["."] + sys.path + seen.add(path) + + sys.path[:] = changes + sys.path try: yield finally: diff --git a/tests/unittest_lint.py b/tests/unittest_lint.py index 3252a5e437..336e05e820 100644 --- a/tests/unittest_lint.py +++ b/tests/unittest_lint.py @@ -165,7 +165,7 @@ def fake_path(): def test_no_args(fake_path): with lint.fix_import_path([]): - assert sys.path == ["."] + fake_path + assert sys.path == fake_path assert sys.path == fake_path @@ -175,7 +175,7 @@ def test_no_args(fake_path): def test_one_arg(fake_path, case): with tempdir() as chroot: create_files(["a/b/__init__.py"]) - expected = [join(chroot, "a")] + ["."] + fake_path + expected = [join(chroot, "a")] + fake_path assert sys.path == fake_path with lint.fix_import_path(case): @@ -195,7 +195,7 @@ def test_one_arg(fake_path, case): def test_two_similar_args(fake_path, case): with tempdir() as chroot: create_files(["a/b/__init__.py", "a/c/__init__.py"]) - expected = [join(chroot, "a")] + ["."] + fake_path + expected = [join(chroot, "a")] + fake_path assert sys.path == fake_path with lint.fix_import_path(case): @@ -214,14 +214,10 @@ def test_two_similar_args(fake_path, case): def test_more_args(fake_path, case): with tempdir() as chroot: create_files(["a/b/c/__init__.py", "a/d/__init__.py", "a/e/f.py"]) - expected = ( - [ - join(chroot, suffix) - for suffix in [sep.join(("a", "b")), "a", sep.join(("a", "e"))] - ] - + ["."] - + fake_path - ) + expected = [ + join(chroot, suffix) + for suffix in [sep.join(("a", "b")), "a", sep.join(("a", "e"))] + ] + fake_path assert sys.path == fake_path with lint.fix_import_path(case):