[ty] Infer type for implicit self parameters in method bodies (#20922)
## Summary Infer a type of `Self` for unannotated `self` parameters in methods of classes. part of https://github.com/astral-sh/ty/issues/159 closes https://github.com/astral-sh/ty/issues/1081 ## Conformance tests changes ```diff +enums_member_values.py:85:9: error[invalid-assignment] Object of type `int` is not assignable to attribute `_value_` of type `str` ``` A true positive ✔️ ```diff -generics_self_advanced.py:35:9: error[type-assertion-failure] Argument does not have asserted type `Self@method2` -generics_self_basic.py:14:9: error[type-assertion-failure] Argument does not have asserted type `Self@set_scale ``` Two false positives going away ✔️ ```diff +generics_syntax_infer_variance.py:82:9: error[invalid-assignment] Cannot assign to final attribute `x` on type `Self@__init__` ``` This looks like a true positive to me, even if it's not marked with `# E` ✔️ ```diff +protocols_explicit.py:56:9: error[invalid-assignment] Object of type `tuple[int, int, str]` is not assignable to attribute `rgb` of type `tuple[int, int, int]` ``` True positive ✔️ ``` +protocols_explicit.py:85:9: error[invalid-attribute-access] Cannot assign to ClassVar `cm1` from an instance of type `Self@__init__` ``` This looks like a true positive to me, even if it's not marked with `# E`. But this is consistent with our understanding of `ClassVar`, I think. ✔️ ```py +qualifiers_final_annotation.py:52:9: error[invalid-assignment] Cannot assign to final attribute `ID4` on type `Self@__init__` +qualifiers_final_annotation.py:65:9: error[invalid-assignment] Cannot assign to final attribute `ID7` on type `Self@method1` ``` New true positives ✔️ ```py +qualifiers_final_annotation.py:52:9: error[invalid-assignment] Cannot assign to final attribute `ID4` on type `Self@__init__` +qualifiers_final_annotation.py:57:13: error[invalid-assignment] Cannot assign to final attribute `ID6` on type `Self@__init__` +qualifiers_final_annotation.py:59:13: error[invalid-assignment] Cannot assign to final attribute `ID6` on type `Self@__init__` ``` This is a new false positive, but that's a pre-existing issue on main (if you annotate with `Self`): https://play.ty.dev/3ee1c56d-7e13-43bb-811a-7a81e236e6ab ❌ => reported as https://github.com/astral-sh/ty/issues/1409 ## Ecosystem * There are 5931 new `unresolved-attribute` and 3292 new `possibly-missing-attribute` attribute errors, way too many to look at all of them. I randomly sampled 15 of these errors and found: * 13 instances where there was simply no such attribute that we could plausibly see. Sometimes [I didn't find it anywhere](8644d886c6/openlibrary/plugins/openlibrary/tests/test_listapi.py (L33)). Sometimes it was set externally on the object. Sometimes there was some [`setattr` dynamicness going on](a49f6b927d/setuptools/wheel.py (L88-L94)). I would consider all of them to be true positives. * 1 instance where [attribute was set on `obj` in `__new__`](9e87b44fd4/sympy/tensor/array/array_comprehension.py (L45C1-L45C36)), which we don't support yet * 1 instance [where the attribute was defined via `__slots__` ](e250ec0fc8/lib/spack/spack/vendor/pyrsistent/_pdeque.py (L48C5-L48C14)) * I see 44 instances [of the false positive above](https://github.com/astral-sh/ty/issues/1409) with `Final` instance attributes being set in `__init__`. I don't think this should block this PR. ## Test Plan New Markdown tests. --------- Co-authored-by: Shaygan Hooshyari <sh.hooshyari@gmail.com>
This commit is contained in:
@@ -56,7 +56,7 @@ In instance methods, the first parameter (regardless of its name) is assumed to
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python-version = "3.11"
|
||||
python-version = "3.12"
|
||||
```
|
||||
|
||||
```py
|
||||
@@ -64,16 +64,30 @@ from typing import Self
|
||||
|
||||
class A:
|
||||
def implicit_self(self) -> Self:
|
||||
# TODO: This should be Self@implicit_self
|
||||
reveal_type(self) # revealed: Unknown
|
||||
reveal_type(self) # revealed: Self@implicit_self
|
||||
|
||||
return self
|
||||
|
||||
def a_method(self) -> int:
|
||||
def first_arg_is_not_self(a: int) -> int:
|
||||
def implicit_self_generic[T](self, x: T) -> T:
|
||||
reveal_type(self) # revealed: Self@implicit_self_generic
|
||||
|
||||
return x
|
||||
|
||||
def method_a(self) -> None:
|
||||
def first_param_is_not_self(a: int):
|
||||
reveal_type(a) # revealed: int
|
||||
return a
|
||||
return first_arg_is_not_self(1)
|
||||
reveal_type(self) # revealed: Self@method_a
|
||||
|
||||
def first_param_is_not_self_unannotated(a):
|
||||
reveal_type(a) # revealed: Unknown
|
||||
reveal_type(self) # revealed: Self@method_a
|
||||
|
||||
def first_param_is_also_not_self(self) -> None:
|
||||
reveal_type(self) # revealed: Unknown
|
||||
|
||||
def first_param_is_explicit_self(this: Self) -> None:
|
||||
reveal_type(this) # revealed: Self@method_a
|
||||
reveal_type(self) # revealed: Self@method_a
|
||||
|
||||
@classmethod
|
||||
def a_classmethod(cls) -> Self:
|
||||
@@ -127,19 +141,16 @@ The name `self` is not special in any way.
|
||||
```py
|
||||
class B:
|
||||
def name_does_not_matter(this) -> Self:
|
||||
# TODO: Should reveal Self@name_does_not_matter
|
||||
reveal_type(this) # revealed: Unknown
|
||||
reveal_type(this) # revealed: Self@name_does_not_matter
|
||||
|
||||
return this
|
||||
|
||||
def positional_only(self, /, x: int) -> Self:
|
||||
# TODO: Should reveal Self@positional_only
|
||||
reveal_type(self) # revealed: Unknown
|
||||
reveal_type(self) # revealed: Self@positional_only
|
||||
return self
|
||||
|
||||
def keyword_only(self, *, x: int) -> Self:
|
||||
# TODO: Should reveal Self@keyword_only
|
||||
reveal_type(self) # revealed: Unknown
|
||||
reveal_type(self) # revealed: Self@keyword_only
|
||||
return self
|
||||
|
||||
@property
|
||||
@@ -165,8 +176,7 @@ T = TypeVar("T")
|
||||
|
||||
class G(Generic[T]):
|
||||
def id(self) -> Self:
|
||||
# TODO: Should reveal Self@id
|
||||
reveal_type(self) # revealed: Unknown
|
||||
reveal_type(self) # revealed: Self@id
|
||||
|
||||
return self
|
||||
|
||||
@@ -252,6 +262,20 @@ class LinkedList:
|
||||
reveal_type(LinkedList().next()) # revealed: LinkedList
|
||||
```
|
||||
|
||||
Attributes can also refer to a generic parameter:
|
||||
|
||||
```py
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
class C(Generic[T]):
|
||||
foo: T
|
||||
def method(self) -> None:
|
||||
reveal_type(self) # revealed: Self@method
|
||||
reveal_type(self.foo) # revealed: T@C
|
||||
```
|
||||
|
||||
## Generic Classes
|
||||
|
||||
```py
|
||||
@@ -342,31 +366,28 @@ b: Self
|
||||
|
||||
# TODO: "Self" cannot be used in a function with a `self` or `cls` parameter that has a type annotation other than "Self"
|
||||
class Foo:
|
||||
# TODO: rejected Self because self has a different type
|
||||
# TODO: This `self: T` annotation should be rejected because `T` is not `Self`
|
||||
def has_existing_self_annotation(self: T) -> Self:
|
||||
return self # error: [invalid-return-type]
|
||||
|
||||
def return_concrete_type(self) -> Self:
|
||||
# TODO: tell user to use "Foo" instead of "Self"
|
||||
# TODO: We could emit a hint that suggests annotating with `Foo` instead of `Self`
|
||||
# error: [invalid-return-type]
|
||||
return Foo()
|
||||
|
||||
@staticmethod
|
||||
# TODO: reject because of staticmethod
|
||||
# TODO: The usage of `Self` here should be rejected because this is a static method
|
||||
def make() -> Self:
|
||||
# error: [invalid-return-type]
|
||||
return Foo()
|
||||
|
||||
class Bar(Generic[T]):
|
||||
foo: T
|
||||
def bar(self) -> T:
|
||||
return self.foo
|
||||
class Bar(Generic[T]): ...
|
||||
|
||||
# error: [invalid-type-form]
|
||||
class Baz(Bar[Self]): ...
|
||||
|
||||
class MyMetaclass(type):
|
||||
# TODO: rejected
|
||||
# TODO: reject the Self usage. because self cannot be used within a metaclass.
|
||||
def __new__(cls) -> Self:
|
||||
return super().__new__(cls)
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user