-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
contextlib.contextmanager allows Iterator[T]
-- should probably be Generator[T, None, None]
?
#2772
Comments
Sounds reasonable. |
contextlib.contextmanager doesn't need to be as specific as 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]] |
this (unfortunately) caused a production incident today -- any chance this can be reconsidered (despite the backward incompatibility)? |
So, this is an interesting pitfall of using
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 |
Marking as deferred unless/until PEP 696 is accepted, as per #7430 (comment). |
See #11422 for the type var generics feature tracker. |
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) |
Now that we have PEP 696, a PR experimenting with the effects here: #12334 |
Some of them are annotated with an `Iterator` return type. However... It just occurred to me that `@contextmanager` cannot work with a function that returns a plain iterator, since it relies on the generator class's `throw` method. `contextmanager` is defined in typeshed as accepting an iterator-returning function, but that appears to be a bug: <python/typeshed#2772>. Change all such annotations to a `Generator` type instead. Some annotations are also broken in other ways; fix them too.
…8617) Some of them are annotated with an `Iterator` return type. However... It just occurred to me that `@contextmanager` cannot work with a function that returns a plain iterator, since it relies on the generator class's `throw` method. `contextmanager` is defined in typeshed as accepting an iterator-returning function, but that appears to be a bug: <python/typeshed#2772>. Change all such annotations to a `Generator` type instead. Some annotations are also broken in other ways; fix them too.
For instance, this script passes mypy:
but fails at runtime (and not in the expected way):
The text was updated successfully, but these errors were encountered: