ty icon indicating copy to clipboard operation
ty copied to clipboard

Show the type alias's name on hover

Open MichaReiser opened this issue 2 months ago • 2 comments

Hovering a type alias currently reveals the aliased type.

Image

We should show the type alias's definition instead, similar to pylance and resolve the documentation etc from the target

Image

MichaReiser avatar Nov 04 '25 14:11 MichaReiser

I'm going to try my hand at this over the weekend :)

Hugo-Polloli avatar Nov 15 '25 12:11 Hugo-Polloli

Actually postponing working on this, before actually implementing anything I want to confirm the exact hover behavior, esp for PEP 695 type aliases.

Right now, with Pylance on Python 3.12, I see:

T = int       # hover T: `(type) T = int` + docs for int
type U = int  # hover U: `(type) U = TypeAliasType` + docs for int

class Test:
    t: T  # hover t: `(variable) t: int`
          # hover T: `(type) T = int` + docs for int

    u: U  # hover u: `(variable) u: int`
          # hover U: `(type) U = type[int]` + docs for int

The only consistent part is that the docs always comes from the aliased target. Also Pylance leaks internal representation (TypeAliasType / type[int]).

However, T = int and type U = int are not the same thing at runtime (the former binds T directly to int, while the latter is a TypeAliasType object), so I think it’s important to hint at that, but without weighing things down by mentioning TypeAliasType in the hover.

What I'm thinking for ty:

T = int       # hover T: `(type) T = int` + docs for int
type U = int  # hover U: `(type alias) type U = int` + docs for int


class Test:
    t: T  # hover t: `(variable) t: T (int)`
          # hover T: `(type) T = int` + docs for int

    u: U  # hover u: `(variable) u: U (int)`
          # hover U: `(type alias) type U = int` + docs for int


type Box[T] = list[T]

x: Box[int]  # hover x: `(variable) x: Box[int] (list[int])`
             # hover Box: `(type alias) type Box[T] = list[T]` + docs for list
  • T = int presented as a plain type binding: (type) T = int.
  • type U = int / type Box[T] = ... presented as first-class alias objects: (type alias) type U = int, etc.
  • Variables always show user written-annotation (resolved-type).

Does this behavior match what you’d like for ty?

Hugo-Polloli avatar Nov 16 '25 14:11 Hugo-Polloli