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

Fixed bug that results in an "incompatible method override" false neg… #9707

Merged
merged 1 commit into from
Jan 14, 2025
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
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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please explain why this should generate an error?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider the following:

d = Derived7_1[int]()
d.method1("")

def func(b: Base7[int]):
    b.method1(1)

func(d)

For an instance of Derived7_1[int], the specialized type of the base class method is (int) -> int, and the specialized type of the derived class method is (str) -> str. It's a Liskov violation.

Copy link

@LeeeeT LeeeeT Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Derived7_1.method1 extends Base7.method1 by making it polymorphic. (S@method1) -> S@method1 is assignable to (T) -> T for any concrete T that comes from the generic specialization, isn't it? I don't know what the intended behavior is, but it feels inconsistent with what was implemented in #9417. That's why I decided to point that out.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is different from the case you reported in #9417 because that didn't involve a class-scoped type variable in the base class. In this case, the override is unsound as demonstrated in the sample above.

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
Loading