p2p-ld / numpydantic

Type annotations for specifying, validating, and serializing arrays with arbitrary backends in Pydantic (and beyond)
https://numpydantic.readthedocs.io/
MIT License
66 stars 1 forks source link

Better compound/generic dtypes with Union #19

Closed sneakers-the-rat closed 1 month ago

sneakers-the-rat commented 2 months ago

From: https://github.com/LiberTEM/LiberTEM-schema/pull/7

We want to be able to specify "any kind of float" or "any kind of int." Currently these are implemented by a tuple of types, bridging from nptyping's version, but they should actually be unions.

We could just implement support for Unions directly and make convenience unions in the dtype module. initially this wasn't done to avoid introducing the complexity that comes with handling the full python typing API (eg. what would it mean to do NDArray[Any, tuple[int, float]] ) but it's pretty clear that Unions at least should be supported.

We could make a type that wraps multiple types, but that would introduce more complexity down the line when we move to a proper Protocol Generic for NDArray and want to make type hints work, so I think Union support is the way to go here.