Repository navigation
feat: add numpy array and scalar types (Fixes #31) - #425
Open
mukktinaadh wants to merge 1 commit into
Open
mukktinaadh wants to merge 1 commit into
mukktinaadh wants to merge 1 commit into
Conversation
- Add NumpyArray type for validating numpy.ndarray with optional dtype and shape constraints - Add NumpyScalar type for validating numpy scalar types - Add NumpyArrayValidator for use with Annotated[np.ndarray, NumpyArrayValidator(...)] - Add proper validation for dtype and shape constraints - Include comprehensive docstrings with examples Fixes pydantic#31
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds numpy array and scalar types to pydantic-extra-types, implementing the feature requested in issue #31.
Changes
New Types Added
NumpyArray- A wrapper type that validatesnumpy.ndarrayobjects with optional dtype and shape constraintsdtypeconstraint (e.g.,dtype=np.float64)shapeconstraint with-1for flexible dimensions (e.g.,shape=(3, -1))np.ndarrayannotation andAnnotated[np.ndarray, NumpyArrayValidator(...)]NumpyScalar- A wrapper type that validates numpy scalar types (np.generic)np.int64,np.float64, etc.NumpyArrayValidator- An annotation for use withAnnotated[np.ndarray, NumpyArrayValidator(...)]dtypeandshapeparametersAnnotated[np.ndarray, NumpyArrayValidator(dtype=np.float64, shape=(3, 3))]Features
-1for any size)Annotated,TypeAdapter, and model fieldsTesting
All existing tests pass. The implementation includes:
NumpyArraywith dtype and shape validationNumpyScalarfor numpy scalar typesNumpyArrayValidatorfor use withAnnotated-1in shape tuplesAnnotatedusage patternsFixes #31