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
test.py:51: error: Revealed type is 'callable_protocol_generic.CallableHolder[builtins.float*, builtins.int*]'
test.py:54: error: Revealed type is 'builtins.int*'
Looks good. However, I want to use callback protocols because of their more expressive handling of star-args, kwargs, etc.
Here's my attempt at recreating the above using callback protocols:
importtypingInT=typing.TypeVar('InT', contravariant=True) # mypy complained that this needs to be contravariantOutT=typing.TypeVar('OutT', covariant=True) # mypy complained that this needs to be covariantclassCallableProtocol(typing.Protocol[InT, OutT]):
def__call__(self, arg: InT) ->OutT:
passclassCallableHolder(typing.Generic[InT, OutT]):
def__init__(self, fn: CallableProtocol[InT, OutT]):
self.fn=fndefcall(self, arg: InT) ->OutT:
returnself.fn(arg)
defget(f: CallableProtocol[InT, OutT]) ->CallableHolder[InT, OutT]:
returnCallableHolder(f)
defreturns_int(x: float) ->int:
return1holder=get(returns_int)
reveal_type(holder)
result=holder.call(1.0)
reveal_type(result)
mypy produces the following result:
test.py:50: error: Argument 1 to "get" has incompatible type "Callable[[float], int]"; expected "CallableProtocol[<nothing>, <nothing>]"
test.py:50: error: Need type annotation for 'holder'
test.py:51: error: Revealed type is 'Any'
test.py:51: error: Cannot determine type of 'holder'
test.py:53: error: Cannot determine type of 'holder'
test.py:54: error: Revealed type is 'Any'
I expected to be able to produce the same result as with Callable.
I'm using 0.701.
thanks!
The text was updated successfully, but these errors were encountered:
Prepare for a surprise: technically mypy is correct that your code is unsafe! The problem is that argument names are part of the protocol, because one can call it as foo(arg=3). To make a variable positional-only (i.e. anonymous) use __arg in the definition of the protocol (or fancy new PEP 570 if you are on Python 3.8+).
The error message is of course horrible. I have seen many people caught by this (and was caught by it myself). Closing as a duplicate of #4530 (already high priority).
ah, I read about the naming caveat but did not put 2 and 2 together because of the error message. I'll give this a try again tonight. thanks for the quick response.
Hi,
I'm trying to use TypeVars with callable protocols, as you could with
Callable
, but I'm not having any luck.This works using
Callable
:mypy generates the following output:
Looks good. However, I want to use callback protocols because of their more expressive handling of star-args, kwargs, etc.
Here's my attempt at recreating the above using callback protocols:
mypy produces the following result:
I expected to be able to produce the same result as with
Callable
.I'm using 0.701.
thanks!
The text was updated successfully, but these errors were encountered: