The mypy docs read
Callback protocols and :py:data:
~typing.Callable
types can be used interchangeably. Keyword argument names in :py:meth:__call__ <object.__call__>
methods must be identical, unless a double underscore prefix is used. For example:typing_extensions import Protocol T = TypeVar('T') class Copy(Protocol): def __call__(self, __origin: T) -> T: ... copy_a: Callable[[T], T] copy_b: Copy copy_a = copy_b # OK copy_b = copy_a # Also OK ```
However, this example also works if we remove the double underscore prefix from before __origin
. E.g.
$ cat t.py
from typing import Callable, TypeVar
from typing_extensions import Protocol
T = TypeVar('T')
class Copy(Protocol):
def __call__(self, origin: T) -> T: ...
copy_a: Callable[[T], T]
copy_b: Copy
copy_a = copy_b # OK
copy_b = copy_a # Also OK
$ mypy t.py
Success: no issues found in 1 source file
So, this example isn't clear to me. When do we need the double underscore prefix?