Skip to content

Commit

Permalink
Improved the handling of a TypeVar whose definition involves a circul…
Browse files Browse the repository at this point in the history
…ar definition. This partially addresses #9353.
  • Loading branch information
erictraut committed Nov 3, 2024
1 parent 8ee65a6 commit 01786a5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
6 changes: 6 additions & 0 deletions packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5357,6 +5357,12 @@ export class Checker extends ParseTreeWalker {
return;
}

// Skip type variables that have been internally synthesized
// for a variety of reasons.
if (param.shared.isSynthesized) {
return;
}

// Skip type variables with auto-variance.
if (param.shared.declaredVariance === Variance.Auto) {
return;
Expand Down
25 changes: 21 additions & 4 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17232,8 +17232,7 @@ export function createTypeEvaluator(
arg.d.valueExpr
);
}
genericTypeParams = [];
addTypeVarsToListIfUnique(genericTypeParams, getTypeVarArgsRecursive(argType));
genericTypeParams = buildTypeParamsFromTypeArgs(argType);
}
}
} else if (
Expand All @@ -17249,8 +17248,7 @@ export function createTypeEvaluator(
arg.d.valueExpr
);
}
protocolTypeParams = [];
addTypeVarsToListIfUnique(protocolTypeParams, getTypeVarArgsRecursive(argType));
protocolTypeParams = buildTypeParamsFromTypeArgs(argType);

if (node.d.typeParams && protocolTypeParams.length > 0) {
addDiagnostic(
Expand Down Expand Up @@ -17749,6 +17747,25 @@ export function createTypeEvaluator(
});
}

function buildTypeParamsFromTypeArgs(classType: ClassType): TypeVarType[] {
const typeParams: TypeVarType[] = [];
const typeArgs = classType.priv.typeArgs ?? [];

typeArgs.forEach((typeArg, index) => {
if (isTypeVar(typeArg)) {
typeParams.push(typeArg);
return;
}

// Synthesize a dummy type parameter.
const typeVar = TypeVarType.createInstance(`__P${index}`);
typeVar.shared.isSynthesized = true;
typeParams.push(typeVar);
});

return typeParams;
}

// Determines whether the type parameters has a default that refers to another
// type parameter. If so, validates that it is in the list of "live" type
// parameters and updates the scope of the type parameter referred to in the
Expand Down

0 comments on commit 01786a5

Please sign in to comment.