Open giokara-oqton opened 2 years ago
What exactly are you trying to express? Do I understand it correctly that you mean to express an ndarray
with some literal values such as zeroes [0, 0, 0]
or ones [1, 1, 1]
? If this is the case, then I think the best thing Shape
currently offers is labels:
>>> NDArray[Shape["N zeroes or ones or twos"], Int32]
NDArray[Shape['N zeroes or ones or twos'], Int]
The isinstance
check will not check for the values though, but it won't break either.
It's not so much about the shape of the array, as the values in the array. We would like to limit the values inside the NDArray to these of a limited set. Not all values need to be the same. The best workaround I've found for now is replacing
def my_function() -> NDArray[Shape['Any'], MyEnum]:
by
def my_function() -> NDArray[Shape['Any'], Int]:
which works, but ideally we would like to limit the possible values in the result to a set defined by us.
I see what you mean. This is at this moment not possible; you can express types and shapes, but not values. This would require a new construct.
I could imagine something like this:
NDArray[Any, Value["0 | 1 | 2"]]
Would that express what you want?
Off-topic: Shape['Any']
may not express what you think it does. It expresses 1 dimension of any size, where you may wanted to express any shape at all. You could write Shape["*, ..."]
or just typing.Any
(not within Shape
).
I'm trying to limit the values of an entry in an NDArray to those of an enum class. Here is a minimal example that illustrates one of my tries until now:
The line in question is:
With versions of nptyping < 2 the code above would pass lint checks. Now the following error is thrown:
The documentation provides a list of possible dtypes. In addition, the Structure keyword can be used to build more complex data structures. Is there also some way to restrict values based on an Enum, since the representation of every option is an integer?
I'd also be happy to use something
Integer[Values[0, 1, 2]]
orSet[0, 1, 2]
in the case above.