[ty] Infer type of self for decorated methods and properties (#21123)

## Summary

Infer a type of unannotated `self` parameters in decorated methods /
properties.

closes https://github.com/astral-sh/ty/issues/1448

## Test Plan

Existing tests, some new tests.
This commit is contained in:
David Peter 2025-10-29 22:22:38 +01:00 committed by GitHub
parent aca8ba76a4
commit 5139f76d1f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 97 additions and 33 deletions

View file

@ -116,7 +116,7 @@ A.implicit_self(1)
Passing `self` implicitly also verifies the type:
```py
from typing import Never
from typing import Never, Callable
class Strange:
def can_not_be_called(self: Never) -> None: ...
@ -139,6 +139,9 @@ The first parameter of instance methods always has type `Self`, if it is not exp
The name `self` is not special in any way.
```py
def some_decorator(f: Callable) -> Callable:
return f
class B:
def name_does_not_matter(this) -> Self:
reveal_type(this) # revealed: Self@name_does_not_matter
@ -153,18 +156,45 @@ class B:
reveal_type(self) # revealed: Self@keyword_only
return self
@some_decorator
def decorated_method(self) -> Self:
reveal_type(self) # revealed: Self@decorated_method
return self
@property
def a_property(self) -> Self:
# TODO: Should reveal Self@a_property
reveal_type(self) # revealed: Unknown
reveal_type(self) # revealed: Self@a_property
return self
async def async_method(self) -> Self:
reveal_type(self) # revealed: Self@async_method
return self
@staticmethod
def static_method(self):
# The parameter can be called `self`, but it is not treated as `Self`
reveal_type(self) # revealed: Unknown
@staticmethod
@some_decorator
def decorated_static_method(self):
reveal_type(self) # revealed: Unknown
# TODO: On Python <3.10, this should ideally be rejected, because `staticmethod` objects were not callable.
@some_decorator
@staticmethod
def decorated_static_method_2(self):
reveal_type(self) # revealed: Unknown
reveal_type(B().name_does_not_matter()) # revealed: B
reveal_type(B().positional_only(1)) # revealed: B
reveal_type(B().keyword_only(x=1)) # revealed: B
reveal_type(B().decorated_method()) # revealed: Unknown
# TODO: this should be B
reveal_type(B().a_property) # revealed: Unknown
async def _():
reveal_type(await B().async_method()) # revealed: B
```
This also works for generic classes:

View file

@ -598,6 +598,7 @@ class CheckClassMethod:
# error: [invalid-overload]
def try_from3(cls, x: int | str) -> CheckClassMethod | None:
if isinstance(x, int):
# error: [call-non-callable]
return cls(x)
return None

View file

@ -53,20 +53,21 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/overloads.md
39 | # error: [invalid-overload]
40 | def try_from3(cls, x: int | str) -> CheckClassMethod | None:
41 | if isinstance(x, int):
42 | return cls(x)
43 | return None
44 |
45 | @overload
46 | @classmethod
47 | def try_from4(cls, x: int) -> CheckClassMethod: ...
48 | @overload
49 | @classmethod
50 | def try_from4(cls, x: str) -> None: ...
51 | @classmethod
52 | def try_from4(cls, x: int | str) -> CheckClassMethod | None:
53 | if isinstance(x, int):
54 | return cls(x)
55 | return None
42 | # error: [call-non-callable]
43 | return cls(x)
44 | return None
45 |
46 | @overload
47 | @classmethod
48 | def try_from4(cls, x: int) -> CheckClassMethod: ...
49 | @overload
50 | @classmethod
51 | def try_from4(cls, x: str) -> None: ...
52 | @classmethod
53 | def try_from4(cls, x: int | str) -> CheckClassMethod | None:
54 | if isinstance(x, int):
55 | return cls(x)
56 | return None
```
# Diagnostics
@ -124,8 +125,22 @@ error[invalid-overload]: Overloaded function `try_from3` does not use the `@clas
| |
| Missing here
41 | if isinstance(x, int):
42 | return cls(x)
42 | # error: [call-non-callable]
|
info: rule `invalid-overload` is enabled by default
```
```
error[call-non-callable]: Object of type `CheckClassMethod` is not callable
--> src/mdtest_snippet.py:43:20
|
41 | if isinstance(x, int):
42 | # error: [call-non-callable]
43 | return cls(x)
| ^^^^^^
44 | return None
|
info: rule `call-non-callable` is enabled by default
```