Skip to content

Commit

Permalink
add tests for generic inlay hints
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Oct 28, 2024
1 parent 5766654 commit fe1bf28
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@

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("")
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
12 changes: 11 additions & 1 deletion packages/pyright-internal/src/tests/typeInlayHintsWalker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ 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',
Expand All @@ -90,6 +90,16 @@ if (process.platform !== 'win32' || !process.env['CI']) {
position: 118,
value: '[str]',
},
{
inlayHintType: 'generic',
position: 167,
value: '[int]',
},
{
inlayHintType: 'generic',
position: 265,
value: '[bool]',
},
]);
});
} else {
Expand Down

0 comments on commit fe1bf28

Please sign in to comment.