Skip to content

Commit

Permalink
Revert "Changed hover provider output for type aliases to conform mor…
Browse files Browse the repository at this point in the history
…e closely with the new Python 3.12 `type` syntax. This partially addresses #8185."

This reverts commit 221a977.
  • Loading branch information
DetachHead committed Jul 5, 2024
1 parent 95e5794 commit e9387d2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
24 changes: 9 additions & 15 deletions packages/pyright-internal/src/languageService/hoverProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ 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 { convertToInstance, doForEachSubtype, isMaybeDescriptorInstance } from '../analyzer/typeUtils';
import { doForEachSubtype, isMaybeDescriptorInstance } from '../analyzer/typeUtils';
import {
ClassType,
Type,
Expand All @@ -39,7 +38,6 @@ 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';
Expand All @@ -51,6 +49,8 @@ import {
getToolTipForType,
getTypeForToolTip,
} from './tooltipUtils';
import { ServiceProvider } from '../common/serviceProvider';
import { isBuiltInModule } from '../analyzer/typeDocStringUtils';

export interface HoverTextPart {
python?: boolean;
Expand Down Expand Up @@ -134,22 +134,17 @@ export function getVariableTypeText(
) {
let label = declaration.isConstant || evaluator.isFinalVariableDeclaration(declaration) ? 'constant' : 'variable';

const expandTypeAlias = false;
let expandTypeAlias = false;
let typeVarName: string | undefined;

if (type.typeAliasInfo && typeNode.nodeType === ParseNodeType.Name) {
const typeAliasInfo = getTypeAliasInfo(type);
if (typeAliasInfo?.name === typeNode.value) {
if (isTypeVar(type)) {
label = type.details.isParamSpec ? 'param spec' : 'type variable';
typeVarName = type.details.name;
} else {
// Handle type aliases specially.
const typeText = evaluator.printType(convertToInstance(getTypeForToolTip(evaluator, typeNode)), {
expandTypeAlias: true,
});

return `(type) ${name} = ` + typeText;
expandTypeAlias = true;
label = 'type alias';
}
}
}
Expand All @@ -160,7 +155,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;
}
Expand Down Expand Up @@ -417,9 +412,8 @@ export class HoverProvider {
}

case DeclarationType.TypeAlias: {
const type = convertToInstance(this._getType(node));
const typeText = this._evaluator.printType(type, { expandTypeAlias: true });
this._addResultsPart(parts, `(type) ${node.value} = ${typeText}`, /* python */ true);
const typeText = node.value + this._getTypeText(node, { expandTypeAlias: true });
this._addResultsPart(parts, `(type alias) ${typeText}`, /* python */ true);
this._addDocumentationPart(parts, node, resolvedDecl);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
////

helper.verifyHover('markdown', {
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',
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',
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

helper.verifyHover('markdown', {
marker1: '```python\nclass C1(name: str = "hello")\n```\n---\n\\_\\_init\\_\\_ docs',
marker2: '```python\n(type) unionType = C1 | C2\n```',
marker2: '```python\n(type alias) unionType: type[C1] | type[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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ 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) SomeType = List[int | str]\n```\n---\nHere's some documentation about SomeType",
marker5:
"```python\n(type alias) SomeType: type[List[int | str]]\n```\n---\nHere's some documentation about SomeType",
marker6: '```python\n(variable) x: Literal[123670029844611072]\n```',
});

0 comments on commit e9387d2

Please sign in to comment.