nptyping icon indicating copy to clipboard operation
nptyping copied to clipboard

typing.Literal in shape of NDArray

Open DanilZittser opened this issue 3 years ago • 1 comments

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?

DanilZittser avatar Jan 20 '22 04:01 DanilZittser

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]

ramonhagenaars avatar Mar 27 '22 18:03 ramonhagenaars