From bdfde828453ad798932e7cd89aac74ec71efff5c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thibault=20Allan=C3=A7on?= <haltode@gmail.com>
Date: Mon, 5 Oct 2020 10:21:41 +0200
Subject: [PATCH] cli: make SWHIDParamType return SWHID type instead of string

---
 swh/model/cli.py | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/swh/model/cli.py b/swh/model/cli.py
index 4c8b7c17..d14d6f98 100644
--- a/swh/model/cli.py
+++ b/swh/model/cli.py
@@ -9,8 +9,9 @@ import sys
 # WARNING: do not import unnecessary things here to keep cli startup time under
 # control
 import click
-from swh.core.cli import swh as swh_cli_group
 
+from swh.core.cli import swh as swh_cli_group
+from swh.model.identifiers import SWHID
 
 CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
 
@@ -25,17 +26,19 @@ _DULWICH_TYPES = {
 
 
 class SWHIDParamType(click.ParamType):
-    name = "persistent identifier"
+    """Click argument that accepts SWHID and return them as
+    :class:`swh.model.identifiers.SWHID` instances """
+
+    name = "SWHID"
 
-    def convert(self, value, param, ctx):
+    def convert(self, value, param, ctx) -> SWHID:
         from swh.model.exceptions import ValidationError
         from swh.model.identifiers import parse_swhid
 
         try:
-            parse_swhid(value)
-            return value  # return as string, as we need just that
+            return parse_swhid(value)
         except ValidationError as e:
-            self.fail("%s is not a valid SWHID. %s." % (value, e), param, ctx)
+            self.fail(f'"{value}" is not a valid SWHID: {e}', param, ctx)
 
 
 def swhid_of_file(path):
@@ -209,7 +212,7 @@ def identify(obj_type, verify, show_filename, follow_symlinks, objects):
 
     if verify:
         swhid = next(results)[1]
-        if verify == swhid:
+        if str(verify) == swhid:
             click.echo("SWHID match: %s" % swhid)
             sys.exit(0)
         else:
-- 
GitLab