Skip to content

Instantly share code, notes, and snippets.

@wiml
Created April 2, 2021 18:34
Show Gist options
  • Save wiml/5434a9e49699d5238034202cb123184d to your computer and use it in GitHub Desktop.
Save wiml/5434a9e49699d5238034202cb123184d to your computer and use it in GitHub Desktop.
mypy-zope overload example
from typing import Optional, Union, List, overload
from zope.interface import Interface, implementer
class ISomething(Interface):
@overload
def getStuff(index: int) -> int:
...
@overload
def getStuff(index: None = None) -> List[int]:
...
def getStuff(index: Optional[int] = None) -> Union[int, List[int]]:
"""
A method with an awkwardly typed signature: if called with an
argument, it returns a result; if called without an argument,
it returns a list of results.
"""
...
@implementer(ISomething)
class MySomething:
@overload
def getStuff(self, index: int) -> int:
...
@overload
def getStuff(self, index: None = None) -> List[int]:
...
def getStuff(self, index: Optional[int] = None) -> Union[int, List[int]]:
if index is None:
return [1, 2, 3, 4]
else:
return 42
z = MySomething()
i: int = z.getStuff(1)
j: List[int] = z.getStuff()
z2: ISomething = ISomething(z)
i = z.getStuff(1)
j = z.getStuff()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment