On Mon, Jul 06, 2020 at 07:50:19PM +0300, Sam Eiderman wrote:
Since support for python2 is dropped we can use the new python3
syntax
for type hints.
Interesting - didn't know about this.
I have pushed this, and another patch to update the published minimum
version of Python to 3.6. I don't test anything earlier, although in
theory it could work for 3.5 still.
Rich.
Signed-off-by: Sam Eiderman <sameid(a)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
_______________________________________________
Libguestfs mailing list
Libguestfs(a)redhat.com
https://www.redhat.com/mailman/listinfo/libguestfs
--
Richard Jones, Virtualization Group, Red Hat
http://people.redhat.com/~rjones
Read my programming and virtualization blog:
http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines. Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v