Skip to content

Commit

Permalink
Fix Unknown and Any variables being coloured as Unbound
Browse files Browse the repository at this point in the history
Fixes #180
  • Loading branch information
ValdezFOmar authored and DetachHead committed Apr 1, 2024
1 parent b1ebff9 commit 3c860e2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export class SemanticTokensWalker extends ParseTreeWalker {
this._addItem(node.start, node.length, SemanticTokenTypes.namespace, []);
return;
// handled bellow
case TypeCategory.Any:
case TypeCategory.Unknown:
case TypeCategory.TypeVar:
break;
Expand Down Expand Up @@ -218,7 +219,11 @@ export class SemanticTokensWalker extends ParseTreeWalker {
// with @classmethod decorator, `__new__`, `__init_subclass__`, etc.) so we need to
// check first if it's a parameter before checking that it's a TypeVar
this._addItem(node.start, node.length, SemanticTokenTypes.typeParameter, []);
} else if (type?.category === TypeCategory.Unknown) {
return;
} else if (
(type?.category === TypeCategory.Unknown || type?.category === TypeCategory.Any) &&
(declarations === undefined || declarations.length === 0)
) {
return;
} else if (isConstantName(node.value) || (symbol && this._evaluator.isFinalVariable(symbol))) {
this._addItem(node.start, node.length, SemanticTokenTypes.variable, [SemanticTokenModifiers.readonly]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import Any

def f(l: list) -> Any:
v = l[0]
return v

g(foo)
bar = f(...)
22 changes: 22 additions & 0 deletions packages/pyright-internal/src/tests/semanticTokensProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,37 @@ if (process.platform !== 'win32' || !process.env['CI']) {
{ type: 'parameter', modifiers: ['definition'], start: 140, length: 1 }, // x
{ type: 'parameter', modifiers: [], start: 159, length: 1 }, // x
{ type: 'parameter', modifiers: [], start: 163, length: 1 }, // y
{ type: 'variable', modifiers: [], start: 169, length: 1 }, // z
{ type: 'parameter', modifiers: [], start: 177, length: 1 }, // x
{ type: 'function', modifiers: [], start: 190, length: 1 }, // g
{ type: 'variable', modifiers: [], start: 192, length: 1 }, // z
// lambda
{ type: 'parameter', modifiers: ['definition'], start: 203, length: 1 }, // a
{ type: 'parameter', modifiers: ['definition'], start: 206, length: 1 }, // b
{ type: 'parameter', modifiers: [], start: 209, length: 1 }, // a
{ type: 'parameter', modifiers: [], start: 213, length: 1 }, // b
]);
});

test('Unknown and Any', () => {
const result = semanticTokenizeSampleFile('unknown.py');
expect(result).toStrictEqual([
{ type: 'namespace', modifiers: [], start: 5, length: 6 }, // typing
{ type: 'variable', modifiers: [], start: 19, length: 3 }, // Any
{ type: 'variable', modifiers: [], start: 19, length: 3 },
{ type: 'function', modifiers: ['definition'], start: 28, length: 1 }, // f
{ type: 'function', modifiers: [], start: 28, length: 1 },
{ type: 'parameter', modifiers: ['definition'], start: 30, length: 1 }, // l
{ type: 'class', modifiers: ['defaultLibrary', 'builtin'], start: 33, length: 4 }, // list
{ type: 'variable', modifiers: [], start: 42, length: 3 }, // Any
{ type: 'variable', modifiers: [], start: 51, length: 1 }, // v
{ type: 'parameter', modifiers: [], start: 55, length: 1 }, // l
{ type: 'variable', modifiers: [], start: 71, length: 1 }, // v
// `g` and `foo` should be ignored
{ type: 'variable', modifiers: [], start: 81, length: 3 }, // bar
{ type: 'function', modifiers: [], start: 87, length: 1 }, // f
]);
});
} else {
// prevent jest from failing because no tests were found
test('windows placeholder', () => {});
Expand Down

0 comments on commit 3c860e2

Please sign in to comment.