From 1988e86b883ace395ba7d18679b5a86f5472b077 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Wed, 11 Apr 2018 08:38:18 -0700 Subject: [PATCH 1/2] fix test failure on Mac Fixes #4883 --- mypy/test/testpep561.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/mypy/test/testpep561.py b/mypy/test/testpep561.py index 5d2e3a5963e3..77a5cdf7e906 100644 --- a/mypy/test/testpep561.py +++ b/mypy/test/testpep561.py @@ -29,6 +29,18 @@ def check_mypy_run(cmd_line: List[str], assert returncode == expected_returncode, returncode +def is_in_venv() -> bool: + """Returns whether we are running inside a venv. + + Based on https://stackoverflow.com/a/42580137. + + """ + if hasattr(sys, 'real_prefix'): + return True + else: + return hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix + + class TestPEP561(TestCase): @contextmanager def install_package(self, pkg: str, @@ -38,9 +50,7 @@ def install_package(self, pkg: str, install_cmd = [python_executable, '-m', 'pip', 'install', '.'] # if we aren't in a virtualenv, install in the # user package directory so we don't need sudo - # In a virtualenv, real_prefix is patched onto - # sys - if not hasattr(sys, 'real_prefix') or python_executable != sys.executable: + if not is_in_venv() or python_executable != sys.executable: install_cmd.append('--user') returncode, lines = run_command(install_cmd, cwd=working_dir) if returncode != 0: From 575767e8892cdcfa38c216963adbf332b6f97d26 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Wed, 11 Apr 2018 09:02:27 -0700 Subject: [PATCH 2/2] add type ignore --- mypy/test/testpep561.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mypy/test/testpep561.py b/mypy/test/testpep561.py index 77a5cdf7e906..7cb65aa487d1 100644 --- a/mypy/test/testpep561.py +++ b/mypy/test/testpep561.py @@ -38,7 +38,8 @@ def is_in_venv() -> bool: if hasattr(sys, 'real_prefix'): return True else: - return hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix + # https://github.com/python/typeshed/pull/2047 + return hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix # type: ignore class TestPEP561(TestCase):