Skip to content

one-argument version for Generator.typing #75881

Closed

Description

@srittau
mannequin
BPO 31700
Nosy @srittau, @ilevkivskyi, @zeth

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2017-11-02.22:56:04.702>
created_at = <Date 2017-10-05.08:35:17.837>
labels = ['3.7', 'invalid']
title = 'one-argument version for Generator.typing'
updated_at = <Date 2021-12-01.01:34:39.694>
user = 'https://github.com/srittau'

bugs.python.org fields:

activity = <Date 2021-12-01.01:34:39.694>
actor = 'zeth'
assignee = 'none'
closed = True
closed_date = <Date 2017-11-02.22:56:04.702>
closer = 'levkivskyi'
components = []
creation = <Date 2017-10-05.08:35:17.837>
creator = 'srittau'
dependencies = []
files = []
hgrepos = []
issue_num = 31700
keywords = []
message_count = 5.0
messages = ['303748', '303839', '305456', '305459', '407421']
nosy_count = 3.0
nosy_names = ['srittau', 'levkivskyi', 'zeth']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue31700'
versions = ['Python 3.7']

Activity

  1. srittau commented on Oct 5, 2017

    srittaumannequin
    MannequinAuthor

    Currently typing.Generator requires three arguments: Generator[YieldType, SendType, ReturnType].

    At least for me, passing values to a generator is a very rare case. I suggest to allow only one argument to be passed to Generator: Generator[YieldType], where the other arguments default to None. This makes the common case more readable and is less error-prone. (I always forget the second and third argument, since that use case is so uncommon for me.)

  2. ilevkivskyi commented on Oct 6, 2017

    @ilevkivskyi
    Member

    You can use Iterator type, for example this works in mypy (didn't test in other type checkers):

      def f() -> Iterator[int]:
          yield 42

    In case you want annotate something specifically as Generator[int, None, None] (for example to use its .close() method), then you can create a generic alias:

      T = TypeVar('T')
      Gen = Generator[T, None, None]
    
      def f() -> Gen[int]:
          ...

    this is supported by mypy as well.

  3. ilevkivskyi commented on Nov 2, 2017

    @ilevkivskyi
    Member

    Since there is no response for few weeks I assume this works for you.

  4. srittau commented on Nov 2, 2017

    srittaumannequin
    MannequinAuthor

    Sorry for not responding, but I didn't know what I could have added that I didn't already say in the opening post. Of course, you can use workaround like using the three-argument version or creating aliases. Using Iterator is of course not a real solution for the general case.

    I still believe that Generator should be usable with just one argument or typing should provide such an alias.

  5. zeth commented on Dec 1, 2021

    zethmannequin
    Mannequin

    I don't get why this is closed. It is a bug. The Generator type annotation works differently than the others, it shouldn't.

    Sebastian Rittau is correct here, the closer doesn't seem to have understood.

    As for using Iterator instead as the documentation suggests, that makes a mockery of the whole process. You might as well set everything to Any.

  6. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions