Open inducer opened 2 years ago
I don't think this is type safe. If B is a subclass of A, f(b) promises you a B, but _f_a(b) gives you an A
Thanks, fair point. Here's another attempt that, I think, addresses your concern, however mypy still complains in the same way. Arguably, it should consider the actual prototype of _f_a
, unless singledispatch
is to be taught about type variables (sounds unlikely?).
from typing import TypeVar
from functools import singledispatch
T = TypeVar("T")
@singledispatch
def f(x: T) -> T:
return x
class A:
pass
AT = TypeVar("AT", bound=A)
@f.register(A)
def _f_a(x: AT) -> AT:
return x
To Reproduce
Run
mypy
(no options) on this file::warning: See below for actual MWE. This is preserved here to preserve the flow of discussion.
Expected Behavior
No complaint. There's no bound on
T
, so I feel that_f_a
should be acceptable.Actual Behavior
Your Environment
mypy.ini
(and other config files): (none)