MilanStaffehl / numdantic

Typing support for numpy arrays and numpy array validation with pydantic.
MIT License
1 stars 0 forks source link

Allow unspecified dimensionality with `Shape[int, ...]` #9

Closed MilanStaffehl closed 1 month ago

MilanStaffehl commented 2 months ago

Detect shapes of indetermined size and skip shape validation if first entry is int or an int-like derivative. If the first entry was a Literal, verify that all axes have that exact same length, but skip verification of dimensionality.

MilanStaffehl commented 1 month ago

Making this work with typing is difficult at the moment, for multiple reasons:

The latter means that the only way to make this work reliably would be to write one's own protocol for the numpy.ndarray class, mirroring the behavior of the numpy implementation plus the covariance of the shape type var.

MilanStaffehl commented 1 month ago

I will make the protocol class for numpy.ndarray its own issue. This seems like a good addition as it will also make Literal and named axes compatible with generic shapes. It will be a lot of boilerplate code, but ultimately it will enable accurate typing for Python < 3.12 so that makes it worthwhile, I think.

In the meantime, this issue and its PR should focus on just adding the validation for arrays of unspecified dimensionality to the pydantic validator annotation and removing Shape from the library for this purpose. The invariance of the shape type parameter will be treated as an expected failure in the tests for now.

New issue for protocol is now #48.