pyright icon indicating copy to clipboard operation
pyright copied to clipboard

A regression allows to violate the Liskov substitution principle for arguments when overriding methods

Open LeeeeT opened this issue 6 months ago • 0 comments

For the following code, pyright 1.1.373 correctly reports an incompatibility with the base class A when overriding the method f in the child class B:

class A:
    def f(self, v: int): ...


class B[T](A):
    def f(self, v: T): ...  # 1.1.373: Type "int" is incompatible with type "T@B"

On version 1.1.374, the above code type checks without any errors. Here's a clear example on how this false negative can hide a serious bug:

class A:
    def f(self, v: int): ...


class B[T](A):
    def f(self, v: T): ...  # 1.1.374: no errors


class C(B[str]):
    def f(self, v: str):
        v.lower()


def test(a: A):
    a.f(1)


test(C())

The type of the base class A is completely neglected in the child class C resulting in a runtime error.

LeeeeT avatar Jul 31 '24 16:07 LeeeeT