Skip to content

Commit

Permalink
add basedpyright.analysis.inlayHints.genericTypes setting
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Oct 25, 2024
1 parent 6d2b555 commit f6a2aaf
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 22 deletions.
Binary file added docs/configuration/inlayHints.genericTypes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions docs/configuration/language-server-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ 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`):

![](inlayHints.genericTypes.png)

### discouraged settings

these options can also be configured [using a config file](./config-files.md). it's recommended to use either a `pyproject.toml` or `pyrightconfig.json` file instead of the language server to configure type checking for the following reasons:
Expand Down
32 changes: 17 additions & 15 deletions packages/pyright-internal/src/analyzer/typeInlayHintsWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,23 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
}

override visitTypeAnnotation(node: TypeAnnotationNode): boolean {
const evaluator = this._program.evaluator;
if (evaluator) {
const annotationType = evaluator.getType(node.d.annotation);
if (
annotationType &&
isInstantiableClass(annotationType) &&
(ClassType.isBuiltIn(annotationType, 'Final') || ClassType.isBuiltIn(annotationType, 'ClassVar'))
) {
const valueType = evaluator.getType(node.d.valueExpr);
if (valueType) {
this.featureItems.push({
inlayHintType: 'generic',
position: node.start + node.length,
value: `[${this._printType(valueType)}]`,
});
if (this._settings.genericTypes && this._checkInRange(node)) {
const evaluator = this._program.evaluator;
if (evaluator) {
const annotationType = evaluator.getType(node.d.annotation);
if (
annotationType &&
isInstantiableClass(annotationType) &&
(ClassType.isBuiltIn(annotationType, 'Final') || ClassType.isBuiltIn(annotationType, 'ClassVar'))
) {
const valueType = evaluator.getType(node.d.valueExpr);
if (valueType) {
this.featureItems.push({
inlayHintType: 'generic',
position: node.start + node.length,
value: `[${this._printType(valueType)}]`,
});
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { ServiceProvider } from './serviceProvider';
import { Uri } from './uri/uri';
import { FileDiagnostics } from './diagnosticSink';

// if adding a new inlay hint type here, make sure you update onInlayHints where it checks if all of them are false
export interface InlayHintSettings {
/**
* pylance's version of this option supports 3 settings: `"all" | "partial" | "off"`. `"all"` shows inlay hints
Expand All @@ -30,6 +29,7 @@ export interface InlayHintSettings {
callArgumentNames: boolean;
functionReturnTypes: boolean;
variableTypes: boolean;
genericTypes: boolean;
}

export interface ServerSettings {
Expand Down
6 changes: 2 additions & 4 deletions packages/pyright-internal/src/languageServerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1002,10 +1002,7 @@ export abstract class LanguageServerBase implements LanguageServerInterface, Dis
if (
workspace.disableLanguageServices ||
// don't bother creating the inlay hint provider if all the inlay hint settings are off
(inlayHintSettings &&
!inlayHintSettings.callArgumentNames &&
!inlayHintSettings.functionReturnTypes &&
!inlayHintSettings.variableTypes)
(inlayHintSettings && !Object.values(inlayHintSettings).some((value) => value))
) {
return null;
}
Expand All @@ -1014,6 +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,
}).onInlayHints();
}, token);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/realLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export abstract class RealLanguageServer extends LanguageServerBase {
logLevel: LogLevel.Info,
autoImportCompletions: true,
functionSignatureDisplay: SignatureDisplayType.formatted,
inlayHints: { callArgumentNames: true, functionReturnTypes: true, variableTypes: true },
inlayHints: { callArgumentNames: true, functionReturnTypes: true, variableTypes: true, genericTypes: true },
};

try {
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export const inlayHintSampleFile = (fileName: string, range?: Range): TypeInlayH
program.setTrackedFiles([fileUri]);
const walker = new TypeInlayHintsWalker(
program,
{ callArgumentNames: true, functionReturnTypes: true, variableTypes: true },
{ callArgumentNames: true, functionReturnTypes: true, variableTypes: true, genericTypes: true },
fileUri,
range
);
Expand Down
6 changes: 6 additions & 0 deletions packages/vscode-pyright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,12 @@
"default": true,
"description": "Whether to show inlay hints on function return types.",
"scope": "resource"
},
"basedpyright.analysis.inlayHints.genericTypes": {
"type": "boolean",
"default": true,
"description": "Whether to show inlay hints on inferred generic types.",
"scope": "resource"
}
}
},
Expand Down

0 comments on commit f6a2aaf

Please sign in to comment.