Skip to content

Commit 1028870

Browse files
committed
[ty] Infer type of implicit self in properties
1 parent 3490611 commit 1028870

File tree

2 files changed

+31
-6
lines changed
  • crates/ty_python_semantic

2 files changed

+31
-6
lines changed

crates/ty_python_semantic/resources/mdtest/annotations/self.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ A.implicit_self(1)
116116
Passing `self` implicitly also verifies the type:
117117

118118
```py
119-
from typing import Never
119+
from typing import Never, Callable
120120

121121
class Strange:
122122
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
139139
The name `self` is not special in any way.
140140

141141
```py
142+
def some_decorator(f: Callable) -> Callable:
143+
return f
144+
142145
class B:
143146
def name_does_not_matter(this) -> Self:
144147
reveal_type(this) # revealed: Self@name_does_not_matter
@@ -153,15 +156,37 @@ class B:
153156
reveal_type(self) # revealed: Self@keyword_only
154157
return self
155158

159+
@some_decorator
160+
def decorated_method(self) -> Self:
161+
reveal_type(self) # revealed: Self@decorated_method
162+
return self
163+
156164
@property
157165
def a_property(self) -> Self:
158-
# TODO: Should reveal Self@a_property
159-
reveal_type(self) # revealed: Unknown
166+
reveal_type(self) # revealed: Self@a_property
160167
return self
161168

169+
@staticmethod
170+
def static_method(self):
171+
# The parameter can be called `self`, but it is not treated as `Self`
172+
reveal_type(self) # revealed: Unknown
173+
174+
@staticmethod
175+
@some_decorator
176+
def decorated_static_method(self):
177+
# TODO: this should be Unknown
178+
reveal_type(self) # revealed: Self@decorated_static_method
179+
180+
@some_decorator
181+
@staticmethod
182+
def decorated_static_method(self):
183+
# TODO: this should be Unknown
184+
reveal_type(self) # revealed: Self@decorated_static_method
185+
162186
reveal_type(B().name_does_not_matter()) # revealed: B
163187
reveal_type(B().positional_only(1)) # revealed: B
164188
reveal_type(B().keyword_only(x=1)) # revealed: B
189+
reveal_type(B().decorated_method()) # revealed: Unknown
165190

166191
# TODO: this should be B
167192
reveal_type(B().a_property) # revealed: Unknown

crates/ty_python_semantic/src/types/infer/builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2583,12 +2583,12 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
25832583
let method = infer_definition_types(db, method_definition)
25842584
.declaration_type(method_definition)
25852585
.inner_type()
2586-
.as_function_literal()?;
2586+
.as_function_literal();
25872587

2588-
if method.is_classmethod(db) {
2588+
if method.is_some_and(|m| m.is_classmethod(db)) {
25892589
// TODO: set the type for `cls` argument
25902590
return None;
2591-
} else if method.is_staticmethod(db) {
2591+
} else if method.is_some_and(|m| m.is_staticmethod(db)) {
25922592
return None;
25932593
}
25942594

0 commit comments

Comments
 (0)