Skip to content

Commit

Permalink
Added OmitTypeVarScopes flag to typePrinter. It's not currently used,…
Browse files Browse the repository at this point in the history
… but we're considering switching to this.
  • Loading branch information
erictraut committed Oct 2, 2024
1 parent 03eda6e commit 39a5a20
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
29 changes: 18 additions & 11 deletions packages/pyright-internal/src/analyzer/typePrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export const enum PrintTypeFlags {
// Use the fully-qualified name of classes, type aliases, modules,
// and functions rather than short names.
UseFullyQualifiedNames = 1 << 12,

// Omit TypeVar scopes.
OmitTypeVarScopes = 1 << 13,
}

export type FunctionReturnTypeCallback = (type: FunctionType) => Type;
Expand Down Expand Up @@ -571,10 +574,13 @@ function printTypeInternal(
);

if (!isAnyOrUnknown(type.shared.boundType)) {
if (printTypeFlags & PrintTypeFlags.PythonSyntax) {
boundTypeString = `Self`;
} else {
if (
(printTypeFlags & PrintTypeFlags.PythonSyntax) === 0 &&
(printTypeFlags & PrintTypeFlags.OmitTypeVarScopes) === 0
) {
boundTypeString = `Self@${boundTypeString}`;
} else {
boundTypeString = `Self`;
}
}

Expand All @@ -593,7 +599,8 @@ function printTypeInternal(
if (isParamSpec(type)) {
const paramSpecText = _getReadableTypeVarName(
type,
(printTypeFlags & PrintTypeFlags.PythonSyntax) !== 0
(printTypeFlags & PrintTypeFlags.PythonSyntax) === 0 &&
(printTypeFlags & PrintTypeFlags.OmitTypeVarScopes) === 0
);

if (type.priv.paramSpecAccess) {
Expand All @@ -602,7 +609,11 @@ function printTypeInternal(
return paramSpecText;
}

let typeVarName = _getReadableTypeVarName(type, (printTypeFlags & PrintTypeFlags.PythonSyntax) !== 0);
let typeVarName = _getReadableTypeVarName(
type,
(printTypeFlags & PrintTypeFlags.PythonSyntax) === 0 &&
(printTypeFlags & PrintTypeFlags.OmitTypeVarScopes) === 0
);

if (isTypeVarTuple(type)) {
if (type.priv.isUnpacked) {
Expand Down Expand Up @@ -1298,12 +1309,8 @@ function _printNestedInstantiable(type: Type, textToWrap: string) {
return textToWrap;
}

function _getReadableTypeVarName(type: TypeVarType, usePythonSyntax: boolean) {
if (usePythonSyntax) {
return type.shared.name;
}

return TypeVarType.getReadableName(type);
function _getReadableTypeVarName(type: TypeVarType, includeScope: boolean) {
return TypeVarType.getReadableName(type, includeScope);
}

function _getTypeVarVarianceText(type: TypeVarType) {
Expand Down
4 changes: 2 additions & 2 deletions packages/pyright-internal/src/analyzer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3075,8 +3075,8 @@ export namespace TypeVarType {
return typeVarType.priv.nameWithScope || typeVarType.shared.name;
}

export function getReadableName(type: TypeVarType) {
if (type.priv.scopeName) {
export function getReadableName(type: TypeVarType, includeScope = true) {
if (type.priv.scopeName && includeScope) {
return `${type.shared.name}@${type.priv.scopeName}`;
}

Expand Down

0 comments on commit 39a5a20

Please sign in to comment.