Skip to content

Commit

Permalink
Add DefaultValue.disallow (#1546)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdickinson authored Sep 24, 2021
1 parent 5b1d5ef commit 82658dc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
2 changes: 2 additions & 0 deletions traits-stubs/traits-stubs/constants.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class DefaultValue(IntEnum):
callable_and_args: int = ...
callable: int = ...
trait_set_object: int = ...
disallow: int = ...


MAXIMUM_DEFAULT_VALUE_TYPE: Any
default_value_map: Any
5 changes: 5 additions & 0 deletions traits/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ class DefaultValue(IntEnum):
#: is the default value.
trait_set_object = traits.ctraits._TRAIT_SET_OBJECT_DEFAULT_VALUE

#: This trait doesn't permit a default value, and an attempt to retrieve
#: the default value using the default_value_for method will fail.
disallow = traits.ctraits._DISALLOW_DEFAULT_VALUE


#: Maximum legal value for default_value_type, for use in testing
#: and validation.
Expand All @@ -215,4 +219,5 @@ class DefaultValue(IntEnum):
DefaultValue.callable_and_args: "factory",
DefaultValue.callable: "method",
DefaultValue.trait_set_object: "set",
DefaultValue.disallow: "invalid",
}
1 change: 1 addition & 0 deletions traits/ctrait.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def default(self):
DefaultValue.object,
DefaultValue.callable_and_args,
DefaultValue.callable,
DefaultValue.disallow,
):
return Undefined
elif kind in (
Expand Down
19 changes: 18 additions & 1 deletion traits/ctraits.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,12 @@ call_notifiers(
is the default value */
#define TRAIT_SET_OBJECT_DEFAULT_VALUE 9

/* This trait doesn't permit a default value */
#define DISALLOW_DEFAULT_VALUE 10

/* Maximum legal value for default_value_type, for use in testing and
validation. */
#define MAXIMUM_DEFAULT_VALUE_TYPE 9
#define MAXIMUM_DEFAULT_VALUE_TYPE 10

/* The maximum value for comparison_mode. Valid values are between 0 and
the maximum value. */
Expand Down Expand Up @@ -1845,6 +1848,12 @@ default_value_for(trait_object *trait, has_traits_object *obj, PyObject *name)
case TRAIT_SET_OBJECT_DEFAULT_VALUE:
return call_class(
TraitSetObject, trait, obj, name, trait->default_value);
case DISALLOW_DEFAULT_VALUE:
PyErr_SetString(
PyExc_ValueError,
"default value not permitted for this trait"
);
return NULL;
}
return result;
}
Expand Down Expand Up @@ -5726,6 +5735,14 @@ PyInit_ctraits(void)
if (error < 0) {
return NULL;
}
error = PyModule_AddIntConstant(
module,
"_DISALLOW_DEFAULT_VALUE",
DISALLOW_DEFAULT_VALUE
);
if (error < 0) {
return NULL;
}
error = PyModule_AddIntConstant(
module,
"_MAXIMUM_DEFAULT_VALUE_TYPE",
Expand Down
22 changes: 22 additions & 0 deletions traits/tests/test_trait_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ class MyTraitType(TraitType):
(DefaultValue.constant, Undefined),
)

def test_disallowed_default_value(self):
class MyTraitType(TraitType):

default_value_type = DefaultValue.disallow

trait_type = MyTraitType()
self.assertEqual(
trait_type.get_default_value(),
(DefaultValue.disallow, Undefined)
)

ctrait = trait_type.as_ctrait()
self.assertEqual(
ctrait.default_value(),
(DefaultValue.disallow, Undefined),
)
self.assertEqual(ctrait.default_kind, "invalid")
self.assertEqual(ctrait.default, Undefined)

with self.assertRaises(ValueError):
ctrait.default_value_for(None, "<dummy>")


class TestDeprecatedTraitTypes(unittest.TestCase):
def test_function_deprecated(self):
Expand Down

0 comments on commit 82658dc

Please sign in to comment.