Skip to content

Instantly share code, notes, and snippets.

@DMRobertson
Created October 28, 2021 13:58
Show Gist options
  • Save DMRobertson/fe8d2aeb71c6b8040a1d9b347d545157 to your computer and use it in GitHub Desktop.
Save DMRobertson/fe8d2aeb71c6b8040a1d9b347d545157 to your computer and use it in GitHub Desktop.
Requiring multiple zope Interfaces
#! /usr/bin/env python3
from zope.interface import Interface, implementer
class IA(Interface):
def do_a() -> None:
...
class IB(Interface):
def do_b() -> None:
...
@implementer(IA, IB)
class C:
def do_a(self) -> None:
...
def do_b(self) -> None:
...
@implementer(IA, IB)
class D:
def do_a(self) -> None:
...
def do_b(self) -> None:
...
c = C()
d = D()
def needs_IA(a: IA) -> None:
...
def needs_IB(b: IB) -> None:
...
needs_IA(c) # ok
needs_IB(c) # ok
needs_IA(d) # ok
needs_IB(d) # ok
class IAB(IA, IB):
...
def needs_IA_and_IB(arg: IAB) -> None:
...
needs_IA_and_IB(c) # want this to be checked as ok
needs_IA_and_IB(d) # and this too
needs_IA_and_IB(object()) # want this to be checked as not okay
$ mypy example.py
example.py:59: error: Argument 1 to "needs_IA_and_IB" has incompatible type "C"; expected "IAB" [arg-type]
example.py:60: error: Argument 1 to "needs_IA_and_IB" has incompatible type "D"; expected "IAB" [arg-type]
example.py:61: error: Argument 1 to "needs_IA_and_IB" has incompatible type "object"; expected "IAB" [arg-type]
Found 3 errors in 1 file (checked 1 source file)
$ python --version
Python 3.9.7
$ mypy --version
mypy 0.910
$ pip freeze | grep zope
24:mypy-zope==0.3.2
55:zope.event==4.5.0
56:zope.interface==5.4.0
57:zope.schema==6.1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment