Open asottile opened 5 years ago
Sounds reasonable.
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]]
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 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.
So, this is an interesting pitfall of using
Iterator[]
instead ofGenerator[]
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?
So, this is an interesting pitfall of using
Iterator[]
instead ofGenerator[]
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
Marking as deferred unless/until PEP 696 is accepted, as per https://github.com/python/typeshed/pull/7430#issuecomment-1238711970.
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 https://github.com/python/typeshed/pull/7430#issuecomment-1238711970 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: https://github.com/python/typeshed/pull/7430#issuecomment-1080460175
Now that we have PEP 696, a PR experimenting with the effects here: https://github.com/python/typeshed/pull/12334
For instance, this script passes mypy:
but fails at runtime (and not in the expected way):