8000 Let overload item have a wider return type than implementation · Issue #12434 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content
Let overload item have a wider return type than implementation #12434
@JukkaL

Description

@JukkaL

A wider return type can be useful if a decorator gets a more precise error type as part of a typeshed update.

@wrobell reported this concrete example in #12021 (comment):

import typing as tp
from contextlib import asynccontextmanager

T = tp.TypeVar('T')

@tp.overload
def f() -> tp.AsyncContextManager[int]:
    ...

@tp.overload
def f(cls: type[T]) -> tp.AsyncContextManager[T]:
    ...

@asynccontextmanager
async def f(cls=int):
    yield 1

Mypy 0.940 got a more precise type for @asynccontextmanager, and this resulted in apparent false positives:

tp.py:14: error: Overloaded function implementation cannot produce return type of signature 1
tp.py:14: error: Overloaded function implementation cannot produce return type of signature 2

I think that we can allow an overload item to have a more general return type than the implementation, for convenience.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0