Skip to content

Commit

Permalink
fix values that were narrowed to Never being coloured as a type
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Jun 2, 2024
1 parent 7972d3d commit e4e2c6d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
18 changes: 13 additions & 5 deletions packages/pyright-internal/src/analyzer/semanticTokensWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,19 @@ export class SemanticTokensWalker extends ParseTreeWalker {
}
}
const symbol = this._evaluator?.lookUpSymbolRecursive(node, node.value, false)?.symbol;
if (type?.category === TypeCategory.Never && symbol && !this._evaluator.getDeclaredTypeOfSymbol(symbol).type) {
// for some reason Never is considered both instantiable and an instance, so we need to look up the type this way
// to differentiate between "instances" of `Never` and type aliases/annotations of Never:
this._addItem(node.start, node.length, SemanticTokenTypes.type, []);
return;
if (type?.category === TypeCategory.Never && symbol) {
// for some reason Never is considered both instantiable and an instance, so we need a way
// to differentiate between "instances" of `Never` and type aliases/annotations of Never.
// this is probably extremely cringe since i have no idea what this is doing and i literally
// just brute forced random shit until all the tests passed
const typeResult = this._evaluator?.getEffectiveTypeOfSymbolForUsage(symbol, node);
if (
typeResult.type.category !== TypeCategory.Never &&
(typeResult.type.category !== TypeCategory.Unbound || !typeResult.includesIllegalTypeAliasDecl)
) {
this._addItem(node.start, node.length, SemanticTokenTypes.type, []);
return;
}
}
const declarations = this._evaluator?.getDeclarationsForNameNode(node);
if (declarations?.some(isParameterDeclaration)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,15 @@

def baz() -> Never:
...

def asdf(foo: Never):
value: Never = foo
value

Type = Never
value: Type = 1

def inferred():
value = ''
if not isinstance(value, str):
value
39 changes: 29 additions & 10 deletions packages/pyright-internal/src/tests/semanticTokensProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,35 @@ if (process.platform !== 'win32' || !process.env['CI']) {
test('never', () => {
const result = semanticTokenizeSampleFile('never.py');
expect(result).toStrictEqual([
{ type: 'namespace', modifiers: [], start: 5, length: 6 },
{ type: 'class', modifiers: [], start: 19, length: 5 },
{ type: 'class', modifiers: [], start: 19, length: 5 },
{ type: 'variable', modifiers: [], start: 26, length: 3 },
{ type: 'type', modifiers: [], start: 31, length: 5 },
{ type: 'class', modifiers: [], start: 37, length: 3 },
{ type: 'class', modifiers: [], start: 43, length: 5 },
{ type: 'function', modifiers: ['definition'], start: 54, length: 3 },
{ type: 'function', modifiers: [], start: 54, length: 3 },
{ type: 'type', modifiers: [], start: 63, length: 5 },
{ type: 'namespace', modifiers: [], start: 5, length: 6 }, // typing
{ type: 'class', modifiers: [], start: 19, length: 5 }, // Never
{ type: 'class', modifiers: [], start: 19, length: 5 }, // Never
{ type: 'variable', modifiers: [], start: 26, length: 3 }, // foo
{ type: 'type', modifiers: [], start: 31, length: 5 }, // Never
{ type: 'class', modifiers: [], start: 37, length: 3 }, // bar
{ type: 'class', modifiers: [], start: 43, length: 5 }, // Never
{ type: 'function', modifiers: ['definition'], start: 54, length: 3 }, // baz
{ type: 'function', modifiers: [], start: 54, length: 3 }, // baz
{ type: 'type', modifiers: [], start: 63, length: 5 }, // Never
{ type: 'function', modifiers: ['definition'], start: 83, length: 4 }, // asdf
{ type: 'function', modifiers: [], start: 83, length: 4 }, // asdf
{ type: 'parameter', modifiers: ['definition'], start: 88, length: 3 }, // foo
{ type: 'type', modifiers: [], start: 93, length: 5 }, // Never
{ type: 'variable', modifiers: [], start: 105, length: 5 }, // value
{ type: 'type', modifiers: [], start: 112, length: 5 }, // Never
{ type: 'parameter', modifiers: [], start: 120, length: 3 }, // foo
{ type: 'variable', modifiers: [], start: 128, length: 5 }, // value
{ type: 'class', modifiers: [], start: 135, length: 4 }, // Type
{ type: 'class', modifiers: [], start: 142, length: 5 }, // Never
{ type: 'variable', modifiers: [], start: 148, length: 5 }, // value
{ type: 'type', modifiers: [], start: 155, length: 4 }, // Type
{ type: 'function', modifiers: ['definition'], start: 169, length: 8 }, // inferred
{ type: 'function', modifiers: [], start: 169, length: 8 }, // inferred
{ type: 'variable', modifiers: [], start: 185, length: 5 }, // value
{ type: 'function', modifiers: ['defaultLibrary', 'builtin'], start: 207, length: 10 }, // isinstance
{ type: 'variable', modifiers: [], start: 218, length: 5 }, // value
{ type: 'class', modifiers: ['defaultLibrary', 'builtin'], start: 225, length: 3 }, // str
{ type: 'variable', modifiers: [], start: 239, length: 5 }, // value
]);
});

Expand Down

0 comments on commit e4e2c6d

Please sign in to comment.