Skip to content

Commit 82cfebe

Browse files
authored
Merge pull request #10167 from maresb/gone-in-message
2 parents 174e0f6 + dbf994e commit 82cfebe

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

news/10165.trivial.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Add a ``feature_flag`` optional kwarg to the ``deprecated()`` function
2+
``pip._internal.utils.deprecation:deprecated``. Also formulate a corresponding canned
3+
message which suggests using the ``--use-feature={feature_flag}`` to test upcoming
4+
behavior.

src/pip/_internal/utils/deprecation.py

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
from pip import __version__ as current_version
1212

1313
DEPRECATION_MSG_PREFIX = "DEPRECATION: "
14+
DEPRECATION_MESSAGE = DEPRECATION_MSG_PREFIX + "{reason}"
15+
GONE_IN_MESSAGE_FUTURE = "pip {gone_in} will enforce this behavior change."
16+
GONE_IN_MESSAGE_PAST = "This behavior change has been enforced since pip {gone_in}."
17+
REPLACEMENT_MESSAGE = "A possible replacement is {replacement}."
18+
FEATURE_FLAG_MESSAGE = (
19+
"You can temporarily use the flag --use-feature={feature_flag} "
20+
"to test the upcoming behavior."
21+
)
22+
ISSUE_MESSAGE = "Discussion can be found at https://github.com/pypa/pip/issues/{issue}."
1423

1524

1625
class PipDeprecationWarning(Warning):
@@ -56,49 +65,63 @@ def deprecated(
5665
reason: str,
5766
replacement: Optional[str],
5867
gone_in: Optional[str],
68+
feature_flag: Optional[str] = None,
5969
issue: Optional[int] = None,
6070
) -> None:
6171
"""Helper to deprecate existing functionality.
6272
6373
reason:
6474
Textual reason shown to the user about why this functionality has
65-
been deprecated.
75+
been deprecated. Should be a complete sentence.
6676
replacement:
6777
Textual suggestion shown to the user about what alternative
6878
functionality they can use.
6979
gone_in:
7080
The version of pip does this functionality should get removed in.
71-
Raises errors if pip's current version is greater than or equal to
81+
Raises an error if pip's current version is greater than or equal to
7282
this.
83+
feature_flag:
84+
Command-line flag of the form --use-feature={feature_flag} for testing
85+
upcoming functionality.
7386
issue:
7487
Issue number on the tracker that would serve as a useful place for
7588
users to find related discussion and provide feedback.
7689
7790
Always pass replacement, gone_in and issue as keyword arguments for clarity
7891
at the call site.
7992
"""
80-
93+
# Determine whether or not the feature is already gone in this version.
94+
is_gone = gone_in is not None and parse(current_version) >= parse(gone_in)
95+
# Allow variable substitutions within the "reason" variable.
96+
formatted_reason = reason.format(gone_in=gone_in)
8197
# Construct a nice message.
8298
# This is eagerly formatted as we want it to get logged as if someone
8399
# typed this entire message out.
100+
formatted_deprecation_message = DEPRECATION_MESSAGE.format(reason=formatted_reason)
101+
gone_in_message = GONE_IN_MESSAGE_PAST if is_gone else GONE_IN_MESSAGE_FUTURE
102+
formatted_gone_in_message = (
103+
gone_in_message.format(gone_in=gone_in) if gone_in else None
104+
)
105+
formatted_replacement_message = (
106+
REPLACEMENT_MESSAGE.format(replacement=replacement) if replacement else None
107+
)
108+
formatted_feature_flag_message = (
109+
None
110+
if is_gone or not feature_flag
111+
else FEATURE_FLAG_MESSAGE.format(feature_flag=feature_flag)
112+
)
113+
formatted_issue_message = ISSUE_MESSAGE.format(issue=issue) if issue else None
84114
sentences = [
85-
(reason, DEPRECATION_MSG_PREFIX + "{}"),
86-
(gone_in, "pip {} will remove support for this functionality."),
87-
(replacement, "A possible replacement is {}."),
88-
(
89-
issue,
90-
(
91-
"You can find discussion regarding this at "
92-
"https://github.com/pypa/pip/issues/{}."
93-
),
94-
),
115+
formatted_deprecation_message,
116+
formatted_gone_in_message,
117+
formatted_replacement_message,
118+
formatted_feature_flag_message,
119+
formatted_issue_message,
95120
]
96-
message = " ".join(
97-
template.format(val) for val, template in sentences if val is not None
98-
)
121+
message = " ".join(sentence for sentence in sentences if sentence)
99122

100-
# Raise as an error if it has to be removed.
101-
if gone_in is not None and parse(current_version) >= parse(gone_in):
123+
# Raise as an error if the functionality is gone.
124+
if is_gone:
102125
raise PipDeprecationWarning(message)
103-
104-
warnings.warn(message, category=PipDeprecationWarning, stacklevel=2)
126+
else:
127+
warnings.warn(message, category=PipDeprecationWarning, stacklevel=2)

tests/unit/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,9 +934,9 @@ def test_deprecated_message_reads_well():
934934

935935
assert message == (
936936
"DEPRECATION: Stop doing this! "
937-
"pip 1.0 will remove support for this functionality. "
937+
"This behavior change has been enforced since pip 1.0. "
938938
"A possible replacement is to be nicer. "
939-
"You can find discussion regarding this at "
939+
"Discussion can be found at "
940940
"https://github.com/pypa/pip/issues/100000."
941941
)
942942

0 commit comments

Comments
 (0)