Closed ghost closed 3 months ago
Thank you for your interest!
This is a problem with nptyping, the upstream dependency that we based the initial version on (though are slowly moving away from).
At this point we use so little of it that I think i will just vendor the rest of it because nptyping appears to be no longer maintained. Shouldn't take long :)
Alright @kelax try 1.2.3
- should be working with numpy 2.
Also please please please do not be shy about any issues, bugs, gripes, or feature requests no matter how small - i am just now using it in my first project as well so i'm sure there will be plenty of kinks, edge cases, and nice to have things! So i would love to hear what would be good :)
ok, thank you for the quick fix!
BTW, If you ask so I have a dumb question. What is the plus-value of Numpydantic versus this light code :
` from typing import Annotated import numpy as np from numpy.typing import NDArray from pydantic import BeforeValidator, PlainSerializer
def ndarray_to_list(x: NDArray) -> list: return x.tolist()
def list_to_ndarray(data: list) -> NDArray: return np.asarray(data)
NDArray = Annotated[ NDArray, BeforeValidator(list_to_ndarray), PlainSerializer(ndarray_to_list), ] `
allow_arbitrary_types
:( try using it in a model. I assume you meant to cast an array to a list so that it would work with pydantic? (x, y, 3)
thing or a (x, y)
thing or (x, y, 3, t)
thing, and so on.np.array(mymodel.array).sum()
every time which is similarly slow to the tolist()
operation.allow_arbitrary_types
. that can be sort of tricky even for arrays with no specification as it requires a recursive json schema definition, so numpydantic handles that for ya.
from pydantic import BaseModel
from numpydantic import NDArray, Shape
import numpy as np
class MyModel(BaseModel): any_array: NDArray constrained_array: NDArray[Shape["3 x, 4 y, 5 z"], np.uint8]
MyModel.model_json_schema()
(schema is big so putting it behind a toggle)
<details>
<summary>expand/collapse json schema</summary>
```json
{
"$defs":
{
"any-shape-array-2b3b6d5522ac5a35":
{
"anyOf":
[
{
"items":
{
"$ref": "#/$defs/any-shape-array-2b3b6d5522ac5a35"
},
"type": "array"
},
{}
]
}
},
"properties":
{
"any_array":
{
"items":
{
"$ref": "#/$defs/any-shape-array-2b3b6d5522ac5a35"
},
"title": "Any Array",
"type": "array"
},
"constrained_array":
{
"dtype": "numpy.uint8",
"items":
{
"items":
{
"items":
{
"maximum": 255,
"minimum": 0,
"type": "integer"
},
"maxItems": 5,
"minItems": 5,
"type": "array"
},
"maxItems": 4,
"minItems": 4,
"type": "array"
},
"maxItems": 3,
"minItems": 3,
"title": "Constrained Array",
"type": "array"
}
},
"required":
[
"any_array",
"constrained_array"
],
"title": "MyModel",
"type": "object"
}
but yes! just casting to a list is certainly simpler, even if it doesn't do the things this package was designed to do :) so i won't be offended if you choose to do that instead <3
Ok, I understand Thanks!
Hi, Numpydantic seems to be exactly what I'm looking for. Unfortunately, I can't add it to my poetry project's depandancies, as you can see :
`poetry add numpydantic Using version ^1.2.2 for numpydantic
Updating dependencies Resolving dependencies... (0.0s)
Because no versions of numpydantic match >1.2.2,<2.0.0 and numpydantic (1.2.2) depends on nptyping (>=2.5.0), numpydantic (>=1.2.2,<2.0.0) requires nptyping (>=2.5.0). Because no versions of nptyping match >2.5.0 and nptyping (2.5.0) depends on numpy (>=1.20.0,<2.0.0), nptyping (>=2.5.0) requires numpy (>=1.20.0,<2.0.0). Thus, numpydantic (>=1.2.2,<2.0.0) requires numpy (>=1.20.0,<2.0.0). So, because prisme depends on both numpy (^2.0.1) and numpydantic (^1.2.2), version solving failed. `
Thank you for your work !