Skip to content

Instantly share code, notes, and snippets.

@enomado
Last active March 12, 2019 16:10
Show Gist options
  • Save enomado/5e07f194d1b1ab43dfb55b0f8944664f to your computer and use it in GitHub Desktop.
Save enomado/5e07f194d1b1ab43dfb55b0f8944664f to your computer and use it in GitHub Desktop.
mypy generic with extends Interface
# generic with extends Interface
class PointableProtocol(Protocol):
x: Any
y: Any
def __init__(self, x: Any, y: Any) -> None:
pass
T = TypeVar("T", bound=PointableProtocol)
def normalize_point_order(p1: T, p2: T) -> Tuple[T, T]:
runtime_type = p1.__class__
res1 = runtime_type(int(min(p1.x, p2.x)), int(min(p1.y, p2.y)))
res2 = runtime_type(
int(max(p1.x, p2.x)), int(max(p1.y, p2.y))
)
return res1, res2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment