GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Allow Generator type annotation to have one argument. · Issue #975 · python/typing

Allow Generator type annotation to have one argument. #975

New issue
Copy link
New issue
Copy link
Closed
Closed
Allow Generator type annotation to have one argument.#975
Copy link
Labels
topic: featureDiscussions about new features for Python's type annotationsDiscussions about new features for Python's type annotations

Description

@zeth
zeth
opened on Dec 1, 2021
Issue body actions

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.

Reacted by Joel Niemelä and Oliver Smith

Activity

  1. zeth
    added
    topic: featureDiscussions about new features for Python's type annotationsDiscussions about new features for Python's type annotations
    on Dec 1, 2021
  2. erictraut commented on Dec 1, 2021

    @erictraut
    erictraut
    on Dec 1, 2021
    Collaborator
    More actions

    There's a good reason that Generator has three type parameters. If you omit type arguments, they will be assumed to be Any (by PEP 484 rules), and I don't think that's what you want.

    It sounds like what you're looking for is a simplified version of Generator that assumes the last two type arguments are None. One option is to create a type alias such as SimpleGenerator = Generator[T, None, None]. Another option, as others have suggested, is to use Iterable[YieldType] or Iterator[YieldType].

    There has been some discussion about adding support for default type arguments, but nothing has been finalized. If default type arguments are added to the type, it would be possible to omit type arguments without having them default to Any.

    Reacted by Igor
  3. JelleZijlstra commented on Dec 1, 2021

    @JelleZijlstra
    JelleZijlstra
    on Dec 1, 2021
    Member
    More actions

    #307 proposes supporting defaults for TypeVars. Generator is definitely one of the most compelling use cases. However, I don't think we should special-case just Generator, especially with the workarounds @erictraut lists, so I'll close this issue as a duplicate.

    If you're interested in pursuing this, we should discuss it further in #307 and write a PEP.

    Reacted by Igor and Joel Niemelä
  4. JelleZijlstra
    closed this as completedon Dec 1, 2021
  5. zeth commented on Dec 1, 2021

    @zeth
    zeth
    on Dec 1, 2021
    Author
    More actions

    There's a good reason that Generator has three type parameters. If you omit type arguments, they will be assumed to be Any (by PEP 484 rules), and I don't think that's what you want.

    Why would Any be a problem here if they are never used?

    One option is to create a type alias such as SimpleGenerator = Generator[T, None, None].

    Alias is better advise, that probably should be in the documentation in my opinion.

    from typing import Generator
    IntGenerator = Generator[int, None, None]
    
    Reacted by Pavlo Tymoshenko
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

    topic: featureDiscussions about new features for Python's type annotationsDiscussions 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

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.