{"id":4288,"library":"traittypes","title":"Trait Types for SciPy and NumPy","description":"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.","status":"maintenance","version":"0.2.3","language":"en","source_language":"en","source_url":"https://github.com/QuantStack/traittypes","tags":["traits","traitlets","numpy","scipy","types","validation","jupyter"],"install":[{"cmd":"pip install traittypes","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core dependency for the trait system.","package":"traitlets"},{"reason":"Required for `Array` and `ArrayType` traits.","package":"numpy"},{"reason":"Required for `ScipySparse` and `ScipySparseType` traits.","package":"scipy"}],"imports":[{"symbol":"Array","correct":"from traittypes import Array"},{"symbol":"ArrayType","correct":"from traittypes import ArrayType"},{"symbol":"ScipySparse","correct":"from traittypes import ScipySparse"},{"symbol":"ScipySparseType","correct":"from traittypes import ScipySparseType"}],"quickstart":{"code":"from traitlets import HasTraits, validate, TraitError\nfrom traittypes import Array\nimport numpy as np\n\nclass DataContainer(HasTraits):\n    data = Array(help=\"A NumPy array container.\")\n    matrix_shape = Array(shape=(2,), help=\"Shape of a matrix (rows, cols).\")\n\n    @validate('data')\n    def _valid_data(self, proposal):\n        if proposal['value'].ndim != 1:\n            raise TraitError('data must be a 1D array.')\n        return proposal['value']\n\n# Instantiate the container\ncontainer = DataContainer()\n\n# Assign a valid array\ncontainer.data = np.array([1, 2, 3, 4])\nprint(f\"Assigned 1D data: {container.data}\")\n\n# Attempt to assign an invalid array (will raise TraitError)\ntry:\n    container.data = np.array([[1, 2], [3, 4]])\nexcept TraitError as e:\n    print(f\"Error assigning 2D data: {e}\")\n\n# Assign a valid shape array\ncontainer.matrix_shape = np.array([10, 20])\nprint(f\"Assigned matrix_shape: {container.matrix_shape}\")\n\n# Attempt to assign an invalid shape array (will raise TraitError due to 'shape' constraint)\ntry:\n    container.matrix_shape = np.array([5, 6, 7])\nexcept TraitError as e:\n    print(f\"Error assigning wrong shape: {e}\")","lang":"python","description":"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."},"warnings":[{"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.","message":"The `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.","severity":"gotcha","affected_versions":"0.2.3 and prior"},{"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`.","message":"When 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"`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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}