freshcrate
Home > Frameworks > traitlets

traitlets

Traitlets Python configuration system

Description

# Traitlets [![Tests](https://github.com/ipython/traitlets/actions/workflows/tests.yml/badge.svg)](https://github.com/ipython/traitlets/actions/workflows/tests.yml) [![Documentation Status](https://readthedocs.org/projects/traitlets/badge/?version=latest)](https://traitlets.readthedocs.io/en/latest/?badge=latest) [![Tidelift](https://tidelift.com/subscription/pkg/pypi-traitlets)](https://tidelift.com/badges/package/pypi/traitlets) | | | | ------------- | ------------------------------------ | | **home** | https://github.com/ipython/traitlets | | **pypi-repo** | https://pypi.org/project/traitlets/ | | **docs** | https://traitlets.readthedocs.io/ | | **license** | Modified BSD License | Traitlets is a pure Python library enabling: - the enforcement of strong typing for attributes of Python objects (typed attributes are called _"traits"_); - dynamically calculated default values; - automatic validation and coercion of trait attributes when attempting a change; - registering for receiving notifications when trait values change; - reading configuring values from files or from command line arguments - a distinct layer on top of traitlets, so you may use traitlets without the configuration machinery. Its implementation relies on the [descriptor](https://docs.python.org/howto/descriptor.html) pattern, and it is a lightweight pure-python alternative of the [_traits_ library](https://docs.enthought.com/traits/). Traitlets powers the configuration system of IPython and Jupyter and the declarative API of IPython interactive widgets. ## Installation For a local installation, make sure you have [pip installed](https://pip.pypa.io/en/stable/installing/) and run: ```bash pip install traitlets ``` For a **development installation**, clone this repository, change into the `traitlets` root directory, and run pip: ```bash git clone https://github.com/ipython/traitlets.git cd traitlets pip install -e . ``` ## Running the tests ```bash pip install "traitlets[test]" py.test traitlets ``` ## Code Styling `traitlets` has adopted automatic code formatting so you shouldn't need to worry too much about your code style. As long as your code is valid, the pre-commit hook should take care of how it should look. To install `pre-commit` locally, run the following:: ``` pip install pre-commit pre-commit install ``` You can invoke the pre-commit hook by hand at any time with:: ``` pre-commit run ``` which should run any autoformatting on your code and tell you about any errors it couldn't fix automatically. You may also install [black integration](https://github.com/psf/black#editor-integration) into your text editor to format code automatically. If you have already committed files before setting up the pre-commit hook with `pre-commit install`, you can fix everything up using `pre-commit run --all-files`. You need to make the fixing commit yourself after that. Some of the hooks only run on CI by default, but you can invoke them by running with the `--hook-stage manual` argument. ## Usage Any class with trait attributes must inherit from `HasTraits`. For the list of available trait types and their properties, see the [Trait Types](https://traitlets.readthedocs.io/en/latest/trait_types.html) section of the documentation. ### Dynamic default values To calculate a default value dynamically, decorate a method of your class with `@default({traitname})`. This method will be called on the instance, and should return the default value. In this example, the `_username_default` method is decorated with `@default('username')`: ```Python import getpass from traitlets import HasTraits, Unicode, default class Identity(HasTraits): username = Unicode() @default('username') def _username_default(self): return getpass.getuser() ``` ### Callbacks when a trait attribute changes When a trait changes, an application can follow this trait change with additional actions. To do something when a trait attribute is changed, decorate a method with [`traitlets.observe()`](https://traitlets.readthedocs.io/en/latest/api.html?highlight=observe#traitlets.observe). The method will be called with a single argument, a dictionary which contains an owner, new value, old value, name of the changed trait, and the event type. In this example, the `_num_changed` method is decorated with `` @observe(`num`) ``: ```Python from traitlets import HasTraits, Integer, observe class TraitletsExample(HasTraits): num = Integer(5, help="a number").tag(config=True) @observe('num') def _num_changed(self, change): print("{name} changed from {old} to {new}".format(**change)) ``` and is passed the following dictionary when called: ```Python { 'owner': object, # The HasTraits instance 'new': 6, # The new value 'old': 5, # The old value 'name': "foo", # The name of the changed trait 'type': 'change', # The

Release History

VersionChangesUrgencyDate
5.14.3Imported from PyPI (5.14.3)Low4/21/2026
v5.14.3 ## 5.14.3 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.14.2...5cf34f6c42b6e39aa1e53394443efca3d7329e3b)) ### Bugs fixed - Fix assigning string value to Set [#903](https://github.com/ipython/traitlets/pull/903) ([@azjps](https://github.com/azjps)) ### Maintenance and upkeep improvements - Bump the actions group with 1 update [#899](https://github.com/ipython/traitlets/pull/899) ([@dependabot](https://github.com/dependabot)) ### Contributors to this release ([GitHub cLow4/19/2024
v5.14.2 ## 5.14.2 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.14.1...2d0fb91f39a71c5528860e23bf9c2138e441b319)) ### Maintenance and upkeep improvements - Update Release Scripts [#900](https://github.com/ipython/traitlets/pull/900) ([@blink1073](https://github.com/blink1073)) - chore: update pre-commit hooks [#897](https://github.com/ipython/traitlets/pull/897) ([@pre-commit-ci](https://github.com/pre-commit-ci)) - Bump the actions group with 1 update [#896](https://github.com/Low3/12/2024
v5.14.1 ## 5.14.1 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.14.0...a1108c92013f9da56a9dccb79f7a5453ffe8d1db)) ### Bugs fixed - FIX: map - to _ for environement variable prefix. [#895](https://github.com/ipython/traitlets/pull/895) ([@Carreau](https://github.com/Carreau)) ### Maintenance and upkeep improvements - chore: update pre-commit hooks [#894](https://github.com/ipython/traitlets/pull/894) ([@pre-commit-ci](https://github.com/pre-commit-ci)) - Update ruff config [#89Low1/2/2024
v5.14.0 ## 5.14.0 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.13.0...4d7504601b9db9eb6f31dfc102b9c469c8517496)) ### Enhancements made - Add option to load traitlets values from environement. [#856](https://github.com/ipython/traitlets/pull/856) ([@Carreau](https://github.com/Carreau)) ### Maintenance and upkeep improvements - Update types for mypy 1.7 [#892](https://github.com/ipython/traitlets/pull/892) ([@blink1073](https://github.com/blink1073)) - chore: update pre-commiLow11/27/2023
v5.13.0 ## 5.13.0 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.12.0...dacbf9b03ebecbd09604ab2fa41e088e3f60b403)) ### Bugs fixed - Use passed-in `info` argument for `TraitType.error()` [#884](https://github.com/ipython/traitlets/pull/884) ([@kylebarron](https://github.com/kylebarron)) ### Maintenance and upkeep improvements - Improve generics handling [#886](https://github.com/ipython/traitlets/pull/886) ([@blink1073](https://github.com/blink1073)) - Finish Strict Typing [#885Low10/30/2023
v5.12.0 ## 5.12.0 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.11.2...b018581f0acd75844bbd7584e233c3ab3126501b)) ### Enhancements made - Pass \*\*kwds in MetaHasDescriptors __new__ and __init__ [#881](https://github.com/ipython/traitlets/pull/881) ([@fleming79](https://github.com/fleming79)) ### Maintenance and upkeep improvements - Adopt sp-repo-review [#883](https://github.com/ipython/traitlets/pull/883) ([@blink1073](https://github.com/blink1073)) - Cleanup after remove PyLow10/25/2023
v5.11.2 ## 5.11.2 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.11.1...e238765a0ba28fc68318cff75e852958d5c65c93)) ### Maintenance and upkeep improvements - More typing cleanup [#877](https://github.com/ipython/traitlets/pull/877) ([@blink1073](https://github.com/blink1073)) ### Contributors to this release ([GitHub contributors page for this release](https://github.com/ipython/traitlets/graphs/contributors?from=2023-10-03&to=2023-10-03&type=c)) [@blink1073](https://github.comLow10/3/2023
v5.11.1 ## 5.11.1 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.11.0...292a699ad1a3a3ece760a8335cae73927cccd711)) ### Maintenance and upkeep improvements - Typing fixups [#875](https://github.com/ipython/traitlets/pull/875) ([@blink1073](https://github.com/blink1073)) ### Contributors to this release ([GitHub contributors page for this release](https://github.com/ipython/traitlets/graphs/contributors?from=2023-10-03&to=2023-10-03&type=c)) [@blink1073](https://github.com/searcLow10/3/2023
v5.11.0 ## 5.11.0 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.10.1...24a24508260d501abd6bc8b42b59163c6c07d8c4)) ### Maintenance and upkeep improvements - More Typing Adoption [#873](https://github.com/ipython/traitlets/pull/873) ([@blink1073](https://github.com/blink1073)) ### Contributors to this release ([GitHub contributors page for this release](https://github.com/ipython/traitlets/graphs/contributors?from=2023-09-26&to=2023-10-03&type=c)) [@blink1073](https://github.coLow10/3/2023
v5.10.1 ## 5.10.1 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.10.0...c0a3afdb2dcb28bd7ade6dd730604df8f3fbfc78)) ### Maintenance and upkeep improvements - Typing upgrades [#868](https://github.com/ipython/traitlets/pull/868) ([@blink1073](https://github.com/blink1073)) ### Contributors to this release ([GitHub contributors page for this release](https://github.com/ipython/traitlets/graphs/contributors?from=2023-09-14&to=2023-09-26&type=c)) [@blink1073](https://github.com/seaLow9/26/2023
v5.10.0 ## 5.10.0 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.9.0...6588680dc69be75676ca3835b4061b1784232600)) ### Enhancements made - Added type hints to HasTraits.observe() and friends. (#834 [#834](https://github.com/ipython/traitlets/pull/834) ([@scholer](https://github.com/scholer)) - feat: trait typing [#818](https://github.com/ipython/traitlets/pull/818) ([@maartenbreddels](https://github.com/maartenbreddels)) ### Maintenance and upkeep improvements - Fix mypy deps anLow9/14/2023
v5.9.0 ## 5.9.0 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.8.1...c11a4d942b08df5c19be88b6cc81dfa8302fef9b)) ### Enhancements made - Polishing argcomplete support [#829](https://github.com/ipython/traitlets/pull/829) ([@azjps](https://github.com/azjps)) ### Maintenance and upkeep improvements - Test that name and description can be set via constructor. [#826](https://github.com/ipython/traitlets/pull/826) ([@Carreau](https://github.com/Carreau)) ### Contributors to this reLow1/30/2023
v5.8.1 ## 5.8.1 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.8.0...18814204c7e7987851cc1836a36863b4fab60165)) ### Bugs fixed - fix: mro_trait can be unbound when the class is not in mro [#824](https://github.com/ipython/traitlets/pull/824) ([@maartenbreddels](https://github.com/maartenbreddels)) ### Maintenance and upkeep improvements ### Documentation improvements - Add more api docs [#821](https://github.com/ipython/traitlets/pull/821) ([@blink1073](https://github.com/bliLow1/9/2023
v5.8.0 ## 5.8.0 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.7.1...47e652f96aff54d1aa3b19337c9c8d80fe0fd4c4)) ### Enhancements made -Shell command-line tab-completion via `argcomplete` [#811](https://github.com/ipython/traitlets/pull/811) ([@azjps](https://github.com/azjps)) - Define `trait.__doc__` = `trait.help` for better API Docs [#816](https://github.com/ipython/traitlets/pull/816) ([@minrk](https://github.com/minrk)) ### Maintenance and upkeep improvements - Expose moLow12/19/2022
v5.7.1 ## 5.7.1 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.7.0...aa0d38bf02d34a6df788477da30eac6e58ffbda5)) ### Bugs fixed - fix: some descriptors raise AttributeError [#812](https://github.com/ipython/traitlets/pull/812) ([@maartenbreddels](https://github.com/maartenbreddels)) ### Contributors to this release ([GitHub contributors page for this release](https://github.com/ipython/traitlets/graphs/contributors?from=2022-12-08&to=2022-12-12&type=c)) [@maartenbreddels](httpLow12/12/2022
v5.7.0 ## 5.7.0 ([Full Changelog](https://github.com/ipython/traitlets/compare/v5.6.0...f07afea52cf6314bc20571c52409ff6cb115a709)) ### Enhancements made - Performance: decrease runtime overhead for constructing HasTraits (up to 20x faster) [#777](https://github.com/ipython/traitlets/pull/777) ([@maartenbreddels](https://github.com/maartenbreddels)) ### Maintenance and upkeep improvements - Minor fixes for Application.aliases [#810](https://github.com/ipython/traitlets/pull/810) ([@azjps](https:/Low12/8/2022
v5.6.0 ## 5.6.0 ([Full Changelog](https://github.com/ipython/traitlets/compare/5.5.0...2c5188a3562f03c0703315b21df41ca7ace23dd3)) ### Maintenance and upkeep improvements - Adopt jupyter releaser [#806](https://github.com/ipython/traitlets/pull/806) ([@blink1073](https://github.com/blink1073)) - Use base setup dependency type [#805](https://github.com/ipython/traitlets/pull/805) ([@blink1073](https://github.com/blink1073)) - More CI Cleanup [#803](https://github.com/ipython/traitlets/pull/803) ([@bLow11/29/2022
5.5.0## What's Changed * Update tests and docs to use non-deprecated functions by @jasongrout in https://github.com/ipython/traitlets/pull/778 * Clean up version handling by @blink1073 in https://github.com/ipython/traitlets/pull/780 * Prep for jupyter releaser by @blink1073 in https://github.com/ipython/traitlets/pull/783 * Format the changelog by @blink1073 in https://github.com/ipython/traitlets/pull/785 * Clean up application typing by @blink1073 in https://github.com/ipython/traitlets/pull/Low10/18/2022
5.4.0## What's Changed * Fix version_info by @minrk in https://github.com/ipython/traitlets/pull/749 * Make generated config files more lintable by @minrk in https://github.com/ipython/traitlets/pull/750 * Fix union from string by @krassowski in https://github.com/ipython/traitlets/pull/773 * Cover more Union parsing scenarios by @krassowski in https://github.com/ipython/traitlets/pull/774 * Upgrade GitHub Actions by @cclauss in https://github.com/ipython/traitlets/pull/760 * Fix and refactorLow9/12/2022
5.3.0## What's Changed * Fix traitlet name in docstring by @fcollonval in https://github.com/ipython/traitlets/pull/745 * Re-support multiple-alias key for ArgParseConfigLoader by @azjps in https://github.com/ipython/traitlets/pull/687 ## New Contributors * @fcollonval made their first contribution in https://github.com/ipython/traitlets/pull/745 * @azjps made their first contribution in https://github.com/ipython/traitlets/pull/687 **Full Changelog**: https://github.com/ipython/traitlets/cLow6/16/2022
5.2.2## What's Changed * Make __all__ explicit and validate in test by @blink1073 in https://github.com/ipython/traitlets/pull/737 * Fix KeyError in Application.get_default_logging_config by @tonyroberts in https://github.com/ipython/traitlets/pull/724 ## New Contributors * @tonyroberts made their first contribution in https://github.com/ipython/traitlets/pull/724 **Full Changelog**: https://github.com/ipython/traitlets/compare/5.2.1...5.3.0Low5/31/2022
5.2.1## What's Changed * Switch to hatch backend by @blink1073 in https://github.com/ipython/traitlets/pull/728 * logging: don't attempt to close handlers unless they have been opened by @oliver-sanders in https://github.com/ipython/traitlets/pull/727 **Full Changelog**: https://github.com/ipython/traitlets/compare/5.2.0...5.2.1Low5/16/2022
5.2.0## What's Changed * Add mypy support by @blink1073 in https://github.com/ipython/traitlets/pull/705 * Disambiguate the use of the term value in the example. by @metaperl in https://github.com/ipython/traitlets/pull/694 * Correct spelling and grammar errors. by @metaperl in https://github.com/ipython/traitlets/pull/695 * DOC: Add log example by @Carreau in https://github.com/ipython/traitlets/pull/693 * Put description of `signature_has_traits` after the header for it. by @metaperl in https:Low5/10/2022

Dependencies & License Audit

Loading dependencies...

Similar Packages

jupyterlab-widgetsJupyter interactive widgets for JupyterLab3.0.16
widgetsnbextensionJupyter interactive widgets for Jupyter Notebook4.0.15
jupyter-clientJupyter protocol implementation and client libraries8.8.0
ipythonIPython: Productive Interactive Computing9.12.0
sphinx-designA sphinx extension for designing beautiful, view size responsive web components.0.7.0