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
933 stars 17 forks source link

code action/double click to transition inlay hints to real life #198

Open KotlinIsland opened 5 months ago

KotlinIsland commented 5 months ago

Before

backticks represent inlay hints

class A: ...
a`: A` = A()

After

class A: ...
a: A = A()
KotlinIsland commented 4 months ago
DetachHead commented 3 months ago

something i noticed about pylance's implementation of this feature is that it doesn't work on all inlay hints for some reason. eg:

def f(a: int, /) -> str: ...

x = f  # works

foo = "" # doesn't work

image

imo inlay hints should always be valid code, so they should always allow double-clicking to insert them

NCBM commented 1 month ago

something i noticed about pylance's implementation of this feature is that it doesn't work on all inlay hints for some reason. eg:

def f(a: int, /) -> str: ...

x = f  # works

foo = "" # doesn't work

image

Recently I tested the case once doesn't work and it worked now (Pylance v2024.7.1).

image

image

Auto-import from typing is even done.

imo inlay hints should always be valid code, so they should always allow double-clicking to insert them

Actually there are also inlay hints which are not valid code, e.g.

a, b = "foo", int()  # `a` is shown nothing while `b` is shown as `int` in inlay hints.

c = 42  # `c` is shown nothing in inlay hints. hovering mouse cursor to see the type.

image

of course they cannot be inserted.

Another thing we can notice is that some not-quite-useful Literals are sometimes not shown.

DetachHead commented 1 month ago

oops, in my example i had it the wrong way around. it's the Callable inlay hint that doesn't work:

def f(a: int, /) -> str: ...

x = f  # doesn't work

foo = "" # works

related: #199

NCBM commented 1 month ago

maybe Callable inlay hints are considered complex by pylance that should be assigned to a type alias?

KotlinIsland commented 1 month ago

maybe Callable inlay hints are considered complex by pylance that should be assigned to a type alias?

Such a based idea:

def f(a=1, /, *, b=False):
    return 1

a = f

After transition operation:

class F(Protocol): # maybe "A"?
    def __call__(self, a: int = ..., /, *, b: bool = ...) -> int: ...

a: F = f