Skip to content
40 changes: 36 additions & 4 deletions packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6112,6 +6112,33 @@ export class Checker extends ParseTreeWalker {
const childClassSelf = ClassType.cloneAsInstance(
selfSpecializeClass(childClassType, { useBoundTypeVars: true })
);

let baseClassSelf = ClassType.isProtocolClass(overriddenClassAndSymbol.classType)
? childClassSelf
: ClassType.cloneAsInstance(
selfSpecializeClass(overriddenClassAndSymbol.classType, { useBoundTypeVars: true })
);

overriddenType = partiallySpecializeType(
overriddenType,
overriddenClassAndSymbol.classType,
this._evaluator.getTypeClassType(),
baseClassSelf
);

overrideType = partiallySpecializeType(
overrideType,
overrideClassAndSymbol.classType,
this._evaluator.getTypeClassType(),
childClassSelf
) as FunctionType | OverloadedType;

if (childClassType.shared.typeVarScopeId) {
overriddenType = makeTypeVarsBound(overriddenType, [childClassType.shared.typeVarScopeId]);
overrideType = makeTypeVarsBound(overrideType, [childClassType.shared.typeVarScopeId]);
Comment thread
crypt0lith marked this conversation as resolved.
Resolved
baseClassSelf = makeTypeVarsBound(baseClassSelf, [childClassType.shared.typeVarScopeId]);
}

const { baseType: overriddenTypeForComparison, overrideType: overrideTypeForComparison } =
this._getCallableVariableOverrideComparison(
overriddenClassAndSymbol.symbol,
Expand All @@ -6126,9 +6153,11 @@ export class Checker extends ParseTreeWalker {
!this._evaluator.validateOverrideMethod(
overriddenTypeForComparison,
overrideTypeForComparison,
/* baseClass */ undefined,
childClassType,
diagAddendum,
/* enforceParamNameMatch */ true
/* enforceParamNameMatch */ true,
baseClassSelf,
childClassSelf
)
) {
if (overrideDecl && overrideDecl.type === DeclarationType.Function) {
Expand Down Expand Up @@ -6955,7 +6984,7 @@ export class Checker extends ParseTreeWalker {
// The "Self" value for the base class depends on whether it's a
// protocol or not. It's not clear from the typing spec whether
// this is the correct behavior.
const baseClassSelf = ClassType.isProtocolClass(baseClass)
let baseClassSelf = ClassType.isProtocolClass(baseClass)
? childClassSelf
: ClassType.cloneAsInstance(selfSpecializeClass(baseClass, { useBoundTypeVars: true }));

Expand All @@ -6976,6 +7005,7 @@ export class Checker extends ParseTreeWalker {
if (childClassType.shared.typeVarScopeId) {
overrideType = makeTypeVarsBound(overrideType, [childClassType.shared.typeVarScopeId]);
baseType = makeTypeVarsBound(baseType, [childClassType.shared.typeVarScopeId]);
baseClassSelf = makeTypeVarsBound(baseClassSelf, [childClassType.shared.typeVarScopeId]);
}

// Determine whether this is an attempt to override a method marked @final.
Expand Down Expand Up @@ -7042,7 +7072,9 @@ export class Checker extends ParseTreeWalker {
overrideTypeForComparison,
childClassType,
diagAddendum,
enforceParamNameMatch
enforceParamNameMatch,
baseClassSelf,
childClassSelf
)
) {
return;
Expand Down
8 changes: 6 additions & 2 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18594,14 +18594,18 @@ export function createTypeEvaluator(
overrideMethod: FunctionType | OverloadedType,
baseClass: ClassType | undefined,
diag: DiagnosticAddendum,
enforceParamNames = true
enforceParamNames = true,
baseClassSelf?: ClassType,
childClassSelf?: ClassType
): boolean {
return overrideValidation.validateOverrideMethod(
baseMethod,
overrideMethod,
baseClass,
diag,
enforceParamNames
enforceParamNames,
baseClassSelf,
childClassSelf
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import {
isFunction,
isFunctionOrOverloaded,
isTypeVar,
isUnion,
OverloadedType,
Type,
TypeVarType,
} from '../types';
import { ApplyTypeVarOptions } from '../typeUtils';
import { ApplyTypeVarOptions, getTypeVarScopeIds, mapSubtypes } from '../typeUtils';

interface Context {
readonly assignType: TypeEvaluator['assignType'];
Expand All @@ -47,7 +49,9 @@ export function createOverrideValidation(context: Context) {
overrideMethod: FunctionType | OverloadedType,
baseClass: ClassType | undefined,
diag: DiagnosticAddendum,
enforceParamNames = true
enforceParamNames = true,
baseClassSelf?: ClassType,
childClassSelf?: ClassType
): boolean {
// If we're overriding a non-method with a method, report it as an error.
// This occurs when a non-property overrides a property.
Expand All @@ -57,9 +61,16 @@ export function createOverrideValidation(context: Context) {
}

if (isFunction(baseMethod)) {
const specializedBaseMethod = partiallySpecializeOverrideMethod(baseMethod, baseClassSelf);

// Handle the easy case - a simple function overriding another simple function.
if (isFunction(overrideMethod)) {
return validateOverrideMethodInternal(baseMethod, overrideMethod, diag, enforceParamNames);
return validateOverrideMethodInternal(
specializedBaseMethod,
partiallySpecializeOverrideMethod(overrideMethod, childClassSelf),
diag,
enforceParamNames
);
}

const overloadsAndImpl = [...OverloadedType.getOverloads(overrideMethod)];
Expand All @@ -73,8 +84,8 @@ export function createOverrideValidation(context: Context) {
if (
overloadsAndImpl.some((overrideOverload) => {
return validateOverrideMethodInternal(
baseMethod,
overrideOverload,
specializedBaseMethod,
partiallySpecializeOverrideMethod(overrideOverload, childClassSelf),
/* diag */ undefined,
enforceParamNames
);
Expand All @@ -90,88 +101,116 @@ export function createOverrideValidation(context: Context) {
// For a non-overloaded method overriding an overloaded method, the
// override must match all of the overloads.
if (isFunction(overrideMethod)) {
const specializedOverrideMethod = partiallySpecializeOverrideMethod(overrideMethod, childClassSelf);

return OverloadedType.getOverloads(baseMethod).every((overload) => {
// If the override isn't applicable for this base class, skip the check.
if (baseClass && !isOverrideMethodApplicable(overload, baseClass)) {
return true;
}

return validateOverrideMethodInternal(
overload,
overrideMethod,
partiallySpecializeOverrideMethod(overload, baseClassSelf),
specializedOverrideMethod,
diag?.createAddendum(),
enforceParamNames
);
});
}

// For an overloaded method overriding an overloaded method, the overrides
// must all match and be in the correct order. It is OK if the base method
// has additional overloads that are not present in the override.
// For an overloaded method overriding an overloaded method, each base
// overload must be matched by an override overload, and the matches must
// be in the same order. It is OK if the override has additional overloads
// that are not present in the base method.

let previousMatchIndex = -1;
const baseOverloads = OverloadedType.getOverloads(baseMethod);
const overrideOverloads = OverloadedType.getOverloads(overrideMethod).map((overrideOverload) => {
return partiallySpecializeOverrideMethod(overrideOverload, childClassSelf);
});

for (let baseOverload of OverloadedType.getOverloads(baseMethod)) {
// If the override isn't applicable for this base class, skip the check.
if (baseClass && !isOverrideMethodApplicable(baseOverload, baseClass)) {
continue;
}

for (const overrideOverload of OverloadedType.getOverloads(overrideMethod)) {
let possibleMatchIndex: number | undefined;
baseOverload = partiallySpecializeOverrideMethod(baseOverload, baseClassSelf);

let matchIndex = baseOverloads.findIndex((baseOverload, index) => {
// If the override isn't applicable for this base class, skip the check.
if (baseClass && !isOverrideMethodApplicable(baseOverload, baseClass)) {
return false;
}

const isCompatible = validateOverrideMethodInternal(
const isCompatible = (overrideOverload: FunctionType) => {
return validateOverrideMethodInternal(
baseOverload,
overrideOverload,
/* diag */ undefined,
enforceParamNames
);
};

// If the override is compatible but the match is one that is below the previous
// matched index, keep looking for additional matches. Record the fact that
// we found at least one match.
if (isCompatible && index <= previousMatchIndex && possibleMatchIndex === undefined) {
possibleMatchIndex = index;
return false;
}

return isCompatible;
const matchIndex = overrideOverloads.findIndex((overrideOverload, index) => {
return index >= previousMatchIndex && isCompatible(overrideOverload);
});

if (matchIndex < 0 && possibleMatchIndex !== undefined) {
matchIndex = possibleMatchIndex;
}

if (matchIndex < 0) {
break;
if (overrideOverloads.some((overrideOverload) => isCompatible(overrideOverload))) {
diag.addMessage(LocAddendum.overrideOverloadOrder());
} else {
diag.addMessage(LocAddendum.overrideOverloadNoMatch());
}
return false;
}

if (matchIndex < previousMatchIndex) {
diag.addMessage(LocAddendum.overrideOverloadOrder());
// An additional overload ahead of the match must not overlap the
// base overload and return an incompatible type.
const isOverlapping = overrideOverloads.some((overrideOverload, index) => {
return index < matchIndex && isOverrideMethodOverlapping(baseOverload, overrideOverload);
});

if (isOverlapping) {
diag.addMessage(LocAddendum.overrideOverloadNoMatch());
return false;
}

previousMatchIndex = matchIndex;
}

if (previousMatchIndex < baseOverloads.length - 1) {
const unmatchedOverloads = baseOverloads.slice(previousMatchIndex + 1);
return true;
}

// See if all of the remaining overrides are nonapplicable.
if (
!baseClass ||
unmatchedOverloads.some((overload) => {
return isOverrideMethodApplicable(overload, baseClass);
})
) {
// We didn't find matches for all of the base overloads.
diag.addMessage(LocAddendum.overrideOverloadNoMatch());
function isOverrideMethodOverlapping(baseOverload: FunctionType, overrideOverload: FunctionType): boolean {
if (
FunctionType.isInstanceMethod(overrideOverload) ||
FunctionType.isClassMethod(overrideOverload) ||
FunctionType.isConstructorMethod(overrideOverload)
) {
baseOverload = FunctionType.clone(baseOverload, /* stripFirstParam */ true);
overrideOverload = FunctionType.clone(overrideOverload, /* stripFirstParam */ true);
}

const flags = AssignTypeFlags.SkipReturnTypeCheck;
const partialOverlapFlags =
flags |
AssignTypeFlags.OverloadOverlap |
AssignTypeFlags.PartialOverloadOverlap |
AssignTypeFlags.IncludeOverlapByOmission;

const checks: [FunctionType, FunctionType, AssignTypeFlags][] = [
[baseOverload, overrideOverload, flags],
[overrideOverload, baseOverload, flags],
[overrideOverload, baseOverload, partialOverlapFlags],
];

// Compare the return types with the TypeVars solved by the
// parameters the two overloads share.
return checks.some(([destType, srcType, checkFlags]) => {
const constraints = new ConstraintTracker();
if (!assignType(destType, srcType, /* diag */ undefined, constraints, checkFlags)) {
return false;
}
}

return true;
return !assignType(
solveAndApplyConstraints(getEffectiveReturnType(baseOverload), constraints),
solveAndApplyConstraints(getEffectiveReturnType(overrideOverload), constraints)
);
});
}

function isOverrideMethodApplicable(baseMethod: FunctionType, childClass: ClassType): boolean {
Expand Down Expand Up @@ -212,6 +251,33 @@ export function createOverrideValidation(context: Context) {
);
}

// Solves the TypeVars in an annotated "self" or "cls" parameter against
// the class that "self" refers to, as is done when the method is bound.
function partiallySpecializeOverrideMethod(method: FunctionType, selfClass: ClassType | undefined): FunctionType {
if (
!selfClass ||
method.shared.parameters.length === 0 ||
!FunctionParam.isTypeDeclared(method.shared.parameters[0]) ||
!(
FunctionType.isInstanceMethod(method) ||
FunctionType.isClassMethod(method) ||
FunctionType.isConstructorMethod(method)
)
) {
return method;
}

const constraints = new ConstraintTracker();
const selfType = FunctionType.isInstanceMethod(method) ? selfClass : ClassType.cloneAsInstantiable(selfClass);

if (!assignType(FunctionType.getParamType(method, 0), selfType, /* diag */ undefined, constraints)) {
return method;
}

const specializedFunction = solveAndApplyConstraints(method, constraints);
return isFunction(specializedFunction) ? specializedFunction : method;
}

function validateOverrideMethodInternal(
baseMethod: FunctionType,
overrideMethod: FunctionType,
Expand Down Expand Up @@ -609,7 +675,30 @@ export function createOverrideValidation(context: Context) {

// Now check the return type.
const baseReturnType = getEffectiveReturnType(baseMethod);
const overrideReturnType = solveAndApplyConstraints(getEffectiveReturnType(overrideMethod), constraints);
let overrideReturnType = solveAndApplyConstraints(getEffectiveReturnType(overrideMethod), constraints);

// Replace unsolved TypeVars of the override in a union with their
// default, or eliminate them if they have no default, as is done for
// call return types.
const overrideScopeIds = getTypeVarScopeIds(overrideMethod);
const isUnsolvedTypeVar = (subtype: Type): subtype is TypeVarType => {
return isTypeVar(subtype) && !!subtype.priv.scopeId && overrideScopeIds.includes(subtype.priv.scopeId);
};
if (
isUnion(overrideReturnType) &&
!overrideReturnType.priv.subtypes.every((subtype) => {
return isUnsolvedTypeVar(subtype) && !subtype.shared.isDefaultExplicit;
})
) {
overrideReturnType = mapSubtypes(overrideReturnType, (subtype) => {
if (isUnsolvedTypeVar(subtype)) {
return subtype.shared.isDefaultExplicit
? solveAndApplyConstraints(subtype.shared.defaultType, constraints)
: undefined;
}
return subtype;
});
}

if (
!assignType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,7 @@ export function createTypeCompatibility(context: Context) {
if ((flags & AssignTypeFlags.PartialOverloadOverlap) !== 0) {
if (srcParam.defaultType) {
if (
(flags & AssignTypeFlags.IncludeOverlapByOmission) === 0 &&
isOverlapOnlyByOmission(destParamType, destParam.defaultType, srcParamType) &&
checkParamListOmission()
) {
Expand Down Expand Up @@ -1716,6 +1717,7 @@ export function createTypeCompatibility(context: Context) {
if (srcParamInfo.defaultType && destParamInfo.defaultType) {
if ((flags & AssignTypeFlags.PartialOverloadOverlap) !== 0) {
if (
(flags & AssignTypeFlags.IncludeOverlapByOmission) === 0 &&
isOverlapOnlyByOmission(destParamInfo.type, destParamInfo.defaultType, srcParamType) &&
checkParamListOmission()
) {
Expand Down
Loading