Skip to content
Open
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
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6126,7 +6126,7 @@ export class Checker extends ParseTreeWalker {
!this._evaluator.validateOverrideMethod(
overriddenTypeForComparison,
overrideTypeForComparison,
/* baseClass */ undefined,
childClassType,
diagAddendum,
/* enforceParamNameMatch */ true
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
Expand All @@ -75,6 +75,7 @@ export function createOverrideValidation(context: Context) {
return validateOverrideMethodInternal(
baseMethod,
overrideOverload,
baseClass,
/* diag */ undefined,
enforceParamNames
);
Expand All @@ -99,6 +100,7 @@ export function createOverrideValidation(context: Context) {
return validateOverrideMethodInternal(
overload,
overrideMethod,
baseClass,
diag?.createAddendum(),
enforceParamNames
);
Expand All @@ -124,6 +126,7 @@ export function createOverrideValidation(context: Context) {
const isCompatible = validateOverrideMethodInternal(
baseOverload,
overrideOverload,
baseClass,
/* diag */ undefined,
enforceParamNames
);
Expand Down Expand Up @@ -215,6 +218,7 @@ export function createOverrideValidation(context: Context) {
function validateOverrideMethodInternal(
baseMethod: FunctionType,
overrideMethod: FunctionType,
childClass: ClassType | undefined,
diag: DiagnosticAddendum | undefined,
enforceParamNames: boolean
): boolean {
Expand Down Expand Up @@ -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,
Comment thread
crypt0lith marked this conversation as resolved.
Resolved
diag?.createAddendum(),
/* constraints */ undefined,
AssignTypeFlags.Default
)
) {
diag?.addMessage(
LocAddendum.overrideParamType().format({
index: i + 1,
baseType: printType(baseParamType),
overrideType: printType(overrideParamType),
})
);
canOverride = false;
}

continue;
}
}
Expand Down
79 changes: 78 additions & 1 deletion packages/pyright-internal/src/tests/samples/methodOverride2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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]): ...
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/typeEvaluator3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down