traittypes provides trait types extending the `traitlets` library, specifically designed for common data structures in the SciPy ecosystem, such as NumPy arrays and SciPy sparse matrices. It allows developers to define robust type-checked attributes for these data types in `traitlets`-based classes. The current version is 0.2.3. The library appears to be in maintenance mode, with no significant development since early 2021, meaning new features or bug fixes are infrequent.
pip install traittypesVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates defining a `HasTraits` class with `traittypes.Array` attributes. It shows how to assign NumPy arrays, enforce dimensionality with custom validation, and utilize the `shape` keyword argument for structural constraints.
Consider the long-term stability and lack of ongoing support when deciding to use `traittypes` for new projects. For existing projects, be prepared to fork or implement custom solutions for any new requirements or bugs.
If immutability or defensive copying is required, explicitly create a copy of the array (e.g., `value.copy()`) before assigning it to the trait, or handle mutability within custom validation logic using `@validate`.
For stricter validation without implicit coercion, use custom validation methods (e.g., `@validate('my_trait')`) to explicitly check the type and content of `proposal['value']` before it's assigned.Install the `traittypes` library using pip: `pip install traittypes`
Ensure that any value assigned to a `traittypes.Array` trait is an actual `numpy.ndarray`. For example, convert a list to an array: `instance.my_array = np.array([1, 2, 3])`
Install the `pandas` library to enable the `DataFrame` trait type: `pip install pandas`
Initialize `traittypes.Array` with a default `numpy.ndarray` instance (e.g., `Array(np.zeros(3))`) or provide no default if it's not immediately required (e.g., `Array()`).