Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Never type aliases being coloured as a variable when using the new python 3.12 type alias syntax #403

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 323, length: 4 }, // type
{ type: 'type', modifiers: [], start: 328, length: 3 }, // Baz
{ type: 'type', modifiers: [], start: 334, length: 5 }, // Never
{ type: 'variable', modifiers: [], start: 340, length: 3 }, // baz
{ type: 'type', modifiers: [], start: 345, length: 3 }, // Baz
]);
});

Expand Down
Loading