-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fix inference for properties with __call__ #15926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3158,3 +3158,20 @@ class C(A, B): | |
class D(A, B): | ||
def f(self, z: int) -> str: pass # E: Method "f" is not using @override but is overriding a method in class "__main__.A" | ||
[typing fixtures/typing-override.pyi] | ||
|
||
[case testCallableProperty] | ||
from typing import Callable | ||
|
||
class something_callable: | ||
def __call__(self, fn) -> str: ... | ||
|
||
def decorator(fn: Callable[..., int]) -> something_callable: ... | ||
|
||
class A: | ||
@property | ||
@decorator | ||
def f(self) -> int: ... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. People recently complained (don't remember where) that this use case: def make_method() -> SomeCallbackProtocol: ...
class A:
meth = make_method() is not handled the same way as def make_method_callable() -> Callable[[int], int]: ...
class B:
meth = make_method_callable() (because certain special-casing only applies to callable types). I am 95% sure this PR should fix that as well (or maybe with very few modifications), if yes, could you please add such test as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for mentioning this! It's not fixed by this change, but should be doable fix. I will make a follow up PR |
||
|
||
reveal_type(A.f) # N: Revealed type is "__main__.something_callable" | ||
reveal_type(A().f) # N: Revealed type is "builtins.str" | ||
[builtins fixtures/property.pyi] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to avoid extra nesting level by computing something before the first
if
? If yes, I would prefer that. This function is already hard to read.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!