Changed hover provider output for type aliases to conform more closely with the new Python 3.12 type syntax. This partially addresses #8185.

This commit is contained in:
Eric Traut 2024-06-21 12:41:22 +02:00
parent 1bbb5d90a1
commit 221a977705
4 changed files with 22 additions and 17 deletions

View File

@ -19,8 +19,9 @@ import {
} from '../analyzer/declaration';
import * as ParseTreeUtils from '../analyzer/parseTreeUtils';
import { SourceMapper } from '../analyzer/sourceMapper';
import { isBuiltInModule } from '../analyzer/typeDocStringUtils';
import { PrintTypeOptions, TypeEvaluator } from '../analyzer/typeEvaluatorTypes';
import { doForEachSubtype, isMaybeDescriptorInstance } from '../analyzer/typeUtils';
import { convertToInstance, doForEachSubtype, isMaybeDescriptorInstance } from '../analyzer/typeUtils';
import {
ClassType,
Type,
@ -38,6 +39,7 @@ import { SignatureDisplayType } from '../common/configOptions';
import { assertNever, fail } from '../common/debug';
import { ProgramView } from '../common/extensibility';
import { convertOffsetToPosition, convertPositionToOffset } from '../common/positionUtils';
import { ServiceProvider } from '../common/serviceProvider';
import { Position, Range, TextRange } from '../common/textRange';
import { Uri } from '../common/uri/uri';
import { ExpressionNode, NameNode, ParseNode, ParseNodeType, StringNode } from '../parser/parseNodes';
@ -49,8 +51,6 @@ import {
getToolTipForType,
getTypeForToolTip,
} from './tooltipUtils';
import { ServiceProvider } from '../common/serviceProvider';
import { isBuiltInModule } from '../analyzer/typeDocStringUtils';
export interface HoverTextPart {
python?: boolean;
@ -134,8 +134,9 @@ export function getVariableTypeText(
) {
let label = declaration.isConstant || evaluator.isFinalVariableDeclaration(declaration) ? 'constant' : 'variable';
let expandTypeAlias = false;
const expandTypeAlias = false;
let typeVarName: string | undefined;
if (type.typeAliasInfo && typeNode.nodeType === ParseNodeType.Name) {
const typeAliasInfo = getTypeAliasInfo(type);
if (typeAliasInfo?.name === typeNode.value) {
@ -143,8 +144,12 @@ export function getVariableTypeText(
label = type.details.isParamSpec ? 'param spec' : 'type variable';
typeVarName = type.details.name;
} else {
expandTypeAlias = true;
label = 'type alias';
// Handle type aliases specially.
const typeText = evaluator.printType(convertToInstance(getTypeForToolTip(evaluator, typeNode)), {
expandTypeAlias: true,
});
return `(type) ${name} = ` + typeText;
}
}
}
@ -155,7 +160,7 @@ export function getVariableTypeText(
}
const typeText =
typeVarName || name + ': ' + evaluator.printType(getTypeForToolTip(evaluator, typeNode), { expandTypeAlias });
typeVarName ?? name + ': ' + evaluator.printType(getTypeForToolTip(evaluator, typeNode), { expandTypeAlias });
return `(${label}) ` + typeText;
}
@ -412,8 +417,9 @@ export class HoverProvider {
}
case DeclarationType.TypeAlias: {
const typeText = node.value + this._getTypeText(node, { expandTypeAlias: true });
this._addResultsPart(parts, `(type alias) ${typeText}`, /* python */ true);
const type = convertToInstance(this._getType(node));
const typeText = this._evaluator.printType(type, { expandTypeAlias: true });
this._addResultsPart(parts, `(type) ${node.value} = ${typeText}`, /* python */ true);
this._addDocumentationPart(parts, node, resolvedDecl);
break;
}

View File

@ -32,9 +32,9 @@
////
helper.verifyHover('markdown', {
marker1: '```python\n(type alias) AliasA: type[ClassA]\n```\n---\nAliasA doc string\n\nClassA doc string',
marker2: '```python\n(type alias) AliasA: type[ClassA]\n```\n---\nAliasA doc string\n\nClassA doc string',
marker3: '```python\n(type alias) AliasB: type[ClassB]\n```\n---\nAliasB alone doc string',
marker4: '```python\n(type alias) AliasC: type[ClassC]\n```\n---\nAliasC doc string\n\nClassC doc string',
marker5: '```python\n(type alias) AliasD: type[ClassD]\n```\n---\nAliasD alone doc string',
marker1: '```python\n(type) AliasA = ClassA\n```\n---\nAliasA doc string\n\nClassA doc string',
marker2: '```python\n(type) AliasA = ClassA\n```\n---\nAliasA doc string\n\nClassA doc string',
marker3: '```python\n(type) AliasB = ClassB\n```\n---\nAliasB alone doc string',
marker4: '```python\n(type) AliasC = ClassC\n```\n---\nAliasC doc string\n\nClassC doc string',
marker5: '```python\n(type) AliasD = ClassD\n```\n---\nAliasD alone doc string',
});

View File

@ -32,7 +32,7 @@
helper.verifyHover('markdown', {
marker1: '```python\nclass C1(name: str = "hello")\n```\n---\n\\_\\_init\\_\\_ docs',
marker2: '```python\n(type alias) unionType: type[C1] | type[C2]\n```',
marker2: '```python\n(type) unionType = C1 | C2\n```',
marker3: '```python\nclass G(value: int)\n```',
marker4: '```python\nclass G(value: int)\n```',
marker5: '```python\nclass C1(name: str = "hello")\n```\n---\n\\_\\_init\\_\\_ docs',

View File

@ -44,7 +44,6 @@ helper.verifyHover('markdown', {
marker2: '```python\n(variable) def func(float) -> float\n```\n---\nA given function',
marker3: '```python\n(variable) y: Literal[2]\n```\n---\ntest y',
marker4: '```python\n(variable) z: int\n```\n---\ntest z',
marker5:
"```python\n(type alias) SomeType: type[List[int | str]]\n```\n---\nHere's some documentation about SomeType",
marker5: "```python\n(type) SomeType = List[int | str]\n```\n---\nHere's some documentation about SomeType",
marker6: '```python\n(variable) x: Literal[123670029844611072]\n```',
});