Skip to content

Commit 27878a5

Browse files
committed
Refactor legacy_install_reason
1 parent 8dc39f0 commit 27878a5

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

src/pip/_internal/commands/install.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from pip._internal.req import install_given_reqs
3030
from pip._internal.req.req_install import InstallRequirement
3131
from pip._internal.utils.compat import WINDOWS
32+
from pip._internal.utils.deprecation import LegacyInstallReasonFailedBdistWheel
3233
from pip._internal.utils.distutils_args import parse_distutils_args
3334
from pip._internal.utils.filesystem import test_writable_dir
3435
from pip._internal.utils.logging import getLogger
@@ -440,7 +441,7 @@ def run(self, options: Values, args: List[str]) -> int:
440441
# those.
441442
for r in build_failures:
442443
if not r.use_pep517:
443-
r.legacy_install_reason = 8368
444+
r.legacy_install_reason = LegacyInstallReasonFailedBdistWheel
444445

445446
to_install = resolver.get_installation_order(requirement_set)
446447

src/pip/_internal/req/req_install.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from pip._internal.operations.install.wheel import install_wheel
4343
from pip._internal.pyproject import load_pyproject_toml, make_pyproject_path
4444
from pip._internal.req.req_uninstall import UninstallPathSet
45-
from pip._internal.utils.deprecation import deprecated
45+
from pip._internal.utils.deprecation import LegacyInstallReason, deprecated
4646
from pip._internal.utils.direct_url_helpers import (
4747
direct_url_for_editable,
4848
direct_url_from_link,
@@ -96,7 +96,7 @@ def __init__(
9696
self.constraint = constraint
9797
self.editable = editable
9898
self.permit_editable_wheels = permit_editable_wheels
99-
self.legacy_install_reason: Optional[int] = None
99+
self.legacy_install_reason: Optional[LegacyInstallReason] = None
100100

101101
# source_dir is the local directory where the linked requirement is
102102
# located, or unpacked. In case unpacking is needed, creating and
@@ -836,18 +836,12 @@ def install(
836836

837837
self.install_succeeded = success
838838

839-
if success and self.legacy_install_reason == 8368:
840-
deprecated(
841-
reason=(
842-
"{} was installed using the legacy 'setup.py install' "
843-
"method, because a wheel could not be built for it.".format(
844-
self.name
845-
)
846-
),
847-
replacement="to fix the wheel build issue reported above",
848-
gone_in=None,
849-
issue=8368,
850-
)
839+
if (
840+
success
841+
and self.legacy_install_reason is not None
842+
and self.legacy_install_reason.emit_after_success
843+
):
844+
self.legacy_install_reason.emit_deprecation(self.name)
851845

852846

853847
def check_invalid_constraint_type(req: InstallRequirement) -> str:

src/pip/_internal/utils/deprecation.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,44 @@ def deprecated(
118118
raise PipDeprecationWarning(message)
119119

120120
warnings.warn(message, category=PipDeprecationWarning, stacklevel=2)
121+
122+
123+
class LegacyInstallReason:
124+
def __init__(
125+
self,
126+
reason: str,
127+
replacement: Optional[str],
128+
gone_in: Optional[str],
129+
feature_flag: Optional[str] = None,
130+
issue: Optional[int] = None,
131+
emit_after_success: bool = False,
132+
emit_before_install: bool = False,
133+
):
134+
self._reason = reason
135+
self._replacement = replacement
136+
self._gone_in = gone_in
137+
self._feature_flag = feature_flag
138+
self._issue = issue
139+
self.emit_after_success = emit_after_success
140+
self.emit_before_install = emit_before_install
141+
142+
def emit_deprecation(self, name: str) -> None:
143+
deprecated(
144+
reason=self._reason.format(name=name),
145+
replacement=self._replacement,
146+
gone_in=self._gone_in,
147+
feature_flag=self._feature_flag,
148+
issue=self._issue,
149+
)
150+
151+
152+
LegacyInstallReasonFailedBdistWheel = LegacyInstallReason(
153+
reason=(
154+
"{name} was installed using the legacy 'setup.py install' "
155+
"method, because a wheel could not be built for it."
156+
),
157+
replacement="to fix the wheel build issue reported above",
158+
gone_in=None,
159+
issue=8368,
160+
emit_after_success=True,
161+
)

0 commit comments

Comments
 (0)