{"id":5526,"library":"traits","title":"Traits","description":"Traits is a Python library that enables the definition of observable typed attributes for Python classes. It provides features like initialization with default values, validation of assigned values, delegation, notification upon value changes, and automatic user interface generation for attribute modification. Traits is a core component of the Enthought Tool Suite and aims to enhance code clarity and reduce boilerplate, particularly in scientific and engineering applications. The current stable version is 7.1.0, and it generally follows a regular release cadence with major updates addressing Python version compatibility and API refinements.","status":"active","version":"7.1.0","language":"en","source_language":"en","source_url":"https://github.com/enthought/traits","tags":["type-checking","data-validation","observable-attributes","gui","enthought","scientific-computing"],"install":[{"cmd":"pip install traits","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"To support trait types for arrays (e.g., Array trait).","package":"numpy","optional":true},{"reason":"To support GUI views and visualization features for traits.","package":"traitsui","optional":true}],"imports":[{"symbol":"HasTraits","correct":"from traits.api import HasTraits"},{"symbol":"Str","correct":"from traits.api import Str"},{"symbol":"Int","correct":"from traits.api import Int"},{"symbol":"Float","correct":"from traits.api import Float"},{"note":"As of Traits 7.0.0, Color, RGBColor, and Font traits were moved from `traits.api` to `traitsui.api`.","wrong":"from traits.api import Color","symbol":"Color","correct":"from traitsui.api import Color"},{"note":"The `Trait()` function is not recommended for new code and may be deprecated; `Union` should be used instead for compound traits.","wrong":"from traits.api import Trait","symbol":"Trait","correct":"from traits.api import Union"}],"quickstart":{"code":"from traits.api import HasTraits, Str, Float, observe\n\nclass Person(HasTraits):\n    name = Str('John Doe')\n    age = Int(30)\n    height = Float(175.0, desc='Height in cm')\n\n    @observe('age')\n    def _age_changed(self, event):\n        print(f\"Age changed from {event.old} to {event.new}\")\n\n# Create an instance\njoe = Person(name='Joe', age=25)\nprint(f\"Initial: {joe.name}, {joe.age}, {joe.height}\")\n\n# Modify trait attributes\njoe.age = 26 # This will trigger the observer\njoe.height = 180.5\nprint(f\"Updated: {joe.name}, {joe.age}, {joe.height}\")\n\n# Attempt invalid assignment (will raise TraitError)\ntry:\n    joe.age = \"thirty\"\nexcept Exception as e:\n    print(f\"Error: {e}\")","lang":"python","description":"This quickstart demonstrates defining a class with various trait types (Str, Int, Float), setting default values, providing a description, and using an observer to react to trait changes. It also shows how Traits enforces type validation, raising a TraitError on invalid assignments."},"warnings":[{"fix":"Upgrade Python to 3.8 or newer, or pin Traits to <7.0.0 in your project dependencies.","message":"Python versions earlier than 3.8 are no longer supported since Traits 7.0.0. If you are on an older Python version, you must upgrade Python or use an older Traits version (e.g., Traits 6.x for Python 3.7).","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"For mutable default values in `Any` traits, use a dynamic default method (e.g., `_my_list_default`) or explicitly assign a new list/dict instance in each object's `__init__` if per-instance copies are desired.","message":"The behavior of default list or dict values for the `Any` trait type changed in Traits 7.0.0. Previously, a per-instance copy was provided; now, the default value is shared between all instances. This can lead to unexpected shared state across objects if not handled carefully.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Ensure that values assigned to `Date` traits are `datetime.date` objects. Convert `datetime` instances to `datetime.date` using `my_datetime_obj.date()` before assignment.","message":"The `Date` trait type no longer accepts `datetime` instances by default since Traits 7.0.0. Assignments of `datetime` objects to `Date` traits will raise a `TraitError`.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Directly access and modify trait attributes using dot notation (e.g., `obj.trait_name` for both get and set) instead of `obj.get('trait_name')` or `obj.set(trait_name=value)`.","message":"The `HasTraits.get()` and `HasTraits.set()` methods were removed in Traits 7.0.0. These methods were used for accessing and modifying trait values programmatically.","severity":"deprecated","affected_versions":">=7.0.0"},{"fix":"Use specific trait types (e.g., `Str`, `Int`, `Float`) or `Union` for compound types instead of the generic `Trait()` function.","message":"The `Trait()` function for creating trait definitions is deprecated and not recommended for new code. It may be removed in future versions.","severity":"deprecated","affected_versions":"All versions, but specifically >=7.0.0 for future removal consideration"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}