You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
foo.py:12: error: Argument 1 to "func" has incompatible type "Callable[[], None]"; expected "Caller"
I believe that mypy should actually allow this, because call is actually an object that fulfills the protocol. This would also open up the opportunity to create callback signatures of any complexity (for example, default and keyword-only arguments) without the need for additional features for Callable.
The text was updated successfully, but these errors were encountered:
This is a great idea! And it is easy to implement. TBH I never liked NamedArg, KwArg, etc. This will look much cleaner and flexible (because we can even express overloads as callbacks).
Fixes#5453
The issue proposed an interesting idea. Allow callables as subtypes of protocols with `__call__`. IMO this is not just reasonable and type safe, but is also more clear that the extended callable syntax in `mypy_extensions`. For example:
```python
class Combiner(Protocol):
def __call__(self, *vals: bytes, maxlen: Optional[int] = None) -> List[bytes]: ...
def batch_proc(data: Iterable[bytes], cb_results: Combiner) -> bytes:
...
```
The callback protocols:
* Have cleaner familiar syntax in contrast to `Callable[[VarArg(bytes), DefaultNamedArg(Optional[int], 'maxlen')], List[bytes]]` (compare to above)
* Allow to be more explicit/flexible about binding of type variables in generic callbacks (see tests for examples)
* Support overloaded callbacks (this is simply impossible with the current extended callable syntax)
* Are easy to implement
If this will get some traction, I would actually propose to deprecate extended callable syntax in favor of callback protocols.
This came up in python/typeshed#2379. Consider the following code:
Currently, mypy 0.620 rejects this:
I believe that mypy should actually allow this, because
call
is actually an object that fulfills the protocol. This would also open up the opportunity to create callback signatures of any complexity (for example, default and keyword-only arguments) without the need for additional features forCallable
.The text was updated successfully, but these errors were encountered: