Skip to content

Commit 11e37aa

Browse files
committed
Separate default and ad-hoc environment APIs
pkg_resources performs annoying caching that needs to be worked around in some parts of pip. This makes it easier to represent the difference between environments backend by WorkingSet() and working_set.
1 parent 349bb73 commit 11e37aa

File tree

6 files changed

+34
-12
lines changed

6 files changed

+34
-12
lines changed

src/pip/_internal/metadata/__init__.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,27 @@
66
from .base import BaseEnvironment
77

88

9-
def get_environment(paths=None):
9+
def get_default_environment():
10+
# type: () -> BaseEnvironment
11+
"""Get the default representation for the current environment.
12+
13+
This returns an Environment instance from the chosen backend. The default
14+
Environment instance should be built from ``sys.path`` and may use caching
15+
to share instance state accorss calls.
16+
"""
17+
from .pkg_resources import Environment
18+
19+
return Environment.default()
20+
21+
22+
def get_environment(paths):
1023
# type: (Optional[List[str]]) -> BaseEnvironment
24+
"""Get a representation of the environment specified by ``paths``.
25+
26+
This returns an Environment instance from the chosen backend based on the
27+
given import paths. The backend must build a fresh instance representing
28+
the state of installed distributions when this function is called.
29+
"""
1130
from .pkg_resources import Environment
1231

13-
if paths is None:
14-
return Environment.default()
1532
return Environment.from_paths(paths)

src/pip/_internal/metadata/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def default(cls):
4242

4343
@classmethod
4444
def from_paths(cls, paths):
45-
# type: (List[str]) -> BaseEnvironment
45+
# type: (Optional[List[str]]) -> BaseEnvironment
4646
raise NotImplementedError()
4747

4848
def get_distribution(self, name):

src/pip/_internal/metadata/pkg_resources.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def default(cls):
5454

5555
@classmethod
5656
def from_paths(cls, paths):
57-
# type: (List[str]) -> BaseEnvironment
57+
# type: (Optional[List[str]]) -> BaseEnvironment
5858
return cls(pkg_resources.WorkingSet(paths))
5959

6060
def _search_distribution(self, name):

src/pip/_internal/self_outdated_check.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from pip._internal.index.collector import LinkCollector
1212
from pip._internal.index.package_finder import PackageFinder
13-
from pip._internal.metadata import get_environment
13+
from pip._internal.metadata import get_default_environment
1414
from pip._internal.models.selection_prefs import SelectionPreferences
1515
from pip._internal.utils.filesystem import adjacent_tmp_file, check_path_owner, replace
1616
from pip._internal.utils.misc import ensure_dir, get_installed_version
@@ -103,7 +103,7 @@ def was_installed_by_pip(pkg):
103103
This is used not to display the upgrade message when pip is in fact
104104
installed by system package manager, such as dnf on Fedora.
105105
"""
106-
dist = get_environment().get_distribution(pkg)
106+
dist = get_default_environment().get_distribution(pkg)
107107
return dist is not None and "pip" == dist.installer
108108

109109

src/pip/_internal/utils/misc.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,14 @@ def get_installed_distributions(
405405
406406
Left for compatibility until direct pkg_resources uses are refactored out.
407407
"""
408-
from pip._internal.metadata import get_environment
408+
from pip._internal.metadata import get_default_environment, get_environment
409409
from pip._internal.metadata.pkg_resources import Distribution as _Dist
410-
dists = get_environment(paths).iter_installed_distributions(
410+
411+
if paths is None:
412+
env = get_default_environment()
413+
else:
414+
env = get_environment(paths)
415+
dists = env.iter_installed_distributions(
411416
local_only=local_only,
412417
skip=skip,
413418
include_editables=include_editables,
@@ -426,9 +431,9 @@ def get_distribution(req_name):
426431
427432
Left for compatibility until direct pkg_resources uses are refactored out.
428433
"""
429-
from pip._internal.metadata import get_environment
434+
from pip._internal.metadata import get_default_environment
430435
from pip._internal.metadata.pkg_resources import Distribution as _Dist
431-
dist = get_environment().get_distribution(req_name)
436+
dist = get_default_environment().get_distribution(req_name)
432437
if dist is None:
433438
return None
434439
return cast(_Dist, dist)._dist

tests/unit/test_self_check_outdated.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def test_pip_self_version_check(monkeypatch, stored_time, installed_ver,
105105
pretend.call_recorder(lambda *a, **kw: None))
106106
monkeypatch.setattr(logger, 'debug',
107107
pretend.call_recorder(lambda s, exc_info=None: None))
108-
monkeypatch.setattr(self_outdated_check, 'get_environment',
108+
monkeypatch.setattr(self_outdated_check, 'get_default_environment',
109109
lambda: MockEnvironment(installer))
110110

111111
fake_state = pretend.stub(

0 commit comments

Comments
 (0)