ramonhagenaars / nptyping

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

Should rescursive nature of `SubscriptableTypes` allow `NDArray[float][int][bool]`? #31

Closed EricCousineau-TRI closed 2 years ago

EricCousineau-TRI commented 4 years ago

When looking through the typing setup, it seems like this is possible (on c813f6d):

>>> from typing import NDArray
>>> NDArray[2][int]
NDArray[(2,), Int[64]]  # Cool!
>>> NDArray[float][int][bool]
NDArray[(typing.Any, ...), Bool]  # Er...

Dunno if that's a bug or feature?

EricCousineau-TRI commented 4 years ago

I've tried to iron out a contract here: https://github.com/EricCousineau-TRI/repro/blob/4a5c71162bf2e3c0afc4d08c5d757aa0841000c4/python/nptyping_ish/test/typing_test.py

Dunno if it's useful here.

ramonhagenaars commented 4 years ago

Interesting, you found a hidden 'feature' that I wasn't aware of.

>>> from typing import NDArray

>>> arr_generic = NDArray
>>> arr_generic
NDArray[(typing.Any, ...), typing.Any]

>>> arr_with_dims = arr_generic[(2, 2)]
>>> arr_with_dims
NDArray[(2, 2), typing.Any]

>>> arr_with_dims_and_type = arr_with_dims[int]
>>> arr_with_dims_and_type
NDArray[(2, 2), Int[32]]

And it seems you can replace the dimension as well as the type:

>>> arr_with_dims_and_type[float]
NDArray[(2, 2), Float[64]]

>>> arr_with_dims_and_type[3]
NDArray[(3,), Int[32]]

I'm not yet sure if it is useful and if it should be officially supported or removed.

EricCousineau-TRI commented 4 years ago

Yeah... In my example, I restricted it, but I still provide access to the .unbound() version on the instantiation, which you can then use to get back to whatever you want.

I haven't encountered any friction with it (using it for some basic behavior cloning stuff with perception, control in robotics - OpenAI Gym-ish), although I haven't been doing anything too intense with typing annotations, other than as basic metadata for dataclass.