Skip to content

Commit 177cf88

Browse files
authored
Merge pull request #12032 from SpecLad/freeze-setuptools
2 parents 4734c4c + 7a69c00 commit 177cf88

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

news/4256.removal.rst

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``freeze`` no longer excludes the ``setuptools``, ``distribute``, and ``wheel``
2+
from the output when running on Python 3.12 or later, where they are not
3+
included in a virtual environment by default. Use ``--exclude`` if you wish to
4+
exclude any of these packages.

src/pip/_internal/commands/freeze.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import sys
22
from optparse import Values
3-
from typing import List
3+
from typing import AbstractSet, List
44

55
from pip._internal.cli import cmdoptions
66
from pip._internal.cli.base_command import Command
77
from pip._internal.cli.status_codes import SUCCESS
88
from pip._internal.operations.freeze import freeze
99
from pip._internal.utils.compat import stdlib_pkgs
1010

11-
DEV_PKGS = {"pip", "setuptools", "distribute", "wheel"}
11+
12+
def _should_suppress_build_backends() -> bool:
13+
return sys.version_info < (3, 12)
14+
15+
16+
def _dev_pkgs() -> AbstractSet[str]:
17+
pkgs = {"pip"}
18+
19+
if _should_suppress_build_backends():
20+
pkgs |= {"setuptools", "distribute", "wheel"}
21+
22+
return pkgs
1223

1324

1425
class FreezeCommand(Command):
@@ -61,7 +72,7 @@ def add_options(self) -> None:
6172
action="store_true",
6273
help=(
6374
"Do not skip these packages in the output:"
64-
" {}".format(", ".join(DEV_PKGS))
75+
" {}".format(", ".join(_dev_pkgs()))
6576
),
6677
)
6778
self.cmd_opts.add_option(
@@ -77,7 +88,7 @@ def add_options(self) -> None:
7788
def run(self, options: Values, args: List[str]) -> int:
7889
skip = set(stdlib_pkgs)
7990
if not options.freeze_all:
80-
skip.update(DEV_PKGS)
91+
skip.update(_dev_pkgs())
8192

8293
if options.excludes:
8394
skip.update(options.excludes)

tests/functional/test_freeze.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,49 @@ def test_basic_freeze(script: PipTestEnvironment) -> None:
8888

8989

9090
def test_freeze_with_pip(script: PipTestEnvironment) -> None:
91-
"""Test pip shows itself"""
91+
"""Test that pip shows itself only when --all is used"""
92+
result = script.pip("freeze")
93+
assert "pip==" not in result.stdout
9294
result = script.pip("freeze", "--all")
9395
assert "pip==" in result.stdout
9496

9597

98+
def test_freeze_with_setuptools(script: PipTestEnvironment) -> None:
99+
"""
100+
Test that pip shows setuptools only when --all is used
101+
or _should_suppress_build_backends() returns false
102+
"""
103+
104+
result = script.pip("freeze", "--all")
105+
assert "setuptools==" in result.stdout
106+
107+
(script.site_packages_path / "mock.pth").write_text("import mock\n")
108+
109+
(script.site_packages_path / "mock.py").write_text(
110+
textwrap.dedent(
111+
"""\
112+
import pip._internal.commands.freeze as freeze
113+
freeze._should_suppress_build_backends = lambda: False
114+
"""
115+
)
116+
)
117+
118+
result = script.pip("freeze")
119+
assert "setuptools==" in result.stdout
120+
121+
(script.site_packages_path / "mock.py").write_text(
122+
textwrap.dedent(
123+
"""\
124+
import pip._internal.commands.freeze as freeze
125+
freeze._should_suppress_build_backends = lambda: True
126+
"""
127+
)
128+
)
129+
130+
result = script.pip("freeze")
131+
assert "setuptools==" not in result.stdout
132+
133+
96134
def test_exclude_and_normalization(script: PipTestEnvironment, tmpdir: Path) -> None:
97135
req_path = wheel.make_wheel(name="Normalizable_Name", version="1.0").save_to_dir(
98136
tmpdir

0 commit comments

Comments
 (0)