Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Infer type of generic class from return type of (optionally awaitable) callable passed to constructor #19143

Open
Labels
bugmypy got something wrongtopic-inferenceWhen to infer types or require explicit annotations
@DouweM

Description

@DouweM

I've confirmed the following works in pyright and pyrefly - but not in mypy:

fromdataclassesimportdataclassfromtyping_extensionsimport (Awaitable,Callable,Generic,TypeVar,assert_type,)T=TypeVar("T")@dataclassclassAgent(Generic[T]):output_type:Callable[...,T]|Callable[...,Awaitable[T]]asyncdefcoro()->bool:returnTruedeffunc()->int:return1# worksassert_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])# worksassert_type(Agent[bool](coro),Agent[bool])

I wantT 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-inferenceWhen to infer types or require explicit annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp