'How to "type annotate" a function that generate classes, so that pylance recognizes the classes members?

I'm trying to use the library python-interface to make more robust interfaces in python.

(I know the usual method is to use the abc module, but this library has some advantages)

Context:

In short, the library is based on a class Interface and a function implements

from interface import implements, Interface

class MyInterface(Interface):

    def method1(self, x):
        pass

    def method2(self, x, y):
        pass


class MyClass(implements(MyInterface)):

    def method1(self, x):
        return x * 2

    def method2(self, x, y):
        return x + y

It also supports extending an interface (via sub-classing).

And the function implements also supports implementing multiple interfaces -> My question is related to this situation.

Question:

I want to type annotate the function implements so that the derived implementation could access the inherited methods with pylance inside Visual Studio Code.

The following annotation does that for one interface.

from typing import TypeVar

InterfaceType = TypeVar("InterfaceType")
    
def implements(interface: InterfaceType) -> InterfaceType: ...

How can I do the annotation so the function implements support implementing multiple interfaces and pylance access the inherited methods?

Edit:

One possible solution is to call the function multiple times, instead of entering multiple arguments to one single call

class First(Interface):
    def met1(self, x):
        pass

class Second(Interface):
    def met2(self, x, y):
        pass

class Implentation(implements(First), implements(Second)): # <- here
    def met1(self, x):
        ...
    def met2(self, x, y):
        ...

But that is too "verbose". How could be the type annotation to use the default proposal (single call)?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source