pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

Hover oddities

Open ljw1004 opened this issue 9 months ago • 3 comments

I was reading through the code for Hover and noticed three oddities...

Missing newline

class Post:
    """Docstring-for-Post"""


def foo(item: type[Post]) -> None:
    """Args:
        item: Docstring-for-item"""
    ...

Hover on the parameter "item" shows --- but I think it should instead have been split into paragraphs

Image

This is the LSP response:

{"jsonrpc":"2.0","id":2,"result":{"contents":{"kind":"markdown","value":"```python\n(parameter) item: type[Post]\n```\nitem: the item to bar---\nThis is what a Post is"},"range":{"start":{"line":21,"character":8},"end":{"line":21,"character":12}}}}

Should be union instead of concatenate

class D1(TypedDict):
    age: int
    """doc-for-D1.age"""


class D2(TypedDict):
    age: str
    """doc-for-D2.age"""


def bar(item: D1 | D2) -> None:
    x = item["age"]
    print(x)
    reveal_type(item["age"])


bar({"age": 1})

Hover on the "age" argument in the callsite to bar. It shows the hover of D1.age, followed by the hover of D2.age. But I reckon it should instead be merging them (key) age: int | str. That would be consistent with the reveal_type in the above code.

Image

(The existing unit test hover.typedDict.key.fourslash.ts tests for the existing behavior; I just reckon the existing behavior feels odd...)

Should have hover for TypedDict member access

In the above example, in the line x = item["age"], hover over the string "age". I expect that this should show a hover tooltip, similar to how it does at the callsite to bar. But it doesn't show anything.

ljw1004 avatar Mar 22 '25 18:03 ljw1004

The language server features in the pyright source base are maintained by the Microsoft pylance team. I'll let them respond and prioritize accordingly.

On your second point, I don't think it makes sense to synthesize a union type for hover purposes. It's arguably preferable to retain the individual definitions with their own docstrings. Hover does not need to be consistent with reveal_type in terms of formatting, since they're used for very different purposes. They even operate on different entities: reveal_type operates on expressions, and hover operates on identifiers.

Could someone from the pylance team please transfer this to the pylance-release project? Thanks!

erictraut avatar Mar 22 '25 19:03 erictraut

Should have hover for TypedDict member access In the above example, in the line x = item["age"], hover over the string "age". I expect that this should show a hover tooltip, similar to how it does at the callsite to bar. But it doesn't show anything.

Agreed. IMO the behavior should be the same as for dataclasses:

from dataclasses import dataclass
from typing import TypedDict


class MyDict(TypedDict):
    foo: str
    bar: int


@dataclass
class MyDataclass:
    foo: str
    bar: int


my_dict: MyDict = { "foo": "asdf", "bar": 1234 }
my_dataclass = MyDataclass(**my_dict)

print(my_dict["foo"])  # hovering over `foo` won't show anything in IDE
print(my_dataclass.foo)  # hovering over `foo` *will* show `foo: str` in IDE

All in all, this makes it difficult to adopt TypedDicts in lieu of dataclasses right now because IDE support for the latter is quite a bit nicer.

codethief avatar Jul 28 '25 14:07 codethief

Addendum: It's not just hover-to-get-type that doesn't work for TypedDict entries. Cntrl + click (go to definition) doesn't work, either, and I suspect these problems are related?

codethief avatar Jul 31 '25 12:07 codethief