Deduplicate deprecated label translation (#3430)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 63daae2, 8438798 · Browse files · 2 files·+30 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+30 added−32 removed
- other:+30 added−32 removed
Provenance
- Commit
3619563- Tree
b5c71d8- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
63daae2- Parent 2
8438798
2 files changed+30 added−32 removed
modified:src/click/core.py+26 added-32 removed
91 unmodified lines92 93 raise RuntimeError(message)94 95 96+def _format_deprecated_label(deprecated: bool | str) -> str:97+ """Return the parenthesized deprecation label shown in help text."""98+ label = _("deprecated").upper()99+ if isinstance(deprecated, str):100+ return f"({label}: {deprecated})"101+ return f"({label})"102+103+104+def _format_deprecated_suffix(deprecated: bool | str) -> str:105+ """Return the trailing reason for a ``DeprecationWarning`` message,106+ prefixed with a space, or an empty string when no reason was given.107+ """108+ if isinstance(deprecated, str):109+ return f" {deprecated}"110+ return ""111+112+113 def batch(iterable: cabc.Iterable[V], batch_size: int) -> list[tuple[V, ...]]:114 return list(zip(*repeat(iter(iterable), batch_size), strict=False))115 116 1035 unmodified lines1152 else:1153 text = ""1154 1155 if self.deprecated:1139- localised_deprectated = _("deprecated").upper()1140- deprecated_message = (1141- f"({localised_deprectated}: {self.deprecated})"1142- if isinstance(self.deprecated, str)1143- else f"({localised_deprectated})"1144- )1145- text = f"{_(text)} {deprecated_message}"1156+ text = f"{_(text)} {_format_deprecated_label(self.deprecated)}"1157 1158 return text.strip()1159 1160 def format_help(self, ctx: Context, formatter: HelpFormatter) -> None:21 unmodified lines1182 else:1183 text = ""1184 1185 if self.deprecated:1175- localised_deprectated = _("deprecated").upper()1176- deprecated_message = (1177- f"({localised_deprectated}: {self.deprecated})"1178- if isinstance(self.deprecated, str)1179- else f"({localised_deprectated})"1180- )1181- text = f"{_(text)} {deprecated_message}"1186+ text = f"{_(text)} {_format_deprecated_label(self.deprecated)}"1187 1188 if text:1189 formatter.write_paragraph()1190 98 unmodified lines1289 """Given a context, this invokes the attached callback (if it exists)1290 in the right way.1291 """1292 if self.deprecated:1288- extra_message = (1289- f" {self.deprecated}" if isinstance(self.deprecated, str) else ""1290- )1293 message = _(1294 "DeprecationWarning: The command {name!r} is deprecated.{extra_message}"1293- ).format(name=self.name, extra_message=extra_message)1295+ ).format(1296+ name=self.name,1297+ extra_message=_format_deprecated_suffix(self.deprecated),1298+ )1299 echo(style(message, fg="red"), err=True)1300 1301 if self.callback is not None:1302 return ctx.invoke(self.callback, **ctx.params)1293 unmodified lines2596 self.deprecated2597 and value is not UNSET2598 and source < ParameterSource.DEFAULT_MAP2599 ):2595- extra_message = (2596- f" {self.deprecated}" if isinstance(self.deprecated, str) else ""2597- )2600 message = _(2601 "DeprecationWarning: The {param_type} {name!r} is deprecated."2602 "{extra_message}"2603 ).format(2604 param_type=self.param_type_name,2605 name=self.human_readable_name,2604- extra_message=extra_message,2606+ extra_message=_format_deprecated_suffix(self.deprecated),2607 )2608 echo(style(message, fg="red"), err=True)2609 2610 # Process the value through the parameter's type.171 unmodified lines2782 else:2783 prompt_text = prompt2784 2785 if deprecated:2784- deprecated_message = (2785- f"(DEPRECATED: {deprecated})"2786- if isinstance(deprecated, str)2787- else "(DEPRECATED)"2788- )2789- help = (2790- f"{help} {deprecated_message}"2791- if help is not None2792- else deprecated_message2793- )2786+ label = _format_deprecated_label(deprecated)2787+ help = f"{help} {label}" if help is not None else label2788 2789 self.prompt = prompt_text2790 self.confirmation_prompt = confirmation_prompt2791 self.prompt_required = prompt_required709 unmodified linesmodified:CHANGES.rst+4 added
55 unmodified lines56 - Show custom error messages from types when :func:`prompt` with57 ``hide_input=True`` fails validation, instead of always showing a58 generic message. Built-in type messages mask the input value.59 :issue:`2809` :pr:`3256`60+- Fix missing space between option help text and the ``(DEPRECATED)``61+ label, and localize the option label so it matches the command label.62+ The label and the ``DeprecationWarning`` reason suffix are now produced63+ by shared helpers. :pr:`3423`64 - Document short option stacking (``-abc`` is parsed as ``-a -b -c``) and65 clarify that multi-character short option names are not supported.66 :issue:`2779` :pr:`3431`67 1367 unmodified lines