[Libguestfs] [PATCH] python: Add type hints

Sam Eiderman sameid at google.com
Tue Jul 7 05:51:31 UTC 2020


Notice that I have added the closing bracket and return type hint into
the indentation function as a hack to line wrap correctly when the
return type hint is causing > 79 chars per line:

So that instead of:

    def btrfs_subvolume_create(self, dest: str, qgroupid:
Optional[str] = None) -> None:

We correctly get:

    def btrfs_subvolume_create(self, dest: str,
                               qgroupid: Optional[str] = None) -> None:

Since the indent function looks for a comma to wrap - this works nicely.

However there is this one case where we use the Tuple hint, which
contains a comma in itself and causes the following weird line
wrapping:

    def btrfs_subvolume_show(self, subvolume: str) -> Union[List[Tuple[str,
                             str]], dict]:
    def inspect_get_drive_mappings(self, root: str) -> Union[List[Tuple[str,
                                   str]], dict]:
    def inspect_get_mountpoints(self, root: str) -> Union[List[Tuple[str,
                                str]], dict]:

In my opinion it is okay, pep8 complains about this and actually
requires either:

Option A

    def btrfs_subvolume_show(self, subvolume: str) -> Union[List[Tuple[str,
                                                                       str]],
                                                            dict]:
        pass

Which has a more complicated nesting scheme and will require the
indentation function to work more intelligently

Option B

    def btrfs_subvolume_show(self,
                             subvolume: str) -> Union[List[Tuple[str, str]],
                                                      dict]:
        pass

Still requires intelligent wrapping

Option C

    def btrfs_subvolume_show(
        self, subvolume: str
    ) -> Union[List[Tuple[str, str]], dict]:

This option is easy to implement, just change the amount of
indentation required (9 + function name) to 8 and begin args on
newline, however in most cases this will make the all other 500
function signatures longer.

WDYT?

On Mon, Jul 6, 2020 at 10:40 PM Richard W.M. Jones <rjones at redhat.com> wrote:
>
> 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 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
> >
> > _______________________________________________
> > Libguestfs mailing list
> > Libguestfs at 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
>




More information about the Libguestfs mailing list