Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generator optional parameters #716

Closed
YodaEmbedding opened this issue Apr 16, 2020 · 5 comments
Closed

Generator optional parameters #716

YodaEmbedding opened this issue Apr 16, 2020 · 5 comments
Labels
resolution: duplicate The idea/problem was already reported topic: feature Discussions about new features for Python's type annotations

Comments

@YodaEmbedding
Copy link

YodaEmbedding commented Apr 16, 2020

Can the last two parameters of Generator[YieldType, SendType, ReturnType] be made optional? Usually, one uses generators without them:

def infinite_stream(start: int) -> Generator[int, None, None]:
    while True:
        yield start
        start += 1

Iterator[YieldType] also works, of course, but Generator[YieldType] is more specific.

See also: python/mypy#4221

@gvanrossum
Copy link
Member

PEP 484 currently doesn't support optional arguments to generics (note that Tuple[int, str] is a special form, not just a generic type). But you can easily define a type alias, e.g.

from typing import *

T = TypeVar("T")

SimpleGenerator = Generator[T, None, None]

def foo() -> SimpleGenerator[int]:
    yield 0
    yield 1.1

This gives an error on yield 1.1:

t.py:9: error: Incompatible types in "yield" (actual type "float", expected type "int")

Using various reveal_type() calls I can also confirm that the type of foo is correct:

t.py:11: note: Revealed type is 'def () -> typing.Generator[builtins.int, None, None]'
t.py:12: note: Revealed type is 'typing.Generator[builtins.int, None, None]'
t.py:13: note: Revealed type is 'builtins.int*'

@JelleZijlstra
Copy link
Member

This is a duplicate of #307. (Generator is probably among the few standard ABCs for which default type parameters would make sense.)

@srittau
Copy link
Collaborator

srittau commented May 12, 2021

@JelleZijlstra These issues aren't duplicates. #307 is about TypeVar(T, default=...), this issue about e.g. Generator[int].

@JelleZijlstra
Copy link
Member

But you could use #307 to fix this issue. I guess they're not exact duplicates though.

@JelleZijlstra JelleZijlstra reopened this May 12, 2021
@srittau srittau added resolution: duplicate The idea/problem was already reported topic: feature Discussions about new features for Python's type annotations labels Nov 4, 2021
@srittau
Copy link
Collaborator

srittau commented Nov 4, 2021

But it's a duplicate of https://bugs.python.org/issue31700. :) Closing again.

@srittau srittau closed this as completed Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
resolution: duplicate The idea/problem was already reported topic: feature Discussions about new features for Python's type annotations
Projects
None yet
Development

No branches or pull requests

4 participants