Skip to content

Commit

Permalink
Merge pull request #36 from takluyver/doc-defining-traits
Browse files Browse the repository at this point in the history
Add short document about defining trait types
  • Loading branch information
minrk committed Jun 16, 2015
2 parents 4f23da2 + e1c1111 commit 46f6421
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/source/defining_traits.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Defining new trait types
========================

.. py:currentmodule:: traitlets
To define a new trait type, subclass from :class:`TraitType`. You can define the
following things:

.. class:: MyTrait

.. attribute:: info_text

A short string describing what this trait should hold.

.. attribute:: default_value

A default value, if one makes sense for this trait type. If there is no
obvious default, don't provide this.

.. method:: validate(obj, value)

Check whether a given value is valid. If it is, it should return the value
(coerced to the desired type, if necessary). If not, it should raise
:exc:`TraitError`. :meth:`TraitType.error` is a convenient way to raise an
descriptive error saying that the given value is not of the required type.

``obj`` is the object to which the trait belongs.

For instance, here's the definition of the :class:`TCPAddress` trait:

.. literalinclude:: /../../traitlets/traitlets.py
:pyobject: TCPAddress
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ the configuration machinery.

using_traitlets
trait_types
defining_traits
config

0 comments on commit 46f6421

Please sign in to comment.