Skip to content

Commit

Permalink
Do not add the current directory to sys.path any longer
Browse files Browse the repository at this point in the history
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 4becf6f,
but there was indication on why we were doing that.
  • Loading branch information
PCManticore committed Mar 12, 2020
1 parent 47ec7e7 commit a6d7ffc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
8 changes: 6 additions & 2 deletions pylint/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 7 additions & 11 deletions tests/unittest_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit a6d7ffc

Please sign in to comment.