I'm currently plugging an Adapter into a port in a handler which can help you get an instance of that port. Here is a simple setup to reproduce:
from typing import Protocol
class SomePort(Protocol):
def caller(self):
raise NotImplementedError
class SomeAdapter:
def caller(self):
print("Called")
class SomeController:
@staticmethod
def get_instance(port: SomePort) -> SomePort:
return port()
instance = SomeController.get_instance(port=SomeAdapter)
instance.caller()
This is working just fine in code but in mypy I'm getting the following issues:
$ mypy --version
mypy 0.931
$ mypy test/test.py
test/test.py:17: error: "SomePort" not callable
test/test.py:20: error: Argument "port" to "get_instance" of "SomeController" has incompatible type "Type[SomeAdapter]"; expected "SomePort"
anything I've misunderstood here? Running the script prints Called just fine, but mypy is unhappy about the way I've set this up. Any help is appreciated. :)
If you want the
portparameter inSomeController.get_instanceto accept a class that is a subtype ofSomePort, you need to annotate it withtype[SomePort]instead ofSomePort.The latter (as you have it in your example) would express that
portshould be an instance of the typeSomePort. SinceSomePortis not callable (no__call__method defined), you get that first Mypy error. And becauseSomeAdapteris a class that follows theSomePortprotocol rather than an instance of such a class, you get that second error.If you are on Python
<3.9usetyping.Typeinstead of the built-intype, i.e.typing.Type[SomePort].