Registry / data / traittypes

traittypes

JSON →
library0.2.3pypypi✓ verified 24d ago

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 traittypes
INSTALL
IMPORT
SIG · TRAITTYPES
T
traittypes
datapythonv0.2.3
Install
1.6s avg
Import
64ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.068s · 18.6MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.060s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Array
from traittypes import Array
ArrayType
from traittypes import ArrayType
ScipySparse
from traittypes import ScipySparse
ScipySparseType
from traittypes import ScipySparseType

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.

from traitlets import HasTraits, validate, TraitError from traittypes import Array import numpy as np class DataContainer(HasTraits): data = Array(help="A NumPy array container.") matrix_shape = Array(shape=(2,), help="Shape of a matrix (rows, cols).") @validate('data') def _valid_data(self, proposal): if proposal['value'].ndim != 1: raise TraitError('data must be a 1D array.') return proposal['value'] # Instantiate the container container = DataContainer() # Assign a valid array container.data = np.array([1, 2, 3, 4]) print(f"Assigned 1D data: {container.data}") # Attempt to assign an invalid array (will raise TraitError) try: container.data = np.array([[1, 2], [3, 4]]) except TraitError as e: print(f"Error assigning 2D data: {e}") # Assign a valid shape array container.matrix_shape = np.array([10, 20]) print(f"Assigned matrix_shape: {container.matrix_shape}") # Attempt to assign an invalid shape array (will raise TraitError due to 'shape' constraint) try: container.matrix_shape = np.array([5, 6, 7]) except TraitError as e: print(f"Error assigning wrong shape: {e}")
Debug
Known issues
gotchaThe `traittypes` project is in maintenance mode and has not seen significant development since early 2021. Users should be aware that new features or bug fixes are unlikely to be implemented.
fix
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.
affects: 0.2.3 and prior
gotchaWhen assigning NumPy arrays to `Array` traits, the array object itself is stored; `traittypes` (like `traitlets`) does not create a deep copy by default. This means modifying the assigned array externally will reflect in the trait's value.
fix
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`.
affects: All versions
gotcha`traittypes` relies on `traitlets`' coercion mechanisms. Values assigned to traits may be implicitly converted to the expected type (e.g., a list might be converted to a NumPy array). This can sometimes hide subtle type mismatches if strict type checking is expected.
fix
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.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'traittypes'
The `traittypes` package is not installed in the current Python environment.
fix
Install the `traittypes` library using pip: `pip install traittypes`
TraitError: The 'my_array' trait of a MyClass instance must be a numpy.ndarray, but a list was specified.
You attempted to assign a value of an incorrect type (e.g., a Python list) to a `traittypes.Array` trait, which strictly expects a `numpy.ndarray` instance.
fix
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])`
ImportError: cannot import name 'DataFrame' from 'traittypes'
The `traittypes.DataFrame` trait type is only available if the `pandas` library is installed. If `pandas` is not found, `traittypes` conditionally omits the `DataFrame` trait.
fix
Install the `pandas` library to enable the `DataFrame` trait type: `pip install pandas`
TypeError: an integer is required (got type int)
You tried to initialize a `traittypes.Array` with a single integer (e.g., `Array(10)`). The underlying `numpy.ndarray` constructor expects a shape tuple or an object to convert to an array when given positional arguments, not just a single integer representing an array's value.
fix
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()`).
Upgrade
Version history
0.2.3latest on PyPI · released Oct 22, 2025
Audit
Dependencies
traitletsrequiredCore dependency for the trait system.
numpyrequiredRequired for `Array` and `ArrayType` traits.
scipyrequiredRequired for `ScipySparse` and `ScipySparseType` traits.
Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
traittypes — pip install traittypes · libregistry