davidhalter / jedi

Awesome autocompletion, static analysis and refactoring library for python
http://jedi.readthedocs.io
Other
5.81k stars 508 forks source link

Support ParamSpec (PEP 612) #1812

Open PeterJCLaw opened 3 years ago

PeterJCLaw commented 3 years ago

I've not tested it, so the simple case miiight just work (though I doubt it). I'm pretty sure the interesting cases won't work out of the box. PEP 612 introduces a new mechanism for spelling Callable type signatures and allows adjusting them as part of a decorator.

Currently Jedi understands cases like:

from typing import Callable, TypeVar, Any

R = TypeVar('R')
TCallable = TypeVar('TCallable', bound=Callable[..., Any])

def decorator_exact(fn: TCallable) -> TCallable:
    return fn

def decorator_similar(fn: Callable[..., R]) -> Callable[..., R]:
    def decorator(*a, **k):
        return fn(*a, **k)
    return decorator

However with PEP 612 the following becomes possible:

from typing import Callable, TypeVar, ParamSpec, Concatenate

R = TypeVar('R')
P = ParamSpec('P')

def decorator_same_sig(fn: Callable[P, R]) -> Callable[P, R]:
    def decorator(*a: P.args, **k: P.kwargs) -> R:
        return fn(*a, **k)
    return inner

def decorator_changed_sig(fn: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
    def decorator(x: int, *a: P.args, **k: P.kwargs) -> R:
        return fn(*a, **k)
    return inner

I suspect the decorator_same_sig may work (i.e: decorated functions have their name/docstring/etc.) passed through as if they were unaltered, though I'm not sure.

I doubt that decorator_changed_sig will pass through the name/docstring etc. and it's not clear exactly what the right behaviour would be even if they were. It would be great to be able to at least show the right signature (i.e: adjusted as the annotation notes) for decorated functions in this case as that seems unambiguously correct.

PeterJCLaw commented 2 years ago

Unassigning as I've not found time for this and realistically I'm not sure I'm going to any time soon. I've enjoyed working on Jedi, though recently my focus has been elsewhere.

davidhalter commented 2 years ago

Understandable! Thanks a lot anyways for the work you've done on generics. Feel free to pick it up again any time. What are you up to these days?

I'm mostly rewriting Jedi in Rust these days, so I also do not have the time for now.