basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

infer parameter type from decorator type

Open KotlinIsland opened this issue 1 year ago • 0 comments

def f(fn: Callable[[int], int]):...

f(lambda i: i) # no error, int
@f
def foo(i): # error
    return i # Unknown

real life:

from typing import Callable

from basedtyping import P, T

def copy_signature(fn: Callable[P, T]) -> Callable[[Callable[P, T]], Callable[P, T]]:
    ...

def f(a: int) -> str:
    return str(a)

@copy_signature(f)
def g(a):
    reveal_type(a)  # Unknown
    return ""

reveal_type(g)  # (int) -> str

The function should act like a lambda here and infer the types of the parameters from the surrounding context

copy_signature(f, lambda a: "")  # correctly infers

KotlinIsland avatar Jun 06 '24 05:06 KotlinIsland