Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved the handling of a TypeVar whose definition involves a circul… #9377

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading