Skip to content

Allow Generator type annotation to have one argument. #975

@zeth

Description

@zeth

To annotate a generator yielding an int, it’s
--> Generator[int, None, None]

That's really quite ugly.

It should be Generator[int]

Passing None does not fit the rest of Python where arguments are optional. Also in the rest of Python, the common cases are the default. This makes an unlikely case most prominent over the far more common case. It’s Python’s job to provide syntactic sugar to deal with the low-level details, not the user. This looks more like C.

https://docs.python.org/3/library/typing.html#typing.Generator

“Alternatively, annotate your generator as having a return type of either Iterable[YieldType] or Iterator[YieldType]”

Yes a generator is an iterator and an iterator is an iterable but then why not set everything as Any? Are we annotating the types or not? That advice makes a mockery of the whole idea. Makes Python harder to teach for no good reason. "Put the type the function returns, unless it's a generator then do this..."

Several people have pointed this out before, their bugs got closed without explanation. One suggested making a bug here, so this is it. Enjoy.

python/mypy#4221
https://bugs.python.org/issue31700

I'm hoping this bug gets closed because someone's already fixed it in a development version 😎.

Cheers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions