[Libguestfs] [PATCH] python: Add type hints

Sam Eiderman sameid at google.com
Mon Jul 6 16:50:19 UTC 2020


Since support for python2 is dropped we can use the new python3 syntax
for type hints.

Signed-off-by: Sam Eiderman <sameid at google.com>
---
 generator/python.ml | 39 +++++++++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)

diff --git a/generator/python.ml b/generator/python.ml
index f0d6b5d96..3640ee39a 100644
--- a/generator/python.ml
+++ b/generator/python.ml
@@ -685,6 +685,7 @@ logvols = g.lvs()
 import os
 import sys
 import libguestfsmod
+from typing import Union, List, Tuple, Optional
 
 ";
 
@@ -802,14 +803,44 @@ class GuestFS(object):
     fun f ->
       let ret, args, optargs = f.style in
       let len_name = String.length f.name in
+      let ret_type_hint =
+        match ret with
+        | RErr -> "None"
+        | RInt _ | RInt64 _ -> "int"
+        | RBool _ -> "bool"
+        | RConstOptString _ -> "Optional[str]"
+        | RConstString _ | RString _ -> "str"
+        | RBufferOut _ -> "bytes"
+        | RStringList _ -> "List[str]"
+        | RStruct _ -> "dict"
+        | RStructList _ -> "List[dict]"
+        | RHashtable _ -> "Union[List[Tuple[str, str]], dict]" in
+      let type_hint_of_argt arg =
+        match arg with
+        | String _ -> ": str"
+        | OptString _ -> ": Optional[str]"
+        | Bool _ -> ": bool"
+        | Int _ | Int64 _ -> ": int"
+        | BufferIn _ -> ": bytes"
+        | StringList _ -> ": List[str]"
+        | Pointer _ -> ""
+      in
+      let type_hint_of_optargt optarg =
+        match optarg with
+        | OBool _ -> "bool"
+        | OInt _ | OInt64 _ -> "int"
+        | OString _ -> "str"
+        | OStringList _ -> "List[str]"
+      in
       let decl_string =
         "self" ^
-        map_join (fun arg ->sprintf ", %s" (name_of_argt arg))
+        map_join (fun arg ->sprintf ", %s%s" (name_of_argt arg) (type_hint_of_argt arg))
           args ^
-        map_join (fun optarg -> sprintf ", %s=None" (name_of_optargt optarg))
-          optargs in
+        map_join (fun optarg -> sprintf ", %s: Optional[%s] = None" (name_of_optargt optarg) (type_hint_of_optargt optarg))
+          optargs ^
+        ") -> " ^ ret_type_hint ^ ":" in
       pr "\n";
-      pr "    def %s(%s):\n"
+      pr "    def %s(%s\n"
         f.name (indent_python decl_string (9 + len_name) 78);
 
       if is_documented f then (
-- 
2.27.0.212.ge8ba1cc988-goog




More information about the Libguestfs mailing list