Skip to content

Commit

Permalink
Fixed bug that results in an "incompatible method override" false neg…
Browse files Browse the repository at this point in the history
…ative when overriding a method that uses class-scoped type parameters with a method that uses method-scoped type parameters. This addresses #9405. (#9707)
  • Loading branch information
erictraut authored Jan 14, 2025
1 parent c1edc0b commit c7b2fb9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6619,7 +6619,7 @@ export class Checker extends ParseTreeWalker {
selfSpecializeClass(childClassType, { useBoundTypeVars: true })
);

const baseType = partiallySpecializeType(
let baseType = partiallySpecializeType(
this._evaluator.getEffectiveTypeOfSymbol(baseClassAndSymbol.symbol),
baseClass,
this._evaluator.getTypeClassType(),
Expand All @@ -6635,6 +6635,7 @@ export class Checker extends ParseTreeWalker {

if (childClassType.shared.typeVarScopeId) {
overrideType = makeTypeVarsBound(overrideType, [childClassType.shared.typeVarScopeId]);
baseType = makeTypeVarsBound(baseType, [childClassType.shared.typeVarScopeId]);
}

if (isFunction(baseType) || isOverloaded(baseType)) {
Expand Down
10 changes: 10 additions & 0 deletions packages/pyright-internal/src/tests/samples/methodOverride1.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,20 @@ def method1(self, x: T) -> T:


class Derived7_1(Base7[T]):
# This should generate an error.
def method1(self, x: S) -> S:
return x


class Derived7_2(Base7[int]):
def method1(self, x: U) -> U:
return x


class Base8[T]:
def method1(self, x: T) -> T: ...


class Derived8[T](Base8[T]):
# This should generate an error.
def method1[U: str](self, x: U) -> U: ...
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 @@ -946,7 +946,7 @@ test('MethodOverride1', () => {

configOptions.diagnosticRuleSet.reportIncompatibleMethodOverride = 'error';
analysisResults = TestUtils.typeAnalyzeSampleFiles(['methodOverride1.py'], configOptions);
TestUtils.validateResults(analysisResults, 40);
TestUtils.validateResults(analysisResults, 42);
});

test('MethodOverride2', () => {
Expand Down

0 comments on commit c7b2fb9

Please sign in to comment.