mypy-zope icon indicating copy to clipboard operation
mypy-zope copied to clipboard

some way to define self types

Open graingert opened this issue 4 years ago • 2 comments

eg I might want to define

class ICopyable(zope.interface.Interface):
    def copy(self: T) -> T:
        pass

but I get "Interface methods should not have 'self' argument"

graingert avatar Apr 01 '21 22:04 graingert

I can use this, but then I lose the type of the function in @implements

class ICopyable(zope.interface.Interface):
    @zope.interface.interface.fromMethod
    def copy(self: T) -> T:
        pass

graingert avatar Apr 01 '21 22:04 graingert

Right, interface method cannot have self argument - it won't work at runtime (my guess). However, we might make @zope.interface.interface.fromMethod trick work, if that's acceptable in your case. It feels a bit like a hack though. It isn't anywhere in the official docs.

The trick here is to make it work both at runtime and during static checking.

kedder avatar Apr 02 '21 19:04 kedder