ramonhagenaars / nptyping

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

typing.Literal in shape of NDArray #56

Open DanilZittser opened 2 years ago

DanilZittser commented 2 years ago

Hi, to all!

I am very grateful for your library, it made the code much clearer.

This code raise TypeError:

from nptyping import NDArray, Float32
from typing import Literal

Encoding = NDArray[(Any, Literal[64, 128, 256, 512]), Float32]

TypeError: Invalid parameter for NDArray: "((1, typing.Literal[64, 128]), Float[32])"

How to make NDArray alias with Literal type?

ramonhagenaars commented 2 years ago

Hi @DanilZittser !

What were you trying to express with the Literal? A 2d array with the second dimension of size 64, 128, 256 or 512?

Would this maybe cover your case?

Encoding64 = NDArray[(Any, 64), Float32]
Encoding128 = NDArray[(Any, 128), Float32]
Encoding256 = NDArray[(Any, 256), Float32]
Encoding512 = NDArray[(Any, 512), Float32]
Encoding = Union[Encoding64, Encoding128, Encoding256, Encoding512]