related-sciences / gwas-analysis

GWAS data analysis experiments
Apache License 2.0
24 stars 6 forks source link

How can you statically type a duck array? #17

Closed eric-czech closed 4 years ago

eric-czech commented 4 years ago

This was a to-do I had in my xarray/scikit-allele prototype that I'll record the answer to here for posterity.

The question is what do signatures look like for functions operating on any array type implementing the __array_function__ protocol? E.g.:

def mean(arr: ?) -> number:
    return arr.sum()

The answer is structural subtyping via PEP-0544 (Protocols). This is out now at least in Python 3.8.2: https://docs.python.org/3/library/typing.html#nominal-vs-structural-subtyping