You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
asynccontextmanager with TypeVar leads to new warnings (regression) #16451
This raises the new errors on master/1.7, but not on 1.6.1.
You can check it on the playground.
Running with --old-type-inference makes the errors go away so I'm tagging @ilevkivskyi as the blog post said they were involved in this new feature. Sorry if this is not appropriate.
Expected Behavior
No errors as I don't think is wrong. pyright also seems to think this is fine.
Actual Behavior
main.py:8: error: Argument 1 to "asynccontextmanager" has incompatible type "Callable[[T], AsyncIterator[T | str]]"; expected "Callable[[VarArg(Never), KwArg(Never)], AsyncIterator[Never]]" [arg-type]main.py:17: error: Need type annotation for "x" [var-annotated]main.py:17: error: Argument "arg" to "func" has incompatible type "str"; expected Never [arg-type]Found 3 errors in 1 file (checked 1 source file)
I'm seeing something similar too, with a device like this:
from __future__ import annotations
from contextlib import asynccontextmanager
from typing import AsyncIterable
from typing_extensions import Self
class A:
@classmethod
@asynccontextmanager
async def x(cls) -> AsyncIterable[Self]:
yield cls()
class B(A):
@classmethod
@asynccontextmanager
async def x(cls) -> AsyncIterable[Self]:
async with super().x() as self:
yield self
% mypy x.py
x.py:10: error: Argument 1 to "asynccontextmanager" has incompatible type "Callable[[type[Self]], AsyncIterable[Self]]"; expected "Callable[[type[Never]], AsyncIterator[Never]]" [arg-type]
x.py:17: error: Argument 1 to "asynccontextmanager" has incompatible type "Callable[[type[Self]], AsyncIterable[Self]]"; expected "Callable[[type[Never]], AsyncIterator[Never]]" [arg-type]
x.py:20: error: Need type annotation for "self" [var-annotated]
Found 3 errors in 1 file (checked 1 source file)
importtyping_extensionsastT=t.TypeVar("T")
deco: t.Callable[[t.Callable[..., T]], t.Callable[..., T]]
@deco# mypy: Argument 1 has incompatible type "Callable[[T], T | int]"; expected "Callable[..., Never]" [arg-type]deffunc(item: T, /) ->T|int:
return0
I actually have been working on this yesterday, and this is quite tricky: inference for unions is quite ad-hoc and it is not easy to adapt it to new inference algorithm.
@Redoubts you problem is different: mypy is correct, you should use AsyncIterator instead (iterable and iterator are not the same).
Sorry, I was in a rush to minimize my example. This looks similarly real tho:
from __future__ import annotations
from contextlib import asynccontextmanager
from typing import AsyncIterator
from typing_extensions import Self
class A:
@classmethod
@asynccontextmanager
async def x(cls) -> AsyncIterator[Self]:
yield cls()
class B(A):
@classmethod
@asynccontextmanager
async def x(cls) -> AsyncIterator[Self]:
async with super().x() as self:
yield self
% mypy x.py
x.py:19: error: Return type "_AsyncGeneratorContextManager[_T_co]" of "x" incompatible with return type "_AsyncGeneratorContextManager[_T_co]" in supertype "A" [override]
x.py:21: error: Incompatible types in "yield" (actual type "_T_co", expected type "Self") [misc]
Found 2 errors in 1 file (checked 1 source file)
Activity
Redoubts commentedon Nov 10, 2023
I'm seeing something similar too, with a device like this:
-e- remove kwargs, wrote too quick
ilevkivskyi commentedon Nov 11, 2023
A simpler repro from another issue:
I actually have been working on this yesterday, and this is quite tricky: inference for unions is quite ad-hoc and it is not easy to adapt it to new inference algorithm.
@Redoubts you problem is different: mypy is correct, you should use
AsyncIteratorinstead (iterable and iterator are not the same).Redoubts commentedon Nov 11, 2023
Sorry, I was in a rush to minimize my example. This looks similarly real tho:
Downgrade mypy for now
Special-case unions in polymorphic inference (#16461)
Downgrade mypy for now
Special-case unions in polymorphic inference (#16461)