diff --git a/mypy/test/test_diff_cache.py b/mypy/test/test_diff_cache.py index 5c632c1153c6..602cf7a6742e 100644 --- a/mypy/test/test_diff_cache.py +++ b/mypy/test/test_diff_cache.py @@ -29,7 +29,8 @@ def test_diff_cache_produces_valid_json(self) -> None: src_dir = tempfile.mkdtemp() output_file = os.path.join(tempfile.mkdtemp(), "diff.json") env = os.environ.copy() - env["PYTHONPATH"] = PREFIX + existing = env.get("PYTHONPATH") + env["PYTHONPATH"] = PREFIX + (os.pathsep + existing if existing else "") try: cache1 = os.path.join(src_dir, "cache1") cache2 = os.path.join(src_dir, "cache2") diff --git a/mypy/test/testcheck.py b/mypy/test/testcheck.py index 377b10ad86d2..d08c8f49ec54 100644 --- a/mypy/test/testcheck.py +++ b/mypy/test/testcheck.py @@ -189,7 +189,11 @@ def run_case_once( # Make sure we are running tests with current worktree files, *not* with # an installed version of mypy. root_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) - worker_env["PYTHONPATH"] = os.pathsep.join([root_dir, plugin_dir]) + new_python_path = os.pathsep.join([root_dir, plugin_dir]) + existing = worker_env.get("PYTHONPATH") + worker_env["PYTHONPATH"] = new_python_path + ( + os.pathsep + existing if existing else "" + ) worker_env["MYPY_TEST_PREFIX"] = root_dir worker_env["MYPY_ALT_LIB_PATH"] = test_temp_dir diff --git a/mypy/test/testcmdline.py b/mypy/test/testcmdline.py index ced443e94f3d..505ea4e29ae8 100644 --- a/mypy/test/testcmdline.py +++ b/mypy/test/testcmdline.py @@ -74,9 +74,12 @@ def test_python_cmdline(testcase: DataDrivenTestCase, step: int) -> None: env.pop("COLUMNS", None) extra_path = os.path.join(os.path.abspath(test_temp_dir), "pypath") # Use the same mypy installation as pytest, including the compiled wheel in CI. + existing = env.get("PYTHONPATH") env["PYTHONPATH"] = os.path.dirname(os.path.abspath(mypy.__path__[0])) if os.path.isdir(extra_path): env["PYTHONPATH"] += os.pathsep + extra_path + if existing: + env["PYTHONPATH"] += os.pathsep + existing cwd = os.path.join(test_temp_dir, custom_cwd or "") args = [arg.replace("$CWD", os.path.abspath(cwd)) for arg in args] process = subprocess.Popen( diff --git a/mypy/test/testdaemon.py b/mypy/test/testdaemon.py index 7115e682e60d..4858d9d7d9e3 100644 --- a/mypy/test/testdaemon.py +++ b/mypy/test/testdaemon.py @@ -84,7 +84,8 @@ def run_cmd(input: str) -> tuple[int, str]: if input.startswith("mypy "): input = sys.executable + " -m" + input env = os.environ.copy() - env["PYTHONPATH"] = PREFIX + existing = env.get("PYTHONPATH") + env["PYTHONPATH"] = PREFIX + (os.pathsep + existing if existing else "") try: output = subprocess.check_output( input, shell=True, stderr=subprocess.STDOUT, text=True, cwd=test_temp_dir, env=env diff --git a/mypyc/test/librt_cache.py b/mypyc/test/librt_cache.py index 751df36bf129..de95a8e6a1cd 100644 --- a/mypyc/test/librt_cache.py +++ b/mypyc/test/librt_cache.py @@ -234,7 +234,7 @@ def run_with_librt( librt_path = get_librt_path(experimental, opt_level=opt_level) # Prepend librt path to PYTHONPATH env = os.environ.copy() - existing = env.get("PYTHONPATH", "") + existing = env.get("PYTHONPATH") env["PYTHONPATH"] = librt_path + (os.pathsep + existing if existing else "") return subprocess.run( diff --git a/mypyc/test/test_commandline.py b/mypyc/test/test_commandline.py index f66ca2ec8ff0..0e0e4ae617cb 100644 --- a/mypyc/test/test_commandline.py +++ b/mypyc/test/test_commandline.py @@ -44,7 +44,8 @@ def run_case(self, testcase: DataDrivenTestCase) -> None: f.write(text) env = os.environ.copy() - env["PYTHONPATH"] = base_path + existing = env.get("PYTHONPATH") + env["PYTHONPATH"] = base_path + (os.pathsep + existing if existing else "") out = b"" try: diff --git a/mypyc/test/test_misc.py b/mypyc/test/test_misc.py index 3da037231320..7fc01c22e712 100644 --- a/mypyc/test/test_misc.py +++ b/mypyc/test/test_misc.py @@ -188,7 +188,8 @@ def test_c_generation_does_not_import_setuptools(self) -> None: with open(os.path.join(tmpdir, "a.py"), "w") as f: f.write("def f(x: int) -> int:\n return x + 1\n") env = os.environ.copy() - env["PYTHONPATH"] = PREFIX + os.pathsep + env.get("PYTHONPATH", "") + existing = env.get("PYTHONPATH") + env["PYTHONPATH"] = PREFIX + (os.pathsep + existing if existing else "") result = subprocess.run( [sys.executable, "-c", script], cwd=tmpdir, env=env, capture_output=True, text=True ) diff --git a/test-data/unit/README.md b/test-data/unit/README.md index aaf774d1b62f..853eb0e3c193 100644 --- a/test-data/unit/README.md +++ b/test-data/unit/README.md @@ -141,7 +141,7 @@ To control which tests are run and how, you can use the `-k` switch: You can also run the type checker for manual testing without installing it by setting up the Python module search path suitably: - export PYTHONPATH=$PWD + export PYTHONPATH=$PWD:$PYTHONPATH python3 -m mypy PROGRAM.py You will have to manually install the `typing` module if you're running Python