Skip to content

Commit f65bc9d

Browse files
committed
prevent inlay hints from appearing on enum members
1 parent e2f87b0 commit f65bc9d

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

packages/pyright-internal/src/analyzer/typeInlayHintsWalker.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Range } from 'vscode-languageserver-types';
22
import { ParseTreeWalker } from '../analyzer/parseTreeWalker';
33
import { isDunderName, isUnderscoreOnlyName } from '../analyzer/symbolNameUtils';
44
import {
5+
ClassType,
56
FunctionType,
67
Type,
78
getTypeAliasInfo,
@@ -15,6 +16,7 @@ import { ProgramView } from '../common/extensibility';
1516
import { limitOverloadBasedOnCall } from '../languageService/tooltipUtils';
1617
import {
1718
CallNode,
19+
ClassNode,
1820
FunctionNode,
1921
NameNode,
2022
ParamCategory,
@@ -28,6 +30,7 @@ import { convertRangeToTextRange } from '../common/positionUtils';
2830
import { Uri } from '../common/uri/uri';
2931
import { ParseFileResults } from '../parser/parser';
3032
import { InlayHintSettings } from '../common/languageServerInterface';
33+
import { transformTypeForEnumMember } from './enums';
3134

3235
export type TypeInlayHintsItemType = {
3336
inlayHintType: 'variable' | 'functionReturn' | 'parameter';
@@ -93,6 +96,7 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
9396
featureItems: TypeInlayHintsItemType[] = [];
9497
parseResults?: ParseFileResults;
9598
private _range: TextRange | undefined;
99+
private _variablesThatShouldntHaveInlayHints = new Set<ParseNode>();
96100

97101
constructor(
98102
private readonly _program: ProgramView,
@@ -110,6 +114,26 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
110114
}
111115
}
112116

117+
override visitClass(node: ClassNode): boolean {
118+
const evaluator = this._program.evaluator;
119+
if (evaluator) {
120+
const classType = evaluator.getTypeOfClass(node)?.classType;
121+
// prevent inlay hints from appearing on enum members
122+
if (classType && ClassType.isEnumClass(classType)) {
123+
ClassType.getSymbolTable(classType).forEach((symbol, name) => {
124+
const symbolType = transformTypeForEnumMember(evaluator, classType, name, true);
125+
if (symbolType) {
126+
const nameNode = symbol.getDeclarations()[0]?.node;
127+
if (nameNode) {
128+
this._variablesThatShouldntHaveInlayHints.add(nameNode);
129+
}
130+
}
131+
});
132+
}
133+
}
134+
return super.visitClass(node);
135+
}
136+
113137
override visitName(node: NameNode): boolean {
114138
if (
115139
this._settings.variableTypes &&
@@ -125,7 +149,8 @@ export class TypeInlayHintsWalker extends ParseTreeWalker {
125149
!(isClass(type) && isLiteralType(type)) &&
126150
!isTypeVar(type) &&
127151
// !isFunction(type) &&
128-
!isParamSpec(type)
152+
!isParamSpec(type) &&
153+
!this._variablesThatShouldntHaveInlayHints.has(node)
129154
) {
130155
this.featureItems.push({
131156
inlayHintType: 'variable',

packages/pyright-internal/src/tests/samples/inlay_hints/variables.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from enum import Enum
12
from typing import Literal
23
from typing_extensions import TypeVar, ParamSpec
34

@@ -14,4 +15,7 @@ def asdf(a: Foo, b: type[Foo], c: int | str, d: Literal[1, 2], e: type[int]) ->
1415
bar = b # inlay hint (type, but not TypeAlias)
1516
baz = c # inlay hint
1617
qux = d # inlay hint
17-
quxx = e # inlay hint (type, but not TypeAlias)
18+
quxx = e # inlay hint (type, but not TypeAlias)
19+
20+
class Foo(Enum):
21+
a = 1 # no inlay hint

packages/pyright-internal/src/tests/typeInlayHintsWalker.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,37 @@ if (process.platform !== 'win32' || !process.env['CI']) {
77
expect(result).toStrictEqual([
88
{
99
inlayHintType: 'variable',
10-
position: 80,
10+
position: 102,
1111
value: ': str',
1212
},
1313
{
1414
inlayHintType: 'variable',
15-
position: 386,
15+
position: 408,
1616
value: ': TypeAlias',
1717
},
1818
{
1919
inlayHintType: 'variable',
20-
position: 547,
20+
position: 569,
2121
value: ': Foo',
2222
},
2323
{
2424
inlayHintType: 'variable',
25-
position: 572,
25+
position: 594,
2626
value: ': type[int]',
2727
},
2828
{
2929
inlayHintType: 'variable',
30-
position: 623,
30+
position: 645,
3131
value: ': int | str',
3232
},
3333
{
3434
inlayHintType: 'variable',
35-
position: 648,
35+
position: 670,
3636
value: ': Literal[1, 2]',
3737
},
3838
{
3939
inlayHintType: 'variable',
40-
position: 674,
40+
position: 696,
4141
value: ': type[int]',
4242
},
4343
]);

0 commit comments

Comments
 (0)