@@ -29,6 +29,19 @@ def check_mypy_run(cmd_line: List[str],
29
29
assert returncode == expected_returncode , returncode
30
30
31
31
32
+ def is_in_venv () -> bool :
33
+ """Returns whether we are running inside a venv.
34
+
35
+ Based on https://stackoverflow.com/a/42580137.
36
+
37
+ """
38
+ if hasattr (sys , 'real_prefix' ):
39
+ return True
40
+ else :
41
+ # https://github.com/python/typeshed/pull/2047
42
+ return hasattr (sys , 'base_prefix' ) and sys .base_prefix != sys .prefix # type: ignore
43
+
44
+
32
45
class TestPEP561 (TestCase ):
33
46
@contextmanager
34
47
def install_package (self , pkg : str ,
@@ -38,9 +51,7 @@ def install_package(self, pkg: str,
38
51
install_cmd = [python_executable , '-m' , 'pip' , 'install' , '.' ]
39
52
# if we aren't in a virtualenv, install in the
40
53
# user package directory so we don't need sudo
41
- # In a virtualenv, real_prefix is patched onto
42
- # sys
43
- if not hasattr (sys , 'real_prefix' ) or python_executable != sys .executable :
54
+ if not is_in_venv () or python_executable != sys .executable :
44
55
install_cmd .append ('--user' )
45
56
returncode , lines = run_command (install_cmd , cwd = working_dir )
46
57
if returncode != 0 :
0 commit comments