anentropic / python-waterloo

A cli tool to convert type annotations found in 'Google-style' docstrings into mypy py2 type comments (and from there into py3 type annotations).
2 stars 0 forks source link

If docstring has "Yields" instead of "Returns" annotate as Generator? #30

Closed anentropic closed 4 years ago

anentropic commented 4 years ago

Currently we treat Yields as just an alternate section title and annotate func as -> <type>

This is probably an incorrect annotation

https://mypy.readthedocs.io/en/stable/kinds_of_types.html#generators suggests three options

The simplest would be -> Iterator[<type>]

The more explicit would be -> Generator[<type>, None, None]

anentropic commented 4 years ago

-> Iterator[<type>]

-> Generator[<type>, None, None]

Probably in either case all docstrings with Yields should log a warning so that you can check the annotation and update.

Despite the cons I still lean towards the latter, as it's more explicit. Also if the 2nd and 3rd types are wrong hopefully the type checker would pick up on that, whereas with the former annotation it might pass silently and leave things under-typed (I guess?)