-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Callable attribute is considered a method #9489
Comments
Hm, this is unfortunate. I think it's due to mypy not really understanding the difference between class and instance attributes. I expect it'll be hard to fix. In the meantime, here's a weird workaround: if you make the type Optional and check for None before the call, it works: class Example:
a: Optional[Callable[[int], int]]
def f(self) -> int:
assert self.a is not None
return self.a(42) |
I just found another workaround using a Protocol: if t.TYPE_CHECKING:
class AProtocol(t.Protocol):
def __call__(self) -> int:
pass
@dataclass
class Example:
a: 'AProtocol' = field(default=lambda: 42) I feel like there should be a way for mypy to distinguish these cases, unless there are conflicting use cases (which I am not aware of yet). But I haven't taken a look into the related mypy code. Edit Ctrl+Enter too early again 😄 Completed the comment |
I'm hitting the same issue, but with a different error. In case anyone else is affect, here is the MWE: from dataclasses import dataclass
from typing import Callable
@dataclass
class Example:
func: Callable[[str], str]
def the_func(s: str) -> str:
return s
example = Example(the_func)
string = example.func("passthrough")
print(string) This runs without problems and should pass Mypy, but instead produces the following errors:
As in the original issue, one workaround is to declare the field as My understanding is that because |
Fixed by #10548 |
Small example to illustrate the issue:
Curiously, adding
Example
as the first argument to the type ofa
makes the call be recognized OK but the default not.Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: