Skip to content

Commit

Permalink
Merge pull request #12032 from SpecLad/freeze-setuptools
Browse files Browse the repository at this point in the history
  • Loading branch information
pradyunsg authored Jul 7, 2023
2 parents 4734c4c + 7a69c00 commit 177cf88
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
4 changes: 4 additions & 0 deletions news/4256.removal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``freeze`` no longer excludes the ``setuptools``, ``distribute``, and ``wheel``
from the output when running on Python 3.12 or later, where they are not
included in a virtual environment by default. Use ``--exclude`` if you wish to
exclude any of these packages.
19 changes: 15 additions & 4 deletions src/pip/_internal/commands/freeze.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import sys
from optparse import Values
from typing import List
from typing import AbstractSet, List

from pip._internal.cli import cmdoptions
from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import SUCCESS
from pip._internal.operations.freeze import freeze
from pip._internal.utils.compat import stdlib_pkgs

DEV_PKGS = {"pip", "setuptools", "distribute", "wheel"}

def _should_suppress_build_backends() -> bool:
return sys.version_info < (3, 12)


def _dev_pkgs() -> AbstractSet[str]:
pkgs = {"pip"}

if _should_suppress_build_backends():
pkgs |= {"setuptools", "distribute", "wheel"}

return pkgs


class FreezeCommand(Command):
Expand Down Expand Up @@ -61,7 +72,7 @@ def add_options(self) -> None:
action="store_true",
help=(
"Do not skip these packages in the output:"
" {}".format(", ".join(DEV_PKGS))
" {}".format(", ".join(_dev_pkgs()))
),
)
self.cmd_opts.add_option(
Expand All @@ -77,7 +88,7 @@ def add_options(self) -> None:
def run(self, options: Values, args: List[str]) -> int:
skip = set(stdlib_pkgs)
if not options.freeze_all:
skip.update(DEV_PKGS)
skip.update(_dev_pkgs())

if options.excludes:
skip.update(options.excludes)
Expand Down
40 changes: 39 additions & 1 deletion tests/functional/test_freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,49 @@ def test_basic_freeze(script: PipTestEnvironment) -> None:


def test_freeze_with_pip(script: PipTestEnvironment) -> None:
"""Test pip shows itself"""
"""Test that pip shows itself only when --all is used"""
result = script.pip("freeze")
assert "pip==" not in result.stdout
result = script.pip("freeze", "--all")
assert "pip==" in result.stdout


def test_freeze_with_setuptools(script: PipTestEnvironment) -> None:
"""
Test that pip shows setuptools only when --all is used
or _should_suppress_build_backends() returns false
"""

result = script.pip("freeze", "--all")
assert "setuptools==" in result.stdout

(script.site_packages_path / "mock.pth").write_text("import mock\n")

(script.site_packages_path / "mock.py").write_text(
textwrap.dedent(
"""\
import pip._internal.commands.freeze as freeze
freeze._should_suppress_build_backends = lambda: False
"""
)
)

result = script.pip("freeze")
assert "setuptools==" in result.stdout

(script.site_packages_path / "mock.py").write_text(
textwrap.dedent(
"""\
import pip._internal.commands.freeze as freeze
freeze._should_suppress_build_backends = lambda: True
"""
)
)

result = script.pip("freeze")
assert "setuptools==" not in result.stdout


def test_exclude_and_normalization(script: PipTestEnvironment, tmpdir: Path) -> None:
req_path = wheel.make_wheel(name="Normalizable_Name", version="1.0").save_to_dir(
tmpdir
Expand Down

0 comments on commit 177cf88

Please sign in to comment.