Skip to content

Commit d23d12b

Browse files
committed
Avoid formatted_* variables, by using a loop
This reduces the boilerplate around each statement, surfacing the logic of message formatting more clearly.
1 parent fe9ba4d commit d23d12b

File tree

1 file changed

+27
-35
lines changed

1 file changed

+27
-35
lines changed

src/pip/_internal/utils/deprecation.py

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@
1111
from pip import __version__ as current_version # NOTE: tests patch this name.
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}."
2314

2415

2516
class PipDeprecationWarning(Warning):
@@ -91,33 +82,34 @@ def deprecated(
9182

9283
# Determine whether or not the feature is already gone in this version.
9384
is_gone = gone_in is not None and parse(current_version) >= parse(gone_in)
94-
# Allow variable substitutions within the "reason" variable.
95-
formatted_reason = reason.format(gone_in=gone_in)
96-
# Construct a nice message.
97-
# This is eagerly formatted as we want it to get logged as if someone
98-
# typed this entire message out.
99-
formatted_deprecation_message = DEPRECATION_MESSAGE.format(reason=formatted_reason)
100-
gone_in_message = GONE_IN_MESSAGE_PAST if is_gone else GONE_IN_MESSAGE_FUTURE
101-
formatted_gone_in_message = (
102-
gone_in_message.format(gone_in=gone_in) if gone_in else None
103-
)
104-
formatted_replacement_message = (
105-
REPLACEMENT_MESSAGE.format(replacement=replacement) if replacement else None
106-
)
107-
formatted_feature_flag_message = (
108-
None
109-
if is_gone or not feature_flag
110-
else FEATURE_FLAG_MESSAGE.format(feature_flag=feature_flag)
111-
)
112-
formatted_issue_message = ISSUE_MESSAGE.format(issue=issue) if issue else None
113-
sentences = [
114-
formatted_deprecation_message,
115-
formatted_gone_in_message,
116-
formatted_replacement_message,
117-
formatted_feature_flag_message,
118-
formatted_issue_message,
85+
86+
message_parts = [
87+
(reason, f"{DEPRECATION_MSG_PREFIX}{{}}"),
88+
(
89+
gone_in,
90+
"pip {} will enforce this behaviour change."
91+
if not is_gone
92+
else "This behavior change has been enforced since pip {}.",
93+
),
94+
(
95+
replacement,
96+
"A possible replacement is {}.",
97+
),
98+
(
99+
feature_flag,
100+
"You can use the flag --use-feature={} to test the upcoming behaviour.",
101+
),
102+
(
103+
issue,
104+
"Discussion can be found at https://github.com/pypa/pip/issues/{}.",
105+
),
119106
]
120-
message = " ".join(sentence for sentence in sentences if sentence)
107+
108+
message = " ".join(
109+
format_str.format(value)
110+
for value, format_str in message_parts
111+
if value is not None
112+
)
121113

122114
# Raise as an error if this behaviour is no longer supported.
123115
if is_gone:

0 commit comments

Comments
 (0)