-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Callable has no attribute __kwdefaults__ #5958
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
Comments
I think this can be fixed quickly just by a typeshed change, for historical reasons mypy uses |
Sure. Could we just do |
This may work, mypy uses various |
Accessing from typing import Callable
def f(fn: Callable[[], None]) -> None:
fn()
print(fn.__kwdefaults__) # Unsafe
class A:
def __call__(self) -> None: pass
f(A()) If we had intersection types, we could use |
Perhaps more importantly, type objects and C functions don't have The current fallback is already unsafe, but we perhaps shouldn't make it any more unsafe. An alternative approach could be something like this:
|
Everything except Also it is a false positive, the same error appears for clearly valid code: def f() -> None: ...
f.__kwdefaults__ |
This is a fair point. However, the use
My argument is that at the moment there is only a weak case for making things even unsafer, since the enabled use case is probably rare. Some attributes like Also, I'd rather talk about how to fix this in a more principled fashion, even if we won't be doing this in the immediate future. For example, maybe Python function objects should have a different fallback from plain |
Here are some additional related examples that generate false positives at the moment: ord.__text_signature__ # Works at runtime, but mypy complains
class A:
def f(self): pass
A().f.__self__ # Works at runtime, but mypy complains The move to The problem is much wider than the original reported issue, and just fixing the reported issue without considering the wider problem risks moving sideways at best, I think. |
To get the ball rolling, here's a random idea about solving the problem more generally:
This wouldn't directly solve the original issue, but it would have the benefit of more closely modelling what happens at runtime. Another idea would be to use intersection types, but that would be problematic since types like |
Another example of this appeared in python/typing#598 |
Hey, I currently working with decorators and signatures. Each time I assign a signature to __signature__ I need to add a myyp ignore. |
The original example in this issue is no longer reproducible due to changes made in typeshed. |
Here's an example (pycharm is also syntax highlighting it):
Mypy error:
error: "Callable[Any]" has no attribute "__kwdefaults__"
The text was updated successfully, but these errors were encountered: