Skip to content

Order dependent override error when subclassing with multiple typevars with defaults #19362

Open
@randolf-scholz

Description

@randolf-scholz

Bug Report

This false positive override error seems to happen when multiple typevars with defaults are present, and we are using them in a different order than the original one.

To Reproduce

Code sample in pyright playground

class Transform[X = object, Y = object]:
    def copy(self) -> "Transform[X, Y]": ...
    def invert(self) -> "Transform[Y, X]": ...


class Foo[X = object, Y = object](Transform[X, Y]):
    def copy(self) -> "Foo[X, Y]": ...  # mypy: ✅ pyright: ✅
    def invert(self) -> "Foo[Y, X]": ...  # mypy: ❌ pyright: ✅
Return type "Foo[Y, X]" of "invert" incompatible with return type "Transform[Y, X]" in supertype "Transform"  [override]

with legacy syntax: https://mypy-play.net/?mypy=latest&python=3.12&gist=df2aabb39005153868c4bcb7d880e921, Code sample in pyright playground

from typing import TypeVar, Generic

X = TypeVar("X", default=object)
Y = TypeVar("Y", default=object)
U = TypeVar("U", default=object)
V = TypeVar("V", default=object)

class Transform(Generic[X, Y]):
    def copy(self) -> "Transform[X, Y]": ...
    def invert(self) -> "Transform[Y, X]": ...

class Foo(Transform[U, V], Generic[U, V]):
    def copy(self) -> "Foo[U, V]": ...  # mypy: ✅ pyright: ✅
    def invert(self) -> "Foo[V, U]": ...  # mypy: ❌ pyright: ✅

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions