Skip to content

Commit

Permalink
Remove TraitPrefixList and TraitPrefixMap (#1703)
Browse files Browse the repository at this point in the history
This PR removes the deprecated TraitPrefixList and TraitPrefixMap subclasses of TraitHandler, along with supporting machinery.
  • Loading branch information
mdickinson committed Aug 10, 2022
1 parent e99ab5a commit 927e167
Show file tree
Hide file tree
Showing 13 changed files with 14 additions and 509 deletions.
10 changes: 0 additions & 10 deletions docs/source/traits_api_reference/trait_handlers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,3 @@ Private Functions
.. autofunction:: traits.trait_handlers._undefined_get

.. autofunction:: traits.trait_handlers._undefined_set

Deprecated Handlers
-------------------

The following :class:`~.TraitHandler` classes and instances are deprecated,
and may be removed in a future version of Traits.

.. autoclass:: TraitPrefixList

.. autoclass:: TraitPrefixMap
85 changes: 4 additions & 81 deletions docs/source/traits_user_manual/custom.rst
Original file line number Diff line number Diff line change
Expand Up @@ -454,87 +454,10 @@ computed value, or raises a TraitError if the assigned value is not valid. Both
TraitHandler and TraitType derive from BaseTraitHandler; TraitHandler has a more
limited interface.

The Traits package provides a number of predefined TraitHandler subclasses. A few
of the predefined trait handler classes are described in the following sections.
These sections also demonstrate how to define a trait using a trait handler and
the Trait() factory function. For a complete list and descriptions of predefined
TraitHandler subclasses, refer to the *Traits API Reference*, in the section on
the traits.trait_handlers module.

.. index:: TraitPrefixList class

.. _traitprefixlist:

TraitPrefixList
```````````````

.. deprecated:: 6.1
:class:`~.TraitPrefixList` is scheduled for removal
in Traits 7.0. Use the :class:`~.PrefixList` trait type instead.

The TraitPrefixList handler accepts not only a specified set of strings as
values, but also any unique prefix substring of those values. The value assigned
to the trait attribute is the full string that the substring matches.

.. index::
pair: TraitPrefixList class; examples

For example::

>>> from traits.api import HasTraits, Trait
>>> from traits.api import TraitPrefixList
>>> class Alien(HasTraits):
... heads = Trait('one', TraitPrefixList(['one','two','three']))
...
>>> alf = Alien()
>>> alf.heads = 'o'
>>> print(alf.heads)
one
>>> alf.heads = 'tw'
>>> print(alf.heads)
two
>>> alf.heads = 't' # Error, not a unique prefix
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\svn\ets3\traits_3.0.3\enthought\traits\trait_handlers.py", line 1802,
in validate self.error( object, name, value )
File "c:\svn\ets3\traits_3.0.3\enthought\traits\trait_handlers.py", line 175,
in error value )
traits.trait_errors.TraitError: The 'heads' trait of an Alien instance
must be 'one' or 'two' or 'three' (or any unique prefix), but a value of 't'
<type 'str'> was specified.

.. index:: TraitPrefixMap class

.. _traitprefixmap:

TraitPrefixMap
``````````````

.. deprecated:: 6.1
:class:`~.TraitPrefixMap` is scheduled for removal
in Traits 7.0. Use the :class:`~.PrefixMap` trait type instead.

The TraitPrefixMap handler combines the TraitPrefixList with mapped traits. Its
constructor takes a parameter that is a dictionary whose keys are strings. A
string is a valid value if it is a unique prefix for a key in the dictionary.
The value assigned is the dictionary value corresponding to the matched key.

.. index::
pair: TraitPrefixMap class; examples

The following example uses TraitPrefixMap to define a Boolean trait that accepts
any prefix of 'true', 'yes', 'false', or 'no', and maps them to 1 or 0.
::

# traitprefixmap.py --- Example of using the TraitPrefixMap handler
from traits.api import Trait, TraitPrefixMap

boolean_map = Trait('true', TraitPrefixMap( {
'true': 1,
'yes': 1,
'false': 0,
'no': 0 } ) )
The Traits package provides a number of predefined TraitHandler subclasses. For
a complete list and descriptions of predefined TraitHandler subclasses, refer
to the *Traits API Reference*, in the section on the traits.trait_handlers
module.

.. index:: handler classes; custom

Expand Down
2 changes: 0 additions & 2 deletions examples/tutorials/doc_examples/examples/lesson.desc
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ traitenum
traitinstance
traitlist
traitmap
traitprefixlist
traitprefixmap
traitrange
traitstring
traittuple
Expand Down
19 changes: 0 additions & 19 deletions examples/tutorials/doc_examples/examples/traitprefixmap.py

This file was deleted.

2 changes: 0 additions & 2 deletions traits-stubs/traits-stubs/api.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,7 @@ from .trait_handlers import (
TraitInstance as TraitInstance,
TraitFunction as TraitFunction,
TraitEnum as TraitEnum,
TraitPrefixList as TraitPrefixList,
TraitMap as TraitMap,
TraitPrefixMap as TraitPrefixMap,
TraitCompound as TraitCompound,
)

Expand Down
10 changes: 0 additions & 10 deletions traits-stubs/traits-stubs/trait_handlers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,12 @@ class TraitEnum(TraitHandler):
...


class TraitPrefixList(TraitHandler):
def __init__(self,
*values: _Any) -> None:
...


class TraitMap(TraitHandler):
def __init__(self,
map: _Dict = ...) -> None:
...


class TraitPrefixMap(TraitMap):
...


class TraitCompound(TraitHandler):
def __init__(self,
*handlers: _Any) -> None:
Expand Down
2 changes: 0 additions & 2 deletions traits/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,7 @@
TraitInstance,
TraitFunction,
TraitEnum,
TraitPrefixList,
TraitMap,
TraitPrefixMap,
TraitCompound,
)

Expand Down
2 changes: 1 addition & 1 deletion traits/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ValidateTrait(IntEnum):
#: A tuple type check.
tuple = 9

#: A prefix map item check.
#: A prefix map item check (unused).
prefix_map = 10

#: A coercable type check.
Expand Down
46 changes: 1 addition & 45 deletions traits/ctraits.c
Original file line number Diff line number Diff line change
Expand Up @@ -3587,27 +3587,6 @@ validate_trait_map(
return raise_trait_error(trait, obj, name, value);
}

/*-----------------------------------------------------------------------------
| Verifies a Python value is in a specified prefix map (i.e. dictionary):
+----------------------------------------------------------------------------*/

static PyObject *
validate_trait_prefix_map(
trait_object *trait, has_traits_object *obj, PyObject *name,
PyObject *value)
{
PyObject *type_info = trait->py_validate;
PyObject *mapped_value =
PyDict_GetItem(PyTuple_GET_ITEM(type_info, 1), value);
if (mapped_value != NULL) {
Py_INCREF(mapped_value);
return mapped_value;
}

return call_validator(
PyTuple_GET_ITEM(trait->py_validate, 2), obj, name, value);
}

/*-----------------------------------------------------------------------------
| Verifies a Python value is a tuple of a specified type and content:
+----------------------------------------------------------------------------*/
Expand Down Expand Up @@ -4055,20 +4034,6 @@ validate_trait_complex(
PyErr_Clear();
break;

case 10: /* Prefix map item check: */
result = PyDict_GetItem(PyTuple_GET_ITEM(type_info, 1), value);
if (result != NULL) {
Py_INCREF(result);
return result;
}
result = call_validator(
PyTuple_GET_ITEM(type_info, 2), obj, name, value);
if (result != NULL) {
return result;
}
PyErr_Clear();
break;

case 11: /* Coercable type check: */
type = PyTuple_GET_ITEM(type_info, 1);
if (PyObject_TypeCheck(value, (PyTypeObject *)type)) {
Expand Down Expand Up @@ -4267,7 +4232,7 @@ static trait_validate validate_handlers[] = {
validate_trait_complex, /* case 7: TraitComplex item check */
NULL, /* case 8: 'Slow' validate check */
validate_trait_tuple, /* case 9: TupleOf item check */
validate_trait_prefix_map, /* case 10: Prefix map item check */
NULL, /* case 10: Prefix map item check (unused) */
validate_trait_coerce_type, /* case 11: Coercable type check */
validate_trait_cast_type, /* case 12: Castable type check */
validate_trait_function, /* case 13: Function validator check */
Expand Down Expand Up @@ -4379,15 +4344,6 @@ _trait_set_validate(trait_object *trait, PyObject *args)
}
break;

case 10: /* Prefix map item check: */
if (n == 3) {
v1 = PyTuple_GET_ITEM(validate, 1);
if (PyDict_Check(v1)) {
goto done;
}
}
break;

case 11: /* Coercable type check: */
if (n >= 2) {
goto done;
Expand Down
35 changes: 0 additions & 35 deletions traits/tests/test_deprecated_handlers.py

This file was deleted.

43 changes: 0 additions & 43 deletions traits/tests/test_trait_prefix_list.py

This file was deleted.

0 comments on commit 927e167

Please sign in to comment.