From ea4c10e59bed54b2e587868caa4d6e75f9ea7a58 Mon Sep 17 00:00:00 2001 From: Mirko Lenz Date: Fri, 8 Nov 2024 08:20:19 +0100 Subject: [PATCH] fix(cli): migrate from multimethod to singledispatchmethod --- arguebuf/cli/translator.py | 34 +++++++++++++++++----------------- poetry.lock | 17 +++-------------- pyproject.toml | 3 +-- 3 files changed, 21 insertions(+), 33 deletions(-) diff --git a/arguebuf/cli/translator.py b/arguebuf/cli/translator.py index 4cf439b..79ec830 100644 --- a/arguebuf/cli/translator.py +++ b/arguebuf/cli/translator.py @@ -1,8 +1,8 @@ import logging from collections.abc import Iterable +from functools import singledispatchmethod from deepl.translator import Translator as DeepLTranslator -from multimethod import multimethod import arguebuf as ag @@ -31,50 +31,50 @@ def _deepl_translate(self, text: str | Iterable[str]) -> str | list[str]: else: return result.text - @multimethod - def translate(self): + @singledispatchmethod + def translate(self, arg): raise NotImplementedError() @translate.register - def _(self, text: str) -> str: - trans = self._deepl_translate(text) + def _(self, arg: str) -> str: + trans = self._deepl_translate(arg) assert isinstance(trans, str) return trans @translate.register - def _(self, texts: Iterable[str]) -> list[str]: - trans = self._deepl_translate(texts) + def _(self, arg: Iterable[str]) -> list[str]: + trans = self._deepl_translate(arg) assert isinstance(trans, list) return trans @translate.register - def _(self, graph: ag.Graph) -> None: + def _(self, arg: ag.Graph) -> None: original_resources = [ - resource.plain_text for resource in graph.resources.values() + resource.plain_text for resource in arg.resources.values() ] for resource, translation in zip( - graph.resources.values(), self.translate(original_resources), strict=True + arg.resources.values(), self.translate(original_resources), strict=True ): resource.text = translation references = [ atom.reference.plain_text - for atom in graph.atom_nodes.values() + for atom in arg.atom_nodes.values() if atom.reference is not None ] for atom, translation in zip( - graph.atom_nodes.values(), self.translate(references), strict=True + arg.atom_nodes.values(), self.translate(references), strict=True ): if atom.reference is not None: atom.reference.text = translation - atoms = [atom.plain_text for atom in graph.atom_nodes.values()] + atoms = [atom.plain_text for atom in arg.atom_nodes.values()] for atom, translation in zip( - graph.atom_nodes.values(), self.translate(atoms), strict=True + arg.atom_nodes.values(), self.translate(atoms), strict=True ): atom.text = translation @translate.register - def _(self, graphs: Iterable[ag.Graph]) -> None: - for graph in graphs: - self.translate(graph) + def _(self, arg: Iterable[ag.Graph]) -> None: + for elem in arg: + self.translate(elem) diff --git a/poetry.lock b/poetry.lock index a21e7ec..f2168ae 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.4 and should not be changed by hand. [[package]] name = "arg-services" @@ -717,17 +717,6 @@ files = [ {file = "mdurl-0.1.2.tar.gz", hash = "sha256:bb413d29f5eea38f31dd4754dd7377d4465116fb207585f97bf925588687c1ba"}, ] -[[package]] -name = "multimethod" -version = "1.12" -description = "Multiple argument dispatching." -optional = true -python-versions = ">=3.9" -files = [ - {file = "multimethod-1.12-py3-none-any.whl", hash = "sha256:fd0c473c43558908d97cc06e4d68e8f69202f167db46f7b4e4058893e7dbdf60"}, - {file = "multimethod-1.12.tar.gz", hash = "sha256:8db8ef2a8d2a247e3570cc23317680892fdf903d84c8c1053667c8e8f7671a67"}, -] - [[package]] name = "networkx" version = "3.4.2" @@ -1140,10 +1129,10 @@ socks = ["pysocks (>=1.5.6,!=1.5.7,<2.0)"] zstd = ["zstandard (>=0.18.0)"] [extras] -cli = ["deepl", "multimethod", "typer"] +cli = ["deepl", "typer"] graphviz = ["pygraphviz"] [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "baa5223c160ea134bb1602f58e7578dc32514427a31451fbbd4363c52e3f22f4" +content-hash = "0929f34a78d60463e1dbcd73ff964b3dc87df60b03dd091ea98843df7fd16c03" diff --git a/pyproject.toml b/pyproject.toml index a88625f..b00f08e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,6 @@ lxml = "^5.1" typer = { version = "^0.9", extras = ["all"], optional = true } deepl = { version = "^1.16", optional = true } pygraphviz = { version = "^1.12", optional = true } -multimethod = { version = "^1.10", optional = true } [tool.poetry.group.dev.dependencies] pytest = "^8.0.0" @@ -35,7 +34,7 @@ lxml-stubs = "^0.5.0" pdoc = "^15.0.0" [tool.poetry.extras] -cli = ["typer", "deepl", "multimethod"] +cli = ["typer", "deepl"] graphviz = ["pygraphviz"] [tool.pytest.ini_options]