Skip to content

Commit

Permalink
Remove the use of cTrait.default_value to set the default value (#1632)
Browse files Browse the repository at this point in the history
The use of cTrait.default_value to set the default value type and default value was deprecated in 2019, in #620. This PR removes that deprecated support. To set the default value type and default value for a CTrait or cTrait instance, use set_default_value.

Co-authored-by: Poruri Sai Rahul <rporuri@enthought.com>
  • Loading branch information
mdickinson and Poruri Sai Rahul committed Apr 20, 2022
1 parent 141e65f commit 99d0aa7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 36 deletions.
32 changes: 9 additions & 23 deletions traits/ctraits.c
Original file line number Diff line number Diff line change
Expand Up @@ -3094,34 +3094,20 @@ _trait_set_default_value(trait_object *trait, PyObject *args)
}

/*-----------------------------------------------------------------------------
| Get or set the 'default_value_type' and 'default_value' fields
| of a CTrait instance. Use of this function for setting the default
| value information is deprecated; use set_default_value instead.
| Get the 'default_value_type' and 'default_value' fields
| of a CTrait instance.
+----------------------------------------------------------------------------*/

static PyObject *
_trait_default_value(trait_object *trait, PyObject *args)
_trait_default_value(trait_object *trait, PyObject *Py_UNUSED(ignored))
{
if (PyArg_ParseTuple(args, "")) {
if (trait->default_value == NULL) {
return Py_BuildValue("iO", 0, Py_None);
}
else {
return Py_BuildValue(
"iO", trait->default_value_type, trait->default_value);
}
if (trait->default_value == NULL) {
return Py_BuildValue("iO", 0, Py_None);
}

PyErr_Clear();
if (PyErr_WarnEx(
PyExc_DeprecationWarning,
"Use of the default_value method with arguments is deprecated. "
"To set defaults, use set_default_value instead.",
1)
!= 0) {
return NULL;
else {
return Py_BuildValue(
"iO", trait->default_value_type, trait->default_value);
}
return _trait_set_default_value(trait, args);
}

/*-----------------------------------------------------------------------------
Expand Down Expand Up @@ -5397,7 +5383,7 @@ static PyMethodDef trait_methods[] = {
PyDoc_STR("__getstate__()")},
{"__setstate__", (PyCFunction)_trait_setstate, METH_VARARGS,
PyDoc_STR("__setstate__(state)")},
{"default_value", (PyCFunction)_trait_default_value, METH_VARARGS,
{"default_value", (PyCFunction)_trait_default_value, METH_NOARGS,
default_value_doc},
{"set_default_value", (PyCFunction)_trait_set_default_value, METH_VARARGS,
set_default_value_doc},
Expand Down
15 changes: 2 additions & 13 deletions traits/tests/test_ctraits.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import sys
import unittest.mock
import warnings
import weakref

from traits.api import HasTraits
Expand Down Expand Up @@ -72,21 +71,11 @@ def test_validate_default_value_for_callable_and_args(self):
trait.set_default_value(
DefaultValue.callable_and_args, value)

def test_default_value_for_set_is_deprecated(self):
def test_default_value_for_set_is_no_longer_supported(self):
trait = CTrait(TraitKind.trait)
with warnings.catch_warnings(record=True) as warn_msgs:
warnings.simplefilter("always", DeprecationWarning)
with self.assertRaises(TypeError):
trait.default_value(DefaultValue.constant, 3.7)

self.assertEqual(len(warn_msgs), 1)
warn_msg = warn_msgs[0]
self.assertIn(
"default_value method with arguments is deprecated",
str(warn_msg.message),
)
_, _, this_module = __name__.rpartition(".")
self.assertIn(this_module, warn_msg.filename)

def test_bad_default_value_type(self):
trait = CTrait(TraitKind.trait)

Expand Down

0 comments on commit 99d0aa7

Please sign in to comment.