Repository navigation
Check override's self or cls against base method's declared type - #12015
Open
crypt0lith wants to merge 2 commits into
Open
crypt0lith wants to merge 2 commits into
crypt0lith wants to merge 2 commits into
Conversation
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
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
|
crypt0lith
commented
Nov 20, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #12012
An override isn't reported as incompatible when the base method declares a
selforclstype that neither the child class nor the override's declared type is assignable to.Root cause
validateOverrideMethodInternalskips the first parameter of instance, class and constructor methods, because an override'sselforclsis the child class rather than the base class. That skip also covers a base method whoseselforclsis declared, so the override's type is never checked against it.Fix
When the base method declares
selforcls, 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 baseselforclsthat 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.pyDerived4_1.method1selfdeclared as a protocol the child class satisfiesDerived4_2.method1selftype as the baseDerived4_3.method1selfdeclared as a protocol the child class doesn't satisfyDerived5_1.method1,Derived5_1.method2selfandclsdeclared as the specialized base class the child derives fromDerived5_2.method1,Derived5_2.method2selfandclsdeclared with a specialization the child class isn't assignable toDerived5_3.method1selftoobject, but the child class isn't assignable to the base'sselftypeDerived6.method1,Derived6.method2selfandclstoobject; the child class is assignable to the base's typesDerived7_1selftoobject, the other'sselftype is satisfied by the child classDerived7_2selftoobject, the other'sselftype isn't satisfied by the child class