Skip to content

Commit

Permalink
Fix hover on .get() of TypedDict instance (#9357)
Browse files Browse the repository at this point in the history
* Fix

* Unit test
  • Loading branch information
debonte authored Oct 30, 2024
1 parent c723170 commit e7f69ee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/pyright-internal/src/languageService/hoverProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,9 @@ export class HoverProvider {
const primaryDeclaration = HoverProvider.getPrimaryDeclaration(declarations);
this._addResultsForDeclaration(results.parts, primaryDeclaration, node);
} else if (declInfo && declInfo.synthesizedTypes.length > 0) {
const name = node.d.value;
const nameNode = node;
declInfo?.synthesizedTypes.forEach((type) => {
this._addResultsForSynthesizedType(results.parts, type, name);
this._addResultsForSynthesizedType(results.parts, type, nameNode);
});
this._addDocumentationPart(results.parts, node, /* resolvedDecl */ undefined);
} else if (!node.parent || node.parent.nodeType !== ParseNodeType.ModuleName) {
Expand Down Expand Up @@ -458,19 +458,21 @@ export class HoverProvider {
}
}

private _addResultsForSynthesizedType(parts: HoverTextPart[], typeInfo: SynthesizedTypeInfo, name: string) {
private _addResultsForSynthesizedType(parts: HoverTextPart[], typeInfo: SynthesizedTypeInfo, hoverNode: NameNode) {
let typeText: string | undefined;

if (isModule(typeInfo.type)) {
typeText = '(module) ' + name;
} else if (typeInfo.node) {
const type = this._getType(typeInfo.node);
typeText = '(module) ' + hoverNode.d.value;
} else {
const node = typeInfo.node ?? hoverNode;

const type = this._getType(node);
typeText = getVariableTypeText(
this._evaluator,
/* declaration */ undefined,
typeInfo.node.d.value,
node.d.value,
type,
typeInfo.node,
node,
this._functionSignatureDisplay
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path="typings/fourslash.d.ts" />

// @filename: test.py
//// from typing_extensions import TypedDict
////
//// class Cls(TypedDict):
//// a: int
//// b: str
////
//// dct: Cls = {"a": 1, "b": "2"}
//// dct.[|/*marker1*/get|]("a")

helper.verifyHover('markdown', {
marker1: "```python\n(variable) def get(k: Literal['a']) -> int\n```",
});

0 comments on commit e7f69ee

Please sign in to comment.