Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't exclude setuptools, distribute & wheel from freeze output on Python 3.12+ #12032

Merged
merged 3 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()))
Comment on lines -64 to +75
Copy link
Member

@uranusjr uranusjr Jul 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor note, it’d be a good idea to add some sorting here so the order package appear is more stable. But honestly nobody cares that much about these help messages anyway so it’s fine to keep things as-is.

),
)
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also doesn't seem directly related to this PR. It's a sensible addition, but why is it included here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just making sure that pip is still hidden by default.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment mentioning pip should hide itself from freeze would be useful

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the test docstring say exactly that?

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