ty icon indicating copy to clipboard operation
ty copied to clipboard

Semantics of `typing.Self` not understood in child classes

Open bepri opened this issue 4 weeks ago • 0 comments

Summary

When attempting to override a method of a parent class with -> Self in a child class, calls to the parent's method are considered invalid.

from typing_extensions import Self, override


class Parent:
    @classmethod
    def new(cls) -> Self:
        return cls()

class Child(Parent):
    @classmethod
    @override
    def new(cls) -> Self:
        return super().new()

The following code reports:

❯ ty check foo.py
error[invalid-return-type]: Return type does not match returned value
  --> foo.py:12:21
   |
10 |     @classmethod
11 |     @override
12 |     def new(cls) -> Self:
   |                     ---- Expected `Self@new` because of return type
13 |         return super().new()
   |                ^^^^^^^^^^^^^ expected `Self@new`, found `Child`
   |
info: rule `invalid-return-type` is enabled by default

Found 1 diagnostic

Running the same file through pyright and mypy yields no errors.

Version

ty 0.0.4 (c1e6188b1 2025-12-18)

bepri avatar Dec 19 '25 21:12 bepri