Skip to content

Commit

Permalink
fix Never type aliases being coloured as a variable when using the …
Browse files Browse the repository at this point in the history
…new python 3.12 type alias syntax
  • Loading branch information
DetachHead committed Jun 10, 2024
1 parent 06301d3 commit ec69125
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
12 changes: 7 additions & 5 deletions packages/pyright-internal/src/analyzer/semanticTokensWalker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ParseTreeWalker } from './parseTreeWalker';
import { TypeEvaluator } from './typeEvaluatorTypes';
import { FunctionType, OverloadedFunctionType, Type, TypeCategory, TypeFlags } from './types';
import { ClassType, FunctionType, OverloadedFunctionType, Type, TypeCategory, TypeFlags } from './types';
import {
ClassNode,
DecoratorNode,
Expand Down Expand Up @@ -206,12 +206,14 @@ export class SemanticTokensWalker extends ParseTreeWalker {
}
const symbol = this._evaluator?.lookUpSymbolRecursive(node, node.value, false)?.symbol;
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 (
// check for new python 3.12 type alias syntax
(typeResult.type.specialForm && ClassType.isBuiltIn(typeResult.type.specialForm, 'TypeAliasType')) ||
// 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
(typeResult.type.category !== TypeCategory.Never &&
typeResult.type.category !== TypeCategory.Unbound &&
typeResult.type.flags & TypeFlags.Instantiable) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ def inferred():

value2: str = ''
if not isinstance(value2, str):
value2
value2

type Baz = Never
baz: Baz
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ if (process.platform !== 'win32' || !process.env['CI']) {
{ type: 'variable', modifiers: [], start: 293, length: 6 }, // value2
{ type: 'class', modifiers: ['defaultLibrary', 'builtin'], start: 301, length: 3 }, // str
{ type: 'variable', modifiers: [], start: 315, length: 6 }, // value2
{ type: 'keyword', modifiers: [], start: 343, length: 4 }, // type
{ type: 'type', modifiers: [], start: 348, length: 3 }, // Baz
{ type: 'type', modifiers: [], start: 354, length: 5 }, // Never
{ type: 'variable', modifiers: [], start: 361, length: 5 }, // baz
{ type: 'type', modifiers: [], start: 366, length: 3 }, // Baz
]);
});

Expand Down

0 comments on commit ec69125

Please sign in to comment.