DetachHead / basedpyright

pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server
https://docs.basedpyright.com
Other
1.02k stars 19 forks source link

infer parameter type from decorator type #379

Open KotlinIsland opened 4 months ago

KotlinIsland commented 4 months ago
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