Skip to content
Closed
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
34 changes: 29 additions & 5 deletions Doc/tools/extensions/changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,31 @@ def run(self) -> list[Node]:
return super().run()


class TerribleHashableDictHack:
def __init__(self, deprecated, removed):
self.deprecated = deprecated
self.removed = removed

def __hash__(self):
return hash((self.deprecated, self.removed))

def __getitem__(self, name):
return getattr(self, name)


class FormatToPctTranslator:
def __getitem__(self, name):
return f'%({name})s'


class DeprecatedRemoved(VersionChange):
required_arguments = 2

_deprecated_label = sphinx_gettext(
"Deprecated since version %s, will be removed in version %s"
"Deprecated since version {deprecated}, will be removed in version {removed}"
)
_removed_label = sphinx_gettext(
"Deprecated since version %s, removed in version %s"
"Deprecated since version {deprecated}, removed in version {removed}"
)

def run(self) -> list[Node]:
Expand All @@ -54,16 +71,23 @@ def run(self) -> list[Node]:
raise ValueError(
"deprecated-removed:: second argument cannot be `next`"
)
self.arguments[0] = version_deprecated, version_removed
self.arguments[0] = TerribleHashableDictHack(
Copy link
Member Author

Choose a reason for hiding this comment

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

Another option that avoids this hack is to check the order of {deprecated} and {removed} in the string, adjust the order of the tuple accordingly, and replace each placeholder with %s. That precludes the possibility of a translation needing to repeat either placeholder, but I don't know if that's a realistic need.

Note that the not-a-dict hack is necessary for this route due to dict not being hashable, raising a SphinxParallelError.

deprecated=version_deprecated,
removed=version_removed,
)

# Set the label based on if we have reached the removal version
current_version = tuple(map(int, self.config.version.split(".")))
removed_version = tuple(map(int, version_removed.split(".")))
if current_version < removed_version:
versionlabels[self.name] = self._deprecated_label
versionlabels[self.name] = self._deprecated_label.format_map(
FormatToPctTranslator()
)
versionlabel_classes[self.name] = "deprecated"
else:
versionlabels[self.name] = self._removed_label
versionlabels[self.name] = self._removed_label.format_map(
FormatToPctTranslator()
)
versionlabel_classes[self.name] = "removed"
try:
return super().run()
Expand Down
Loading