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 — contextmanager decorators require Generators by asottile · Pull Request #2773 · python/typeshed

Conversation

@asottile

@asottile asottile commented Jan 30, 2019

Copy link
Copy Markdown
Contributor

Resolves #2772

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@asottile
contextmanager decorators require Generators
5ee28c7
@asottile

asottile commented Jan 30, 2019

Copy link
Copy Markdown
Contributor Author

the sample from the issue now fails with:

$ mypy --custom-typeshed . t.py 
t.py:4: error: Argument 1 to "contextmanager" has incompatible type "Callable[[], Iterator[int]]"; expected "Callable[..., Generator[<nothing>, Any, Any]]"

I wasn't sure what to put for the other generic parameters for Generator / AsyncGenerator so I set them as Any

Also wasn't sure how else to test this so hopefully CI points me in the right direction if I've messed it up 😆

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@asottile

asottile commented Jan 30, 2019

Copy link
Copy Markdown
Contributor Author

hmmm, CI is failing -- looks like AsyncGenerator was introduced in 3.5.4 -- I don't know what to do to resolve that though :(

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ilevkivskyi

ilevkivskyi commented Jan 30, 2019

Copy link
Copy Markdown
Member

Will this allow:

@contextmanager
def func() -> Iterator[int]:
    yield 1  # explicit

If not, then there is no way this can be merged. There are thousands existing annotations like this, and we can't make them all fail because of a false negative in rare corner case.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@asottile

asottile commented Jan 30, 2019

Copy link
Copy Markdown
Contributor Author

it will not, Iterator[T] isn't the correct type for a generator -- but it's understandable that you wouldn't want to break things. and as far as I know there isn't a warning mechanism to nudge people in the right direction.

my original issue is because of a particularly nasty bug enabled by a developer's misunderstanding of how contextmanager works (that, surprise surprise, was only triggered in an exceptional case). that mypy failed to catch this statically was the real kicker because if the annotation was correct in typeshed this would have been caught before review and long before production. (@contextmanager decorating a function which returns an actual Iterator[T] works in non-exception cases)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ilevkivskyi

ilevkivskyi commented Jan 30, 2019

Copy link
Copy Markdown
Member

and as far as I know there isn't a warning mechanism to nudge people in the right direction

Actually there is basic support for warnings in mypy. But currently warnings still give return code 1. There is some (slowly moving) ongoing work on how to treat errors/warnings more systematically.

Eric-Arellano reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JelleZijlstra

JelleZijlstra commented Feb 1, 2019

Copy link
Copy Markdown
Member

I agree with @ilevkivskyi that we can't afford to break all existing annotations that use Iterator for contextmanagered functions. Maybe for now we can just add a comment in the stub to clarify.

Another way forward could be to add special-casing in mypy that detects when a generator function has Iterator[T] as its declared return type and changes it to Generator[T, None, None].

Eric-Arellano and AlexWaygood reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@msullivan

msullivan commented Feb 1, 2019

Copy link
Copy Markdown
Contributor

Another way forward could be to add special-casing in mypy that detects when a generator function has Iterator[T] as its declared return type and changes it to Generator[T, None, None].

This seems like a good idea to me

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@srittau srittau added the status: deferred Issue or PR deferred until some precondition is fixed label Oct 10, 2019
@srittau

srittau commented Oct 10, 2019

Copy link
Copy Markdown
Collaborator

I have marked this as deferred for now. While I think that in general typeshed should have exact types, but backwards compatibility is an issue. Until that is solved/worked around we can't merge this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@asottile asottile closed this Apr 22, 2020
@asottile
asottile deleted the contextmanager_generators branch April 22, 2020 20:32
@asottile asottile mentioned this pull request Mar 3, 2022
require Generator for contextmanager #7430
Closed
@AlexWaygood AlexWaygood mentioned this pull request Sep 23, 2023
Fix contextlib.asynccontextmanager to work with coroutine functions #10753
Closed
@Sachaa-Thanasius Sachaa-Thanasius mentioned this pull request Jun 2, 2024
Fix the signatures of @(async)contextmanager #12087
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

status: deferred Issue or PR deferred until some precondition is fixed

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

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

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@asottile @ilevkivskyi @JelleZijlstra @msullivan @srittau
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.