-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Open
Labels
bugmypy got something wrongmypy got something wrongtopic-inferenceWhen to infer types or require explicit annotationsWhen to infer types or require explicit annotations
Description
I've confirmed the following works in pyright and pyrefly - but not in mypy:
from dataclasses import dataclass
from typing_extensions import (
Awaitable,
Callable,
Generic,
TypeVar,
assert_type,
)
T = TypeVar("T")
@dataclass
class Agent(Generic[T]):
output_type: Callable[..., T] | Callable[..., Awaitable[T]]
async def coro() -> bool:
return True
def func() -> int:
return 1
# works
assert_type(Agent(func), Agent[int])
# mypy - error: Argument 1 to "Agent" has incompatible type "Callable[[], Coroutine[Any, Any, bool]]"; expected "Callable[..., Never] | Callable[..., Awaitable[Never]]" [arg-type]
coro_agent = Agent(coro)
# pyright, pyrefly - works
# mypy - error: Expression is of type "Agent[Any]", not "Agent[bool]"
assert_type(coro_agent, Agent[bool])
# works
assert_type(Agent[bool](coro), Agent[bool])
I want T
to be inferred as the ultimate return type of the awaitable if an async function is passed rather than a regular one, but I suppose it's ambiguous which side of the union is the best match.
It would be great to see this work in mypy, but I'm also open to suggestions to do this in a less ambiguous way!
- This is related to a new PydanticAI feature, if you're curious check out Support functions as output_type, as well as lists of functions and other types pydantic/pydantic-ai#1785 (comment)
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-inferenceWhen to infer types or require explicit annotationsWhen to infer types or require explicit annotations