Skip to content

Instantly share code, notes, and snippets.

View ashleney's full-sized avatar
⌨️
professionally adding bloat to your favorite libs

ashlen ashleney

⌨️
professionally adding bloat to your favorite libs
View GitHub Profile
@ashleney
ashleney / add.py
Created June 14, 2021 12:36
Proper annotation of python's add operator
"""An example of how deep python annotations can go.
Here I show a possible way to annotate an add() function to account for as many cases as possible.
We assume that the add() function simply adds the two numbers and does not call internal __add__ or __radd__ methods.
"""
from typing import Protocol, overload
T_contra = TypeVar('T_contra', contravariant=True)
T_co = TypeVar('T_co', covariant=True)