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 — contextlib.contextmanager allows `Iterator[T]` -- should probably be `Generator[T, None, None]`? · Issue #2772 · python/typeshed

contextlib.contextmanager allows Iterator[T] -- should probably be Generator[T, None, None]? #2772

New issue
Copy link
New issue
Copy link
Closed
#12087
Closed
contextlib.contextmanager allows Iterator[T] -- should probably be Generator[T, None, None]?#2772
#12087
Copy link
Labels
status: deferredIssue or PR deferred until some precondition is fixedIssue or PR deferred until some precondition is fixedstubs: false negativeType checkers do not report an error, but shouldType checkers do not report an error, but should

Description

@asottile
asottile
opened on Jan 30, 2019
Issue body actions

For instance, this script passes mypy:

import contextlib
from typing import Iterator

@contextlib.contextmanager
def f() -> Iterator[int]:
    return iter([1])


with f():
    pass


with f():
    raise TypeError('wat')

but fails at runtime (and not in the expected way):

$ python t.py 
Traceback (most recent call last):
  File "t.py", line 14, in <module>
    raise TypeError('wat')
TypeError: wat

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "t.py", line 14, in <module>
    raise TypeError('wat')
  File "/usr/lib/python3.6/contextlib.py", line 99, in __exit__
    self.gen.throw(type, value, traceback)
AttributeError: 'list_iterator' object has no attribute 'throw'
Reacted by Ganden Schaffner, Cal Jacobson, Guido Imperiale, Thomas Grainger and Roman Donchenko

Activity

  1. srittau
    added
    size-small
    stubs: false negativeType checkers do not report an error, but shouldType checkers do not report an error, but should
    on Jan 30, 2019
  2. srittau commented on Jan 30, 2019

    @srittau
    srittau
    on Jan 30, 2019
    Collaborator
    More actions

    Sounds reasonable.

  3. graingert commented on Jun 30, 2021

    @graingert
    graingert
    on Jun 30, 2021
    Contributor
    More actions

    contextlib.contextmanager doesn't need to be as specific as Generator[T, None, None], it could take the slightly wider type:

    class ContextManagerArgReturnType(Protocol, Generic[T]):
        def __next__(self) -> T: ...
        def throw(self, exctype: Optional[Type[BaseException]], excinst: Optional[BaseException], exctb: Optional[TracebackType]) -> Any: ...
    
    ContextManagerArgType = Callable[[], ContextManagerArgReturnType[T]]
  4. asottile commented on Mar 1, 2022

    @asottile
    asottile
    on Mar 1, 2022
    ContributorAuthor
    More actions

    this (unfortunately) caused a production incident today -- any chance this can be reconsidered (despite the backward incompatibility)?

    Reacted by Thomas Grainger
    Reacted by Iván || HacKan
  5. HacKanCuBa commented on Jun 2, 2022

    @HacKanCuBa
    HacKanCuBa
    on Jun 2, 2022
    More actions

    So, this is an interesting pitfall of using Iterator[] instead of Generator[] as the docs suggest:

    A generator can be annotated by the generic type Generator[YieldType, SendType, ReturnType]
    ...
    Alternatively, annotate your generator as having a return type of either Iterable[YieldType] or Iterator[YieldType]

    I'm just bumping this up.

  6. AlexWaygood commented on Jun 2, 2022

    @AlexWaygood
    AlexWaygood
    on Jun 2, 2022
    Member
    More actions

    So, this is an interesting pitfall of using Iterator[] instead of Generator[] as the docs suggest:

    A generator can be annotated by the generic type Generator[YieldType, SendType, ReturnType]
    ...
    Alternatively, annotate your generator as having a return type of either Iterable[YieldType] or Iterator[YieldType]

    I'm just bumping this up.

    Can I ask why you're bumping it up? Did the current stubs mean that mypy failed to spot a bug in your code, the same as happened to @asottile?

  7. HacKanCuBa commented on Jun 3, 2022

    @HacKanCuBa
    HacKanCuBa
    on Jun 3, 2022
    More actions

    So, this is an interesting pitfall of using Iterator[] instead of Generator[] as the docs suggest:

    A generator can be annotated by the generic type Generator[YieldType, SendType, ReturnType]
    ...
    Alternatively, annotate your generator as having a return type of either Iterable[YieldType] or Iterator[YieldType]

    I'm just bumping this up.

    Can I ask why you're bumping it up? Did the current stubs mean that mypy failed to spot a bug in your code, the same as happened to @asottile?

    Which brought me here after reading a blogpost :D

  8. AlexWaygood
    added
    status: deferredIssue or PR deferred until some precondition is fixedIssue or PR deferred until some precondition is fixed
    and removed
    size-small
    on Nov 1, 2023
  9. AlexWaygood commented on Nov 1, 2023

    @AlexWaygood
    AlexWaygood
    on Nov 1, 2023
    Member
    More actions

    Marking as deferred unless/until PEP 696 is accepted, as per #7430 (comment).

  10. srittau commented on Feb 14, 2024

    @srittau
    srittau
    on Feb 14, 2024
    Collaborator
    More actions

    See #11422 for the type var generics feature tracker.

  11. hauntsaninja commented on Feb 20, 2024

    @hauntsaninja
    hauntsaninja
    on Feb 20, 2024
    Collaborator
    More actions

    Note that while PEP 696 will help some ergonomic issues and therefore could be good to re-assess, I'm still pretty strongly disinclined to change this. It's been 1.5 years since I commented in #7430 (comment) asking people to report real-life false negatives and no one else has (I just went through all the issues that link to this one and I don't remember any mypy issue reports either). A ruff lint with autofix could help, as would Jukka's suggestion here: #7430 (comment)

    Reacted by Alex Waygood
    Reacted by anthony sottile and James Hilton-Balfe
  12. Akuli
    added
    status: deferredIssue or PR deferred until some precondition is fixedIssue or PR deferred until some precondition is fixed
    and removed
    status: deferredIssue or PR deferred until some precondition is fixedIssue or PR deferred until some precondition is fixed
    on Jun 3, 2024
  13. max-muoto commented on Jul 13, 2024

    @max-muoto
    max-muoto
    on Jul 13, 2024
    Contributor
    More actions

    Now that we have PEP 696, a PR experimenting with the effects here: #12334

  14. SpecLad
    added 2 commits that reference this issue on Oct 30, 2024

    Fix return type annotations for functions used with @contextmanager

    48a69cf

    Fix return type annotations for functions used with @contextmanager (#…

    23e1e0a
  15. srittau
    closed this as completedin #12087on Jan 23, 2026
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

    status: deferredIssue or PR deferred until some precondition is fixedIssue or PR deferred until some precondition is fixedstubs: false negativeType checkers do not report an error, but shouldType checkers do not report an error, but should

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — 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.