ramonhagenaars / nptyping

💡 Type hints for Numpy and Pandas
MIT License
576 stars 29 forks source link

Allow PEP 646 variadic generics syntax? #103

Open burnpanck opened 1 year ago

burnpanck commented 1 year ago

PEP 646 Variadic Generics have landed in python 3.11, which allow generics to be written that accept a variable number of arguments, and the prime use-case is for numpy-like arrays. As far as I can tell however, neither numpy itself, nor the python standard library ship actual typings making use of that. Given that nptyping already supports that use-case just through the use of strings instead of variadic generics, it may be reasonable to extend to the new syntax. Is that something which aligns with the library's vision?

ramonhagenaars commented 1 year ago

Hi Burnpanck,

Variadic generics are indeed on this library's radar. I would use it to support more generic types, for example indices in DataFrames. This is how it could look like:

from nptyping import Structure as S, Index  # note: Index does not exist yet

DataFrame[S["a: Int, b: Float"]]  # hint a structured DataFrame
DataFrame[Index["a"]]  # only hint an index
DataFrame[S["a: Int, b: Float"], Index["a"]]  # hint a structure and an index

However, mypy support is still in progress (see https://github.com/python/mypy/issues/12280) and nptyping wants to stay mypy-compliant. Once mypy has implemented support, nptyping will follow.