-
Notifications
You must be signed in to change notification settings - Fork 216
Closed as duplicate of#623
Labels
genericsBugs or features relating to ty's generics implementationBugs or features relating to ty's generics implementationhelp wantedContributions especially welcomeContributions especially welcome
Milestone
Description
Summary
I've confirmed the following works in pyright, mypy, and pyrefly - but not in ty:
from dataclasses import dataclass
from typing_extensions import (
Callable,
Generic,
TypeVar,
assert_type,
)
T = TypeVar("T")
@dataclass
class Agent(Generic[T]):
output_type: Callable[..., T]
def func() -> int:
return 1
# pyright, mypy, pyrefly - works
# ty - `Agent[int]` and `Agent[Unknown]` are not equivalent types + Expected `((...) -> T) | ((...) -> Awaitable[T])`, found `def func() -> int`
assert_type(Agent(func), Agent[int])
# works
assert_type(Agent[int](func), Agent[int])Details
I'm hoping to also get Callable[..., T] | Callable[..., Awaitable[T]] to be inferred as the ultimate return type of the awaitable if an async function is passed rather than a regular one, but that's more tricky as it's ambiguous which side of the union should be matched. Note that pyright and pyrefly already handle this "correctly", but not 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
# 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]"
# ty - `Agent[bool]` and `Agent[Unknown]` are not equivalent types
assert_type(coro_agent, Agent[bool])
# works
assert_type(Agent[bool](coro), Agent[bool])It would be great to see both work in ty, but I'm also open to suggestions to do the latter 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)
Version
ty 0.0.1-alpha.6
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
genericsBugs or features relating to ty's generics implementationBugs or features relating to ty's generics implementationhelp wantedContributions especially welcomeContributions especially welcome