Skip to content

Check override's self or cls against base method's declared type - #12015

Open
crypt0lith wants to merge 2 commits into
microsoft:mainfrom
crypt0lith:fix/override-annotated-self
Open

crypt0lith wants to merge 2 commits into
microsoft:mainfrom
crypt0lith:fix/override-annotated-self

Conversation

@crypt0lith

@crypt0lith crypt0lith commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #12012

An override isn't reported as incompatible when the base method declares a self or cls type that neither the child class nor the override's declared type is assignable to.

Root cause

validateOverrideMethodInternal skips the first parameter of instance, class and constructor methods, because an override's self or cls is the child class rather than the base class. That skip also covers a base method whose self or cls is declared, so the override's type is never checked against it.

Fix

When the base method declares self or cls, require the child class or the override's declared type to be assignable to it, and report a mismatch as a parameter 1 type mismatch, like other parameters. Still skip a base self or cls that isn't declared, since the override's is always assignable to it. When two base classes define the same method, check the child class this way too, and skip base overloads that don't apply to it, as the check against a single base class already does.

Test

Cases added to methodOverride2.py
Case Expected Description
Derived4_1.method1 base self declared as a protocol the child class satisfies
Derived4_2.method1 override declares the same self type as the base
Derived4_3.method1 error base self declared as a protocol the child class doesn't satisfy
Derived5_1.method1, Derived5_1.method2 base self and cls declared as the specialized base class the child derives from
Derived5_2.method1, Derived5_2.method2 error base self and cls declared with a specialization the child class isn't assignable to
Derived5_3.method1 error override widens self to object, but the child class isn't assignable to the base's self type
Derived6.method1, Derived6.method2 override widens self and cls to object; the child class is assignable to the base's types
Derived7_1 inherited methods: one widens self to object, the other's self type is satisfied by the child class
Derived7_2 error inherited methods: one widens self to object, the other's self type isn't satisfied by the child class

Method override validation skips the first parameter of instance,
class and constructor methods, so an override's self or cls is never
compared against a type the base method declares for it.

Fixes microsoft#12012
@bschnurr

Copy link
Copy Markdown
Member

The restricted-receiver cases in the new test work, but the check also rejects an override that accepts a wider receiver:

from typing import override

class Base:
    def f(self: "Base") -> int:
        return 1

class Derived(Base):
    @override
    def f(self: object) -> int:
        return 2

main (ff00b031) accepts this. With 23dfdcb9 integrated, Pyright reports reportIncompatibleMethodOverride on Derived.f because object is not assignable to Base. A receiver annotated object accepts every receiver the base method accepts. Could the receiver check preserve wider annotations while retaining the intended restricted-receiver diagnostics? Please add this instance-method case as a regression test.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Override not checked against base method's annotated self

2 participants