[ty] Infer type of implicit cls parameter in method bodies (#21685)

## Summary

Extends https://github.com/astral-sh/ruff/pull/20922 to infer
unannotated `cls` parameters as `type[Self]` in method bodies.

Part of https://github.com/astral-sh/ty/issues/159.
This commit is contained in:
Ibraheem Ahmed
2025-12-10 04:31:28 -05:00
committed by GitHub
parent d2aabeaaa2
commit ff7086d9ad
8 changed files with 167 additions and 154 deletions

View File

@@ -63,6 +63,12 @@ python-version = "3.12"
from typing import Self
class A:
def __init__(self):
reveal_type(self) # revealed: Self@__init__
def __init_subclass__(cls, default_name, **kwargs):
reveal_type(cls) # revealed: type[Self@__init_subclass__]
def implicit_self(self) -> Self:
reveal_type(self) # revealed: Self@implicit_self
@@ -91,8 +97,7 @@ class A:
@classmethod
def a_classmethod(cls) -> Self:
# TODO: This should be type[Self@bar]
reveal_type(cls) # revealed: Unknown
reveal_type(cls) # revealed: type[Self@a_classmethod]
return cls()
@staticmethod