[Libguestfs] [PATCH] python: Add type hints

Richard W.M. Jones rjones at redhat.com
Tue Jul 7 08:58:10 UTC 2020


On Tue, Jul 07, 2020 at 08:51:31AM +0300, Sam Eiderman wrote:
> 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?

I'm not massively concerned about generated code.  It should be
possible for humans to follow it and we tend to format it as nicely as
is feasible, but a bit of line wrapping isn't the end of the world.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-top is 'top' for virtual machines.  Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top




More information about the Libguestfs mailing list