Fixed a bug that resulted in a crash under certain circumstances when defining a type alias using a type statement when no type parameters are defined. This addresses #6664.

This commit is contained in:
Eric Traut 2023-12-06 10:37:15 -08:00
parent 221f55591c
commit 0e71f1dbf9
2 changed files with 24 additions and 20 deletions

View File

@ -12529,7 +12529,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
valueExpr, valueExpr,
/* isPep695Syntax */ false, /* isPep695Syntax */ false,
/* typeParamNodes */ undefined, /* typeParamNodes */ undefined,
() => typeParameters ?? [] () => typeParameters
); );
} }
@ -15170,6 +15170,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
name: NameNode, name: NameNode,
errorNode: ExpressionNode, errorNode: ExpressionNode,
isPep695Syntax: boolean, isPep695Syntax: boolean,
isPep695TypeVarType: boolean,
typeParameters?: TypeVarType[], typeParameters?: TypeVarType[],
typeParamNodes?: TypeParameterNode[] typeParamNodes?: TypeParameterNode[]
): Type { ): Type {
@ -15228,21 +15229,22 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
); );
} }
const fileInfo = AnalyzerNodeInfo.getFileInfo(name); if (!isPep695Syntax && !isPep695TypeVarType) {
const boundTypeVars = typeParameters.filter(
const boundTypeVars = typeParameters.filter( (typeVar) => typeVar.scopeId !== typeAliasScopeId && typeVar.scopeType === TypeVarScopeType.Class
(typeVar) => typeVar.scopeId !== typeAliasScopeId && typeVar.scopeType === TypeVarScopeType.Class
);
if (boundTypeVars.length > 0) {
addError(
Localizer.Diagnostic.genericTypeAliasBoundTypeVar().format({
names: boundTypeVars.map((t) => `${t.details.name}`).join(', '),
}),
errorNode
); );
if (boundTypeVars.length > 0) {
addError(
Localizer.Diagnostic.genericTypeAliasBoundTypeVar().format({
names: boundTypeVars.map((t) => `${t.details.name}`).join(', '),
}),
errorNode
);
}
} }
const fileInfo = AnalyzerNodeInfo.getFileInfo(name);
const typeAlias = TypeBase.cloneForTypeAlias( const typeAlias = TypeBase.cloneForTypeAlias(
type, type,
name.value, name.value,
@ -15254,7 +15256,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
// All PEP 695 type aliases are special forms because they are // All PEP 695 type aliases are special forms because they are
// TypeAliasType objects at runtime. // TypeAliasType objects at runtime.
if (isPep695Syntax) { if (isPep695Syntax || isPep695TypeVarType) {
typeAlias.flags |= TypeFlags.SpecialForm; typeAlias.flags |= TypeFlags.SpecialForm;
} }
@ -15582,7 +15584,8 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
rightHandType, rightHandType,
typeAliasNameNode, typeAliasNameNode,
node.rightExpression, node.rightExpression,
/* isPep695Syntax */ false /* isPep695Syntax */ false,
/* isPep695TypeVarType */ false
); );
assert(typeAliasTypeVar !== undefined); assert(typeAliasTypeVar !== undefined);
@ -15670,11 +15673,10 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
/* isPep695Syntax */ true, /* isPep695Syntax */ true,
node.typeParameters?.parameters, node.typeParameters?.parameters,
() => { () => {
let typeParameters: TypeVarType[] = [];
if (node.typeParameters) { if (node.typeParameters) {
typeParameters = evaluateTypeParameterList(node.typeParameters); return evaluateTypeParameterList(node.typeParameters);
} }
return typeParameters; return undefined;
} }
); );
} }
@ -15739,6 +15741,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
nameNode, nameNode,
valueNode, valueNode,
isPep695Syntax, isPep695Syntax,
/* isPep695TypeVarType */ true,
typeParameters, typeParameters,
typeParamNodes typeParamNodes
); );
@ -20788,7 +20791,8 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
inferredType, inferredType,
resolvedDecl.typeAliasName, resolvedDecl.typeAliasName,
resolvedDecl.node, resolvedDecl.node,
/* isPep695Syntax */ false /* isPep695Syntax */ false,
/* isPep695TypeVarType */ false
); );
isUnambiguousType = true; isUnambiguousType = true;

View File

@ -255,7 +255,7 @@ function printTypeInternal(
const typeParams = type.typeAliasInfo.typeParameters; const typeParams = type.typeAliasInfo.typeParameters;
if (typeParams) { if (typeParams && typeParams.length > 0) {
let argumentStrings: string[] | undefined; let argumentStrings: string[] | undefined;
// If there is a type arguments array, it's a specialized type alias. // If there is a type arguments array, it's a specialized type alias.