python-interface icon indicating copy to clipboard operation
python-interface copied to clipboard

mypy stubs

Open hasii2011 opened this issue 2 years ago • 1 comments

Do you have the above somewhere?

hasii2011 avatar Mar 28 '22 14:03 hasii2011

I'm also looking for stubs to this lib, but for pyright (which is used inside pylance).

I'm not very familiar with stubs rules, but I wrote a little __init__.pyi to solve my problem for now, shown below.

But it does not cover the case of multiple interfaces implementation.

For this particular case, I have to manually create the output of implements and then create the implementation by multiple inheritance:

class I1(Interface):
    def method1(self, x):
        pass

class I2(Interface):
    def method2(self, y):
        pass

iI1, iI2 = implements(I1), implements(I2)

class ImplBoth(iI1,iI2):
    ...

For the other cases, this stub is working, so far.

__init__.pyi

from .default import default as default
from .interface import (implements as implements,
                        Interface as Interface,
                        InvalidImplementation as InvalidImplementation)

__all__ = [
    "default",
    "InvalidImplementation",
    "Interface",
    "implements",
]

from typing import TypeVar

InterfaceType = TypeVar("InterfaceType")
DefaultMethod = TypeVar("DefaultMethod")

class Interface: ...
    
def implements(interface: InterfaceType) -> InterfaceType: ...

def default(method: DefaultMethod) -> DefaultMethod:
    """Default implementation of a function in terms of interface methods."""
    ...

Diogo-Rossi avatar Apr 04 '22 01:04 Diogo-Rossi