Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add inlay hints for generics when instantiating classes #818

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/configuration/language-server-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ the following settings are exclusive to basedpyright

![](inlayHints.functionReturnTypes.png)

**basedpyright.analysis.inlayHints.genericTypes** [boolean]: Whether to show inlay hints on inferred generic types. (currently only works on `Final` and `ClassVar`):
**basedpyright.analysis.inlayHints.genericTypes** [boolean]: Whether to show inlay hints on inferred generic types. Defaults to `false`:

![](inlayHints.genericTypes.png)

Expand Down
52 changes: 45 additions & 7 deletions packages/pyright-internal/src/analyzer/typeInlayHintsWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,11 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
!isAny(type) &&
!(isClass(type) && isLiteralType(type)) &&
!isTypeVar(type) &&
// !isFunction(type) &&
!isParamSpec(type)
) {
this.featureItems.push({
inlayHintType: 'variable',
position: node.start + node.length,
position: this._endOfNode(node),
value: `: ${
type.props?.typeAliasInfo &&
node.nodeType === ParseNodeType.Name &&
Expand All @@ -172,7 +171,7 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
}

override visitCall(node: CallNode): boolean {
if (this._settings.callArgumentNames && this._checkInRange(node)) {
if (this._checkInRange(node)) {
this._generateHintsForCallNode(node);
}
return super.visitCall(node);
Expand Down Expand Up @@ -211,7 +210,7 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
if (valueType) {
this.featureItems.push({
inlayHintType: 'generic',
position: node.start + node.length,
position: this._endOfNode(node),
value: `[${this._printType(valueType)}]`,
});
}
Expand All @@ -229,12 +228,49 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
if (!evaluator) {
return;
}
const functionType = evaluator.getType(node.d.leftExpr);
if (!functionType) {
const callableType = evaluator.getType(node.d.leftExpr);
if (!callableType) {
return;
}

// inlay hints for generics
if (
this._settings.genericTypes &&
// where the type is not explicitly specified
node.d.leftExpr.nodeType !== ParseNodeType.Index &&
// only show them on classes, because the index syntax to specify generics isn't valid on functions
isClass(callableType)
) {
const returnType = evaluator.getType(node);
if (
returnType &&
isClass(returnType) &&
returnType.priv.typeArgs?.length === returnType.shared.typeParams.length
) {
const printedTypeArgs = returnType.priv.typeArgs.flatMap((typeArg) =>
isClass(typeArg) && typeArg.priv.tupleTypeArgs
? typeArg.priv.tupleTypeArgs.map((asdf) => this._printType(asdf.type))
: this._printType(typeArg)
);
if (returnType.priv.tupleTypeArgs) {
// for tuples, as far as i can tell there's no cases where it can infer non-variadic generics, so we just always
// add the ellipsis
printedTypeArgs.push('...');
}
this.featureItems.push({
inlayHintType: 'generic',
position: this._endOfNode(node.d.leftExpr),
value: `[${printedTypeArgs.join(', ')}]`,
});
}
}

if (!this._settings.callArgumentNames) {
return;
}

// if it's an overload, figure out which one to use based on the arguments:
const matchedFunctionType = limitOverloadBasedOnCall(evaluator, functionType, node.d.leftExpr);
const matchedFunctionType = limitOverloadBasedOnCall(evaluator, callableType, node.d.leftExpr);
const matchedArgs = this._program.evaluator?.matchCallArgsToParams(node, matchedFunctionType);

// if there was no match, or if there were multiple matches, we don't want to show any inlay hints because they'd likely be wrong:
Expand Down Expand Up @@ -289,6 +325,8 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
}
}

private _endOfNode = (node: ParseNode) => node.start + node.length;

private _printType = (type: Type): string =>
this._program.evaluator!.printType(type, { enforcePythonSyntax: true });
}
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/languageServerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ export abstract class LanguageServerBase implements LanguageServerInterface, Dis
callArgumentNames: inlayHintSettings?.callArgumentNames ?? true,
functionReturnTypes: inlayHintSettings?.functionReturnTypes ?? true,
variableTypes: inlayHintSettings?.variableTypes ?? true,
genericTypes: inlayHintSettings?.genericTypes ?? true,
genericTypes: inlayHintSettings?.genericTypes ?? false,
}).onInlayHints();
}, token);
}
Expand Down
7 changes: 6 additions & 1 deletion packages/pyright-internal/src/realLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ export abstract class RealLanguageServer extends LanguageServerBase {
logLevel: LogLevel.Info,
autoImportCompletions: true,
functionSignatureDisplay: SignatureDisplayType.formatted,
inlayHints: { callArgumentNames: true, functionReturnTypes: true, variableTypes: true, genericTypes: true },
inlayHints: {
callArgumentNames: true,
functionReturnTypes: true,
variableTypes: true,
genericTypes: false,
},
};

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import ClassVar, Final
from typing import ClassVar, Final, Unpack


foo: Final = int()
Expand All @@ -7,4 +7,20 @@

class Foo:
a: ClassVar = "asdf"
b: ClassVar[str] = "asdf"
b: ClassVar[str] = "asdf"

_ = list([1])

class Foo[T]:
def __init__(self, value: T) -> None:
self.value = value

_ = Foo(True)

_ = tuple((1,2,3))

class Bar[U, *T]:
def __init__(self, asdf: U,*value: Unpack[T]) -> None:
pass

_ = Bar([1], 1,2,"")
9 changes: 7 additions & 2 deletions packages/pyright-internal/src/tests/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { SemanticTokenItem, SemanticTokensWalker } from '../analyzer/semanticTok
import { TypeInlayHintsItemType, TypeInlayHintsWalker } from '../analyzer/typeInlayHintsWalker';
import { Range } from 'vscode-languageserver-types';
import { ServiceProvider } from '../common/serviceProvider';
import { InlayHintSettings } from '../common/languageServerInterface';

// This is a bit gross, but it's necessary to allow the fallback typeshed
// directory to be located when running within the jest environment. This
Expand Down Expand Up @@ -147,13 +148,17 @@ export const semanticTokenizeSampleFile = (fileName: string): SemanticTokenItem[
return walker.items;
};

export const inlayHintSampleFile = (fileName: string, range?: Range): TypeInlayHintsItemType[] => {
export const inlayHintSampleFile = (
fileName: string,
range?: Range,
settings: Partial<InlayHintSettings> = {}
): TypeInlayHintsItemType[] => {
const program = createProgram();
const fileUri = UriEx.file(resolveSampleFilePath(path.join('inlay_hints', fileName)));
program.setTrackedFiles([fileUri]);
const walker = new TypeInlayHintsWalker(
program,
{ callArgumentNames: true, functionReturnTypes: true, variableTypes: true, genericTypes: true },
{ callArgumentNames: true, functionReturnTypes: true, variableTypes: true, genericTypes: false, ...settings },
fileUri,
range
);
Expand Down
36 changes: 33 additions & 3 deletions packages/pyright-internal/src/tests/typeInlayHintsWalker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,48 @@ if (process.platform !== 'win32' || !process.env['CI']) {
]);
});
test('generics', () => {
const result = inlayHintSampleFile('generics.py');
const result = inlayHintSampleFile('generics.py', undefined, { genericTypes: true });
expect(result).toStrictEqual([
{
inlayHintType: 'generic',
position: 47,
position: 55,
value: '[int]',
},
{
inlayHintType: 'generic',
position: 118,
position: 126,
value: '[str]',
},
{
inlayHintType: 'generic',
position: 175,
value: '[int]',
},
{
inlayHintType: 'generic',
position: 273,
value: '[bool]',
},
{
inlayHintType: 'parameter',
position: 274,
value: 'value=',
},
{
inlayHintType: 'generic',
position: 290,
value: '[Literal[1, 2, 3], ...]',
},
{
inlayHintType: 'generic',
position: 399,
value: '[list[int], int, int, str]',
},
{
inlayHintType: 'parameter',
position: 400,
value: 'asdf=',
},
]);
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-pyright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1764,7 +1764,7 @@
},
"basedpyright.analysis.inlayHints.genericTypes": {
"type": "boolean",
"default": true,
"default": false,
"description": "Whether to show inlay hints on inferred generic types.",
"scope": "resource"
}
Expand Down
Loading