Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional trait type #1786

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Optional Trait - Docs, linting, and another test
k2bd committed May 26, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 907fc67a496c4a723e63c2e9066524ad5116da76
3 changes: 3 additions & 0 deletions docs/source/traits_api_reference/trait_types.rst
Original file line number Diff line number Diff line change
@@ -241,6 +241,9 @@ Traits
.. autoclass:: Union
:show-inheritance:

.. autoclass:: Optional
:show-inheritance:

.. autoclass:: Either
:show-inheritance:

43 changes: 42 additions & 1 deletion docs/source/traits_user_manual/defining.rst
Original file line number Diff line number Diff line change
@@ -265,7 +265,7 @@ the table.
.. index:: Directory(), Disallow, Either(), Enum()
.. index:: Event(), Expression(), false, File()
.. index:: Instance(), List(), Method(), Module()
.. index:: Password(), Property(), Python()
.. index:: Optional(), Password(), Property(), Python()
.. index:: PythonValue(), Range(), ReadOnly(), Regex()
.. index:: Set() String(), This, Time()
.. index:: ToolbarButton(), true, Tuple(), Type()
@@ -355,6 +355,8 @@ the table.
+------------------+----------------------------------------------------------+
| Module | Module([\*\*\ *metadata*]) |
+------------------+----------------------------------------------------------+
| Optional | Optional(*trait*\ [, \*\*\ *metadata*]) |
+------------------+----------------------------------------------------------+
| Password | Password([*value* = '', *minlen* = 0, *maxlen* = |
| | sys.maxsize, *regex* = '', \*\*\ *metadata*]) |
+------------------+----------------------------------------------------------+
@@ -700,6 +702,45 @@ The following example illustrates the difference between `Either` and `Union`::
... primes = Union([2], None, {'3':6}, 5, 7, 11)
ValueError: Union trait declaration expects a trait type or an instance of trait type or None, but got [2] instead

.. index:: Optional trait

.. _optional:

Optional
::::::::
The Optional trait is a shorthand for ``Union(None, *trait*)``. It allows
the value of the trait to be either None or a specified type. The default
value of the trait is None unless specified by ``default_value``.

.. index::
pair: Optional trait; examples

The following is an example of using Optional::

# optional.py --- Example of Optional predefined trait

from traits.api import HasTraits, Optional, Str

class Person(HasTraits):
name = Str
nickname = Optional(Str)

This example defines a ``Person`` with a ``name`` and an optional ``nickname``.
Their ``nickname`` can be ``None`` or a string. For example::

>>> from traits.api import HasTraits, Optional, Str
>>> class Person(HasTraits):
... name = Str
... nickname = Optional(Str)
...
>>> joseph = Person(name="Joseph")
>>> # Joseph has no nickname
>>> joseph.nickname is None
True
>>> joseph.nickname = "Joe"
>>> joseph.nickname
'Joe'

.. index:: Either trait

.. _either:
1 change: 1 addition & 0 deletions traits/api.pyi
Original file line number Diff line number Diff line change
@@ -109,6 +109,7 @@ from .trait_types import (
ToolbarButton as ToolbarButton,
Either as Either,
Union as Union,
Optional as Optional,
Type as Type,
Subclass as Subclass,
Symbol as Symbol,
42 changes: 40 additions & 2 deletions traits/tests/test_optional.py
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@
import unittest

from traits.api import (
Bytes,
DefaultValue,
Float,
HasTraits,
@@ -21,8 +20,8 @@
Str,
TraitError,
TraitType,
Type,
Optional,
Union,
Constant,
)
from traits.trait_types import _NoneTrait
@@ -234,6 +233,45 @@ def _attribute_changed(self, new):
obj.attribute = None
self.assertIsNone(obj.shadow_attribute)

def test_optional_nested(self):
"""
You can nest ``Optional``... if you want to
"""

class TestClass(HasTraits):
attribute = Optional(Optional(Int))

self.assertIsNone(TestClass(attribute=None).attribute)
self.assertIsNone(TestClass().attribute)

obj = TestClass(attribute=3)

obj.attribute = 5
self.assertEqual(obj.attribute, 5)

obj.attribute = None
self.assertIsNone(obj.attribute)

def test_optional_union_of_optional(self):
"""
``Union(T1, Optional(T2))`` acts like ``Union(T1, None, T2)``
"""
class TestClass(HasTraits):
attribute = Union(Int, Optional(Float))

self.assertEqual(TestClass(attribute=3).attribute, 3)
self.assertEqual(TestClass(attribute=3.0).attribute, 3.0)
self.assertIsNone(TestClass(attribute=None).attribute)
self.assertEqual(TestClass().attribute, 0)

a = TestClass(attribute=3)
a.attribute = 5
self.assertEqual(a.attribute, 5)
a.attribute = 5.0
self.assertEqual(a.attribute, 5.0)
a.attribute = None
self.assertIsNone(a.attribute)

def test_optional_extend_trait(self):
class OptionalOrStr(Optional):
def validate(self, obj, name, value):
2 changes: 1 addition & 1 deletion traits/trait_types.py
Original file line number Diff line number Diff line change
@@ -4297,7 +4297,7 @@ class Optional(Union):
trait : a trait or value that can be converted using trait_from()
The type of item that the set contains. Must not be ``None``.
"""
def __init__(self, trait, **metadata):
def __init__(self, trait, **metadata):
if trait is None:
raise TraitError("Optional type must not be None.")