Skip to content

Commit 3cbb57e

Browse files
committed
Do not append namespace packages to sys.path
1 parent bd9550d commit 3cbb57e

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

namespace_pkg/existing/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
2+
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
3+
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
4+
5+
from existing import my_func # type: ignore[attr-defined]
6+
7+
my_func()

namespace_pkg/something_else/__init__.py

Whitespace-only changes.

pylint/lint/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
from __future__ import annotations
66

77
import contextlib
8+
import os
89
import sys
910
import traceback
1011
from collections.abc import Iterator, Sequence
1112
from datetime import datetime
1213
from pathlib import Path
1314

15+
from astroid import modutils
16+
from astroid.interpreter._import.spec import ModuleType, find_spec
17+
1418
from pylint.config import PYLINT_HOME
1519
from pylint.lint.expand_modules import get_python_path
1620

@@ -77,6 +81,12 @@ def _patch_sys_path(args: Sequence[str]) -> list[str]:
7781
seen = set()
7882
for arg in args:
7983
path = get_python_path(arg)
84+
try:
85+
spec = find_spec(modutils.modpath_from_file(arg))
86+
if spec.type == ModuleType.PY_NAMESPACE:
87+
continue
88+
except ImportError:
89+
pass
8090
if path not in seen:
8191
changes.append(path)
8292
seen.add(path)

tests/lint/unittest_lint.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import argparse
1010
import os
1111
import re
12+
import subprocess
1213
import sys
1314
import tempfile
1415
from collections.abc import Iterable, Iterator
@@ -170,6 +171,16 @@ def test_more_args(fake_path, case):
170171
assert sys.path == fake_path
171172

172173

174+
def test_namespace_package_sys_path() -> None:
175+
"""Test that we do not append namespace packages to sys.path."""
176+
result = subprocess.run(
177+
[sys.executable, "-m", "pylint", "namespace_pkg/"],
178+
check=False,
179+
stdout=subprocess.PIPE,
180+
)
181+
assert "Module import itself" not in result.stdout.decode("utf-8")
182+
183+
173184
@pytest.fixture(scope="module")
174185
def disable():
175186
return ["I"]

0 commit comments

Comments
 (0)