You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Document that Either should not be used in new code (#1699)
This PR adds notes to the user manual and API documentation to discourage users from using Either in new code. (Union should be preferred.)
(cherry picked from commit c5cfdde)
@@ -638,56 +638,12 @@ prefix. Instantiating the class produces the following::
638
638
>>> print(bob.married)
639
639
no
640
640
641
-
.. index:: Either trait
642
-
643
-
.. _either:
644
-
645
-
Either
646
-
::::::
647
-
Another predefined trait that merits special explanation is Either. The
648
-
Either trait is intended for attributes that may take a value of more than
649
-
a single trait type, including None. The default value of Either is None, even
650
-
if None is not one of the types the user explicitly defines in the constructor,
651
-
but a different default value can be provided using the ``default`` argument.
652
-
653
-
.. index::
654
-
pair: Either trait; examples
655
-
656
-
The following is an example of using Either::
657
-
658
-
# either.py --- Example of Either predefined trait
659
-
660
-
from traits.api import HasTraits, Either, Str
661
-
662
-
class Employee(HasTraits):
663
-
manager_name = Either(Str, None)
664
-
665
-
This example defines an Employee class, which has a **manager_name** trait
666
-
attribute, which accepts either an Str instance or None as its value, and
667
-
will raise a TraitError if a value of any other type is assigned. For example::
668
-
669
-
>>> from traits.api import HasTraits, Either, Str
670
-
>>> class Employee(HasTraits):
671
-
... manager_name = Either(Str, None)
672
-
...
673
-
>>> steven = Employee(manager_name="Jenni")
674
-
>>> # Here steven's manager is named "Jenni"
675
-
>>> steven.manager_name
676
-
'Jenni'
677
-
>>> eric = Employee(manager_name=None)
678
-
>>> # Eric is the boss, so he has no manager.
679
-
>>> eric.manager_name is None
680
-
True
681
-
>>> # Assigning a value that is neither a string nor None will fail.
682
-
>>> steven.manager_name = 5
683
-
traits.trait_errors.TraitError: The 'manager_name' trait of an Employee instance must be a string or None, but a value of 5 <type 'int'> was specified.
684
-
685
641
.. index:: Union trait
686
642
687
643
.. _union:
688
644
689
645
Union
690
-
::::::
646
+
:::::
691
647
The Union trait accepts a value that is considered valid by at least one
692
648
of the traits in its definition. It is a simpler and therefore less error-prone
693
649
alternative to the `Either` trait, which allows more complex constructs and
@@ -719,7 +675,7 @@ attribute, which accepts either an Str instance or None as its value, a
719
675
**salary** trait that accepts an instance of Salary or Float and will raise a
720
676
TraitError if a value of any other type is assigned. For example::
721
677
722
-
>>> from traits.api import HasTraits, Either, Str
678
+
>>> from traits.api import HasTraits, Str, Union
723
679
>>> class Employee(HasTraits):
724
680
... manager_name = Union(Str, None)
725
681
...
@@ -731,7 +687,7 @@ TraitError if a value of any other type is assigned. For example::
731
687
732
688
The following example illustrates the difference between `Either` and `Union`::
733
689
734
-
>>> from traits.api import HasTraits, Either, Union, Str
690
+
>>> from traits.api import Either, HasTraits, Str, Union
735
691
>>> class IntegerClass(HasTraits):
736
692
... primes = Either([2], None, {'3':6}, 5, 7, 11)
737
693
...
@@ -744,6 +700,56 @@ The following example illustrates the difference between `Either` and `Union`::
744
700
... primes = Union([2], None, {'3':6}, 5, 7, 11)
745
701
ValueError: Union trait declaration expects a trait type or an instance of trait type or None, but got [2] instead
746
702
703
+
.. index:: Either trait
704
+
705
+
.. _either:
706
+
707
+
Either
708
+
::::::
709
+
710
+
.. note::
711
+
The :class:`~.Either` trait type may eventually be deprecated, and should
712
+
not be used in new code. Use the more well-behaved :class:`~.Union` trait
713
+
type instead.
714
+
715
+
Another predefined trait that merits special explanation is Either. The
716
+
Either trait is intended for attributes that may take a value of more than
717
+
a single trait type, including None. The default value of Either is None, even
718
+
if None is not one of the types the user explicitly defines in the constructor,
719
+
but a different default value can be provided using the ``default`` argument.
720
+
721
+
.. index::
722
+
pair: Either trait; examples
723
+
724
+
The following is an example of using Either::
725
+
726
+
# either.py --- Example of Either predefined trait
727
+
728
+
from traits.api import HasTraits, Either, Str
729
+
730
+
class Employee(HasTraits):
731
+
manager_name = Either(Str, None)
732
+
733
+
This example defines an Employee class, which has a **manager_name** trait
734
+
attribute, which accepts either an Str instance or None as its value, and
735
+
will raise a TraitError if a value of any other type is assigned. For example::
736
+
737
+
>>> from traits.api import HasTraits, Either, Str
738
+
>>> class Employee(HasTraits):
739
+
... manager_name = Either(Str, None)
740
+
...
741
+
>>> steven = Employee(manager_name="Jenni")
742
+
>>> # Here steven's manager is named "Jenni"
743
+
>>> steven.manager_name
744
+
'Jenni'
745
+
>>> eric = Employee(manager_name=None)
746
+
>>> # Eric is the boss, so he has no manager.
747
+
>>> eric.manager_name is None
748
+
True
749
+
>>> # Assigning a value that is neither a string nor None will fail.
750
+
>>> steven.manager_name = 5
751
+
traits.trait_errors.TraitError: The 'manager_name' trait of an Employee instance must be a string or None, but a value of 5 <type 'int'> was specified.
0 commit comments