diff --git a/packages/pyright-internal/src/analyzer/checker.ts b/packages/pyright-internal/src/analyzer/checker.ts index 67e61b6ab949..9f3df799cad2 100644 --- a/packages/pyright-internal/src/analyzer/checker.ts +++ b/packages/pyright-internal/src/analyzer/checker.ts @@ -6126,7 +6126,7 @@ export class Checker extends ParseTreeWalker { !this._evaluator.validateOverrideMethod( overriddenTypeForComparison, overrideTypeForComparison, - /* baseClass */ undefined, + childClassType, diagAddendum, /* enforceParamNameMatch */ true ) diff --git a/packages/pyright-internal/src/analyzer/typeEvaluator/overrideValidation.ts b/packages/pyright-internal/src/analyzer/typeEvaluator/overrideValidation.ts index 098c00d8b2ef..4d9f8b9fa7f0 100644 --- a/packages/pyright-internal/src/analyzer/typeEvaluator/overrideValidation.ts +++ b/packages/pyright-internal/src/analyzer/typeEvaluator/overrideValidation.ts @@ -59,7 +59,7 @@ export function createOverrideValidation(context: Context) { if (isFunction(baseMethod)) { // Handle the easy case - a simple function overriding another simple function. if (isFunction(overrideMethod)) { - return validateOverrideMethodInternal(baseMethod, overrideMethod, diag, enforceParamNames); + return validateOverrideMethodInternal(baseMethod, overrideMethod, baseClass, diag, enforceParamNames); } const overloadsAndImpl = [...OverloadedType.getOverloads(overrideMethod)]; @@ -75,6 +75,7 @@ export function createOverrideValidation(context: Context) { return validateOverrideMethodInternal( baseMethod, overrideOverload, + baseClass, /* diag */ undefined, enforceParamNames ); @@ -99,6 +100,7 @@ export function createOverrideValidation(context: Context) { return validateOverrideMethodInternal( overload, overrideMethod, + baseClass, diag?.createAddendum(), enforceParamNames ); @@ -124,6 +126,7 @@ export function createOverrideValidation(context: Context) { const isCompatible = validateOverrideMethodInternal( baseOverload, overrideOverload, + baseClass, /* diag */ undefined, enforceParamNames ); @@ -215,6 +218,7 @@ export function createOverrideValidation(context: Context) { function validateOverrideMethodInternal( baseMethod: FunctionType, overrideMethod: FunctionType, + childClass: ClassType | undefined, diag: DiagnosticAddendum | undefined, enforceParamNames: boolean ): boolean { @@ -337,6 +341,32 @@ export function createOverrideValidation(context: Context) { FunctionType.isClassMethod(overrideMethod) || FunctionType.isConstructorMethod(overrideMethod) ) { + const baseParamType = baseParamDetails.params[i].type; + const overrideParamType = overrideParamDetails.params[i].type; + + // A declared "self" or "cls" type in the base method must + // accept the child class or the override's declared type. + if ( + !(isTypeVar(baseParamType) && baseParamType.shared.isSynthesized) && + !(childClass && isOverrideMethodApplicable(baseMethod, childClass)) && + !assignType( + baseParamType, + overrideParamType, + diag?.createAddendum(), + /* constraints */ undefined, + AssignTypeFlags.Default + ) + ) { + diag?.addMessage( + LocAddendum.overrideParamType().format({ + index: i + 1, + baseType: printType(baseParamType), + overrideType: printType(overrideParamType), + }) + ); + canOverride = false; + } + continue; } } diff --git a/packages/pyright-internal/src/tests/samples/methodOverride2.py b/packages/pyright-internal/src/tests/samples/methodOverride2.py index a557a44a340a..cbb03f97ccce 100644 --- a/packages/pyright-internal/src/tests/samples/methodOverride2.py +++ b/packages/pyright-internal/src/tests/samples/methodOverride2.py @@ -2,7 +2,7 @@ # diagnostic check. -from typing import Any, Generic, ParamSpec, Self, TypeVar +from typing import Any, Generic, ParamSpec, Protocol, Self, TypeVar class Base1: @@ -92,3 +92,80 @@ def method2(self, x: "Derived3[int]") -> "Derived3[int]": ... # This should generate an error. def method3(self, x: Self) -> Self: ... + + +class HasValue4(Protocol): + value: int + + +class Base4(Protocol): + def method1(self: HasValue4) -> int: ... + + +class Derived4_1(Base4): + value: int + + def method1(self) -> int: ... + + +class Derived4_2(Base4): + def method1(self: HasValue4) -> int: ... + + +class Derived4_3(Base4): + # This should generate an error. + def method1(self) -> int: ... + + +class Base5[T]: + def method1(self: Base5[int]) -> int: ... + + @classmethod + def method2(cls: type[Base5[int]]) -> int: ... + + +class Derived5_1(Base5[int]): + def method1(self) -> int: ... + + @classmethod + def method2(cls) -> int: ... + + +class Derived5_2(Base5[str]): + # This should generate an error. + def method1(self) -> int: ... + + # This should generate an error. + @classmethod + def method2(cls) -> int: ... + + +class Derived5_3(Base5[str]): + # This should generate an error. + def method1(self: object) -> int: ... + + +class Base6: + def method1(self: Base6) -> int: ... + + @classmethod + def method2(cls: type[Base6]) -> int: ... + + +class Derived6(Base6): + def method1(self: object) -> int: ... + + @classmethod + def method2(cls: type[object]) -> int: ... + + +class Base7: + def method1(self: object) -> int: ... + + +class Derived7_1(Base7, Base6): ... + + +# This should generate an error because the base classes define +# method1 in an incompatible way. +class Derived7_2(Base7, Base5[str]): ... diff --git a/packages/pyright-internal/src/tests/typeEvaluator3.test.ts b/packages/pyright-internal/src/tests/typeEvaluator3.test.ts index 448cbe0d7df0..18d411d27349 100644 --- a/packages/pyright-internal/src/tests/typeEvaluator3.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluator3.test.ts @@ -1019,7 +1019,7 @@ test('MethodOverride2', () => { configOptions.diagnosticRuleSet.reportIncompatibleMethodOverride = 'error'; analysisResults = TestUtils.typeAnalyzeSampleFiles(['methodOverride2.py'], configOptions); - TestUtils.validateResults(analysisResults, 8); + TestUtils.validateResults(analysisResults, 13); }); test('MethodOverride3', () => {