Skip to content

Semantics of typing.Self not understood in child classes #2122

@bepri

Description

@bepri

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 (c1e6188 2025-12-18)

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingtyping semanticstyping-module features, spec compliance, etc

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions