Repository: python/mypy

Issue:
mypy looks for modules outside of virtualenv
**Bug Report**

I have mypy installed inside of a virtualenv. To my surprise, mypy looked for modules outside of the virtualenv. It appears that this is the section of code responsible:

https://github.com/python/mypy/blob/a1863c90/mypy/pyinfo.py#L27-L29

The [docs for site.getusersitepackages()](https://docs.python.org/3/library/site.html#site.getusersitepackages) say this:

> To determine if the user-specific site-packages was added to sys.path ENABLE_USER_SITE should be used.

From my reading, the code section linked to above should check `site.ENABLE_USER_SITE` before adding `site.getusersitepackages()` to the returned list of paths.

As a workaround, I found that I can set `PYTHONUSERBASE` to some non-existent path, so that mypy won't look in a valid directory.

**To Reproduce**

1. Install mypy in virtualenv
2. Install package outside of virtualenv, e.g. `pip install --user types-PyYaml`
3. Run `mypy foo/`
4. Notice that mypy doesn't complain about missing stubs for `PyYaml` package
5. `mypy -v` also has this line of output:

       LOG:  Metadata fresh for yaml.dumper: file /home/knkski/.local/lib/python3.9/site-packages/yaml-stubs/dumper.pyi

**Expected Behavior**

mypy inside virtualenv would not look outside of the virtualenv for any packages

**Actual Behavior**

mypy inside virtualenv looked inside system user site packages folder

**Your Environment**

- Mypy version used: 0.910
- Mypy command-line flags: `mypy foo/`
- Mypy configuration options from `mypy.ini` (and other config files): N/A
- Python version used: 3.9.5
- Operating system and version: Ubuntu 21.04

mypy looks for modules outside of virtualenv
**Bug Report**

I have mypy installed inside of a virtualenv. To my surprise, mypy looked for modules outside of the virtualenv. It appears that this is the section of code responsible:

https://github.com/python/mypy/blob/a1863c90/mypy/pyinfo.py#L27-L29

The [docs for site.getusersitepackages()](https://docs.python.org/3/library/site.html#site.getusersitepackages) say this:

> To determine if the user-specific site-packages was added to sys.path ENABLE_USER_SITE should be used.

From my reading, the code section linked to above should check `site.ENABLE_USER_SITE` before adding `site.getusersitepackages()` to the returned list of paths.

As a workaround, I found that I can set `PYTHONUSERBASE` to some non-existent path, so that mypy won't look in a valid directory.

**To Reproduce**

1. Install mypy in virtualenv
2. Install package outside of virtualenv, e.g. `pip install --user types-PyYaml`
3. Run `mypy foo/`
4. Notice that mypy doesn't complain about missing stubs for `PyYaml` package
5. `mypy -v` also has this line of output:

       LOG:  Metadata fresh for yaml.dumper: file /home/knkski/.local/lib/python3.9/site-packages/yaml-stubs/dumper.pyi

**Expected Behavior**

mypy inside virtualenv would not look outside of the virtualenv for any packages

**Actual Behavior**

mypy inside virtualenv looked inside system user site packages folder

**Your Environment**

- Mypy version used: 0.910
- Mypy command-line flags: `mypy foo/`
- Mypy configuration options from `mypy.ini` (and other config files): N/A
- Python version used: 3.9.5
- Operating system and version: Ubuntu 21.04



Discussion:
Hm, would you like to submit a fix for this?
Hm, would you like to submit a fix for this?

Resolving patch:
diff --git a/mypy/pyinfo.py b/mypy/pyinfo.py
--- a/mypy/pyinfo.py
+++ b/mypy/pyinfo.py
@@ -24,12 +24,16 @@ def getprefixes():
 
 def getsitepackages():
     # type: () -> List[str]
-    if hasattr(site, 'getusersitepackages') and hasattr(site, 'getsitepackages'):
-        user_dir = site.getusersitepackages()
-        return site.getsitepackages() + [user_dir]
+    res = []
+    if hasattr(site, 'getsitepackages'):
+        res.extend(site.getsitepackages())
+
+        if hasattr(site, 'getsitepackages') and site.ENABLE_USER_SITE:
+            res.insert(0, site.getusersitepackages())
     else:
         from distutils.sysconfig import get_python_lib
-        return [get_python_lib()]
+        res = [get_python_lib()]
+    return res
 
 
 if __name__ == '__main__':


Test patch: