-
-
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
Callback protocol with other attributes #10976
Comments
I'm not sure "callback protocol" should be defined as a concept by itself: it's just an emergent property of the type system that a Protocol with a For the bug report you link to, I feel the issue is that the type checker doesn't understand that a function is a |
It sounds like there's no definitive answer. After much discussion, we've decided to interpret "callback protocol" in a more lax manner, consistent with what @JelleZijlstra said above. We've made this change in pyright, which now deviates from mypy in the following sample. If you agree with our conclusion, please consider this a request for a change in mypy. from typing import Protocol
class SomeFunc(Protocol):
__name__: str
other_attribute: int
def __call__(self) -> str:
...
def other_func(f: SomeFunc):
print(f.__name__)
f.other_attribute = 1
f.other_attribute = "str" # pyright and mypy emit an error
f.missing_attribute = 3 # pyright and mypy emit an error
@other_func # pyright does not emit an error, but mypy does
def some_func() -> str:
... |
I'm not sure if this is a mypy bug or intended behavior. I'm looking for advice.
PEP 544 introduced the concept of a callback protocol. In particular, it says:
It is not clear to me whether a protocol that defines members other than
__call__
is considered a callback protocol. PEP 544 says:This could imply that a protocol class that includes attributes other than
__call__
is not a callback protocol, but maybe I'm reading too much into it. I thought I remembered reading somewhere (perhaps on a typing-sig thread?) that if a protocol class included attributes other than__call__
, it would not be considered a callback protocol, but I can't seem to find that thread.A pyright user (@kennipj) recently filed this bug report. Currently, the behavior of pyright matches mypy in this case. I'm wondering if mypy's behavior is intended or whether it's a bug.
Here's another mypy bug that is related to this issue: #10403.
The text was updated successfully, but these errors were encountered: