diff --git a/docs/source/traits_user_manual/defining.rst b/docs/source/traits_user_manual/defining.rst index e259e97b4..9c923acca 100644 --- a/docs/source/traits_user_manual/defining.rst +++ b/docs/source/traits_user_manual/defining.rst @@ -307,11 +307,11 @@ the table. | Constant | Constant(*value*\ [, \*\*\ *metadata*]) | +------------------+----------------------------------------------------------+ | Date | Date(*value*\ [, *default_value* = None, | -| | *allow_datetime* = None, *allow_none* = None, | +| | *allow_datetime* = False, *allow_none* = False, | | | \*\*\ *metadata*]) | +------------------+----------------------------------------------------------+ | Datetime | Datetime(*value*\ [, *default_value* = None, | -| | *allow_none* = None, \*\*\ *metadata*]) | +| | *allow_none* = False, \*\*\ *metadata*]) | +------------------+----------------------------------------------------------+ | Dict | Dict([*key_trait* = None, *value_trait* = None, | | | *value* = None, *items* = True, \*\*\ *metadata*]) | @@ -394,7 +394,7 @@ the table. | This | n/a | +------------------+----------------------------------------------------------+ | Time | Time(*value*\ [, *default_value* = None, | -| | *allow_none* = None, \*\*\ *metadata*]) | +| | *allow_none* = False, \*\*\ *metadata*]) | +------------------+----------------------------------------------------------+ | ToolbarButton | ToolbarButton([*label* = '', *image* = None, *style* = | | | 'toolbar', *orientation* = 'vertical', *width_padding* = | diff --git a/traits/tests/test_date.py b/traits/tests/test_date.py index 604cb56a8..a1aa731e6 100644 --- a/traits/tests/test_date.py +++ b/traits/tests/test_date.py @@ -71,28 +71,22 @@ def test_assign_non_date(self): with self.assertRaises(TraitError) as exception_context: obj.simple_date = "1975-2-13" message = str(exception_context.exception) - self.assertIn("must be a date or None, but", message) + self.assertIn("must be a non-datetime date, but", message) def test_assign_none_with_allow_none_not_given(self): obj = HasDateTraits(simple_date=UNIX_EPOCH) - self.assertIsNotNone(obj.simple_date) - with self.assertWarns(DeprecationWarning) as warnings_cm: + with self.assertRaises(TraitError) as exception_context: obj.simple_date = None - self.assertIsNone(obj.simple_date) - - _, _, this_module = __name__.rpartition(".") - self.assertIn(this_module, warnings_cm.filename) - self.assertIn( - "None will no longer be accepted", - str(warnings_cm.warning), - ) + self.assertEqual(obj.simple_date, UNIX_EPOCH) + message = str(exception_context.exception) + self.assertIn("must be a non-datetime date, but", message) def test_assign_none_with_allow_none_false(self): obj = HasDateTraits(none_prohibited=UNIX_EPOCH) with self.assertRaises(TraitError) as exception_context: obj.none_prohibited = None message = str(exception_context.exception) - self.assertIn("must be a date, but", message) + self.assertIn("must be a non-datetime date, but", message) def test_assign_none_with_allow_none_true(self): obj = HasDateTraits(none_allowed=UNIX_EPOCH) @@ -106,7 +100,7 @@ def test_assign_datetime_with_allow_datetime_false(self): with self.assertRaises(TraitError) as exception_context: obj.datetime_prohibited = test_datetime message = str(exception_context.exception) - self.assertIn("must be a non-datetime date or None, but", message) + self.assertIn("must be a non-datetime date, but", message) def test_assign_datetime_with_allow_datetime_true(self): test_datetime = datetime.datetime(1975, 2, 13) @@ -117,21 +111,14 @@ def test_assign_datetime_with_allow_datetime_true(self): def test_assign_datetime_with_allow_datetime_not_given(self): # For traits where "allow_datetime" is not specified, a # DeprecationWarning should be issued on assignment of datetime. + test_date = datetime.date(2023, 1, 11) test_datetime = datetime.datetime(1975, 2, 13) - obj = HasDateTraits() - with self.assertWarns(DeprecationWarning) as warnings_cm: - # Note: the warning depends on the type, not the value; this case - # should warn even though the time component of the datetime is - # zero. + obj = HasDateTraits(simple_date=test_date) + with self.assertRaises(TraitError) as exception_context: obj.simple_date = test_datetime - self.assertEqual(obj.simple_date, test_datetime) - - _, _, this_module = __name__.rpartition(".") - self.assertIn(this_module, warnings_cm.filename) - self.assertIn( - "datetime instances will no longer be accepted", - str(warnings_cm.warning), - ) + self.assertEqual(obj.simple_date, test_date) + message = str(exception_context.exception) + self.assertIn("must be a non-datetime date, but", message) def test_allow_none_false_allow_datetime_false(self): obj = HasDateTraits(strict=UNIX_EPOCH) diff --git a/traits/tests/test_datetime.py b/traits/tests/test_datetime.py index e712b715a..75001cc67 100644 --- a/traits/tests/test_datetime.py +++ b/traits/tests/test_datetime.py @@ -63,34 +63,29 @@ def test_assign_non_datetime(self): with self.assertRaises(TraitError) as exception_context: obj.simple_datetime = "2021-02-05 12:00:00" message = str(exception_context.exception) - self.assertIn("must be a datetime or None, but", message) + self.assertIn("must be a datetime, but", message) def test_assign_date(self): obj = HasDatetimeTraits() with self.assertRaises(TraitError) as exception_context: obj.simple_datetime = datetime.date(1975, 2, 13) message = str(exception_context.exception) - self.assertIn("must be a datetime or None, but", message) + self.assertIn("must be a datetime, but", message) self.assertIsNone(obj.simple_datetime) def test_assign_none_with_allow_none_not_given(self): obj = HasDatetimeTraits(simple_datetime=UNIX_EPOCH) - self.assertIsNotNone(obj.simple_datetime) - with self.assertWarns(DeprecationWarning) as warnings_cm: + with self.assertRaises(TraitError) as exception_context: obj.simple_datetime = None - self.assertIsNone(obj.simple_datetime) - - _, _, this_module = __name__.rpartition(".") - self.assertIn(this_module, warnings_cm.filename) - self.assertIn( - "None will no longer be accepted", - str(warnings_cm.warning), - ) + self.assertEqual(obj.simple_datetime, UNIX_EPOCH) + message = str(exception_context.exception) + self.assertIn("must be a datetime, but", message) def test_assign_none_with_allow_none_false(self): obj = HasDatetimeTraits(none_prohibited=UNIX_EPOCH) with self.assertRaises(TraitError) as exception_context: obj.none_prohibited = None + self.assertEqual(obj.none_prohibited, UNIX_EPOCH) message = str(exception_context.exception) self.assertIn("must be a datetime, but", message) diff --git a/traits/tests/test_time.py b/traits/tests/test_time.py index 624a2339c..fba69508b 100644 --- a/traits/tests/test_time.py +++ b/traits/tests/test_time.py @@ -63,34 +63,29 @@ def test_assign_non_time(self): with self.assertRaises(TraitError) as exception_context: obj.simple_time = "12:00:00" message = str(exception_context.exception) - self.assertIn("must be a time or None, but", message) + self.assertIn("must be a time, but", message) def test_assign_datetime(self): obj = HasTimeTraits() with self.assertRaises(TraitError) as exception_context: obj.simple_time = datetime.datetime(1975, 2, 13) message = str(exception_context.exception) - self.assertIn("must be a time or None, but", message) + self.assertIn("must be a time, but", message) self.assertIsNone(obj.simple_time) def test_assign_none_with_allow_none_not_given(self): obj = HasTimeTraits(simple_time=UNIX_EPOCH) - self.assertIsNotNone(obj.simple_time) - with self.assertWarns(DeprecationWarning) as warnings_cm: + with self.assertRaises(TraitError) as exception_context: obj.simple_time = None - self.assertIsNone(obj.simple_time) - - _, _, this_module = __name__.rpartition(".") - self.assertIn(this_module, warnings_cm.filename) - self.assertIn( - "None will no longer be accepted", - str(warnings_cm.warning), - ) + self.assertEqual(obj.simple_time, UNIX_EPOCH) + message = str(exception_context.exception) + self.assertIn("must be a time, but", message) def test_assign_none_with_allow_none_false(self): obj = HasTimeTraits(none_prohibited=UNIX_EPOCH) with self.assertRaises(TraitError) as exception_context: obj.none_prohibited = None + self.assertEqual(obj.none_prohibited, UNIX_EPOCH) message = str(exception_context.exception) self.assertIn("must be a time, but", message) diff --git a/traits/trait_types.py b/traits/trait_types.py index 26e26fe25..e981fccdd 100644 --- a/traits/trait_types.py +++ b/traits/trait_types.py @@ -4498,18 +4498,18 @@ class Date(TraitType): """ A trait type whose value must be a date. The value must be an instance of :class:`datetime.date`. Note that - :class:`datetime.datetime` is a subclass of :class:`datetime.date`, so - by default instances of :class:`datetime.datetime` are also permitted. - Use ``Date(allow_datetime=False)`` to exclude this possibility. + :class:`datetime.datetime` is a subclass of :class:`datetime.date`, but by + default instances of :class:`datetime.datetime` are not permitted. Use + ``Date(allow_datetime=True)`` to allow :class:`datetime.datetime` instances + to be assigned to a ``Date`` trait. - .. deprecated:: 6.3.0 - In the future, :class:`datetime.datetime` instances will not be valid - values for this trait type unless "allow_datetime=True" is explicitly - given. + .. versionchanged:: 7.0.0 + :class:`datetime.datetime` instances are no longer valid values for + this trait type unless "allow_datetime=True" is explicitly given. - .. deprecated:: 6.3.0 - In the future, ``None`` will not be a valid value for this trait type - unless "allow_none=True" is explicitly given. + .. versionchanged:: 7.0.0 + ``None`` is no longer a valid value for this trait type unless + "allow_none=True" is explicitly given. Parameters ---------- @@ -4517,18 +4517,13 @@ class Date(TraitType): The default value for this trait. If no default is provided, the default is ``None``. allow_datetime : bool, optional - If ``False``, instances of ``datetime.datetime`` are not valid - values for this Trait. If ``True``, ``datetime.datetime`` instances - are explicitly permitted. If this argument is not given, - ``datetime.datetime`` instances will be accepted, but a - ``DeprecationWarning`` will be issued; in some future version of - Traits, ``datetime.datetime`` instances will not be permitted. + If ``False``, instances of ``datetime.datetime`` are not considered + valid values for this Trait. If ``True``, ``datetime.datetime`` + instances are permitted. The default is ``False``. allow_none : bool, optional If ``False``, it's not permitted to assign ``None`` to this trait. - If ``True``, ``None`` instances are permitted. If this argument is - not given, ``None`` instances will be accepted but a - ``DeprecationWarning`` will be issued; in some future verison of - Traits, ``None`` may no longer be permitted. + If ``True``, ``None`` instances are permitted. The default is + ``False``. **metadata: dict Additional metadata. """ @@ -4540,8 +4535,8 @@ def __init__( self, default_value=None, *, - allow_datetime=None, - allow_none=None, + allow_datetime=False, + allow_none=False, **metadata, ): super().__init__(default_value, **metadata) @@ -4554,34 +4549,10 @@ def validate(self, object, name, value): if value is None: if self.allow_none: return value - elif self.allow_none is None: - warnings.warn( - ( - "In the future, None will no longer be accepted by " - "this trait type. To allow None and silence this " - "warning, use Date(allow_none=True)." - ), - DeprecationWarning, - stacklevel=2, - ) - return value elif isinstance(value, datetime.datetime): if self.allow_datetime: return value - elif self.allow_datetime is None: - warnings.warn( - ( - "In the future, datetime.datetime instances will no " - "longer be accepted by this trait type. To accept " - "datetimes and silence this warning, use " - "Date(allow_datetime=True) or " - "Union(Datetime(), Date())." - ), - DeprecationWarning, - stacklevel=2, - ) - return value elif isinstance(value, datetime.date): return value @@ -4592,12 +4563,12 @@ def info(self): """ Return text description of this trait. """ - if self.allow_datetime or self.allow_datetime is None: + if self.allow_datetime: datetime_qualifier = "" else: datetime_qualifier = " non-datetime" - if self.allow_none or self.allow_none is None: + if self.allow_none: none_qualifier = " or None" else: none_qualifier = "" @@ -4615,9 +4586,9 @@ class Datetime(TraitType): The value must be an instance of :class:`datetime.datetime`. - .. deprecated:: 6.3.0 - In the future, ``None`` will not be a valid value for this trait type - unless "allow_none=True" is explicitly given. + .. versionchanged:: 7.0.0 + ``None`` is no longer a valid value for this trait type unless + "allow_none=True" is explicitly given. Parameters ---------- @@ -4626,10 +4597,8 @@ class Datetime(TraitType): default is ``None``. allow_none : bool, optional If ``False``, it's not permitted to assign ``None`` to this trait. - If ``True``, ``None`` instances are permitted. If this argument is - not given, ``None`` instances will be accepted but a - ``DeprecationWarning`` will be issued; in some future verison of - Traits, ``None`` may no longer be permitted. + If ``True``, ``None`` instances are permitted. The default is + ``False``. **metadata: dict Additional metadata. """ @@ -4641,7 +4610,7 @@ def __init__( self, default_value=None, *, - allow_none=None, + allow_none=False, **metadata, ): super().__init__(default_value, **metadata) @@ -4653,17 +4622,6 @@ def validate(self, object, name, value): if value is None: if self.allow_none: return value - elif self.allow_none is None: - warnings.warn( - ( - "In the future, None will no longer be accepted by " - "this trait type. To allow None and silence this " - "warning, use Datetime(allow_none=True)." - ), - DeprecationWarning, - stacklevel=2, - ) - return value elif isinstance(value, datetime.datetime): return value @@ -4674,7 +4632,7 @@ def info(self): """ Return text description of this trait. """ - if self.allow_none or self.allow_none is None: + if self.allow_none: none_qualifier = " or None" else: none_qualifier = "" @@ -4692,9 +4650,9 @@ class Time(TraitType): The value must be an instance of :class:`datetime.time`. - .. deprecated:: 6.3.0 - In the future, ``None`` will not be a valid value for this trait type - unless "allow_none=True" is explicitly given. + .. versionchanged:: 7.0.0 + ``None`` is no longer a valid value for this trait type unless + "allow_none=True" is explicitly given. Parameters ---------- @@ -4703,10 +4661,8 @@ class Time(TraitType): default is ``None``. allow_none : bool, optional If ``False``, it's not permitted to assign ``None`` to this trait. - If ``True``, ``None`` instances are permitted. If this argument is - not given, ``None`` instances will be accepted but a - ``DeprecationWarning`` will be issued; in some future verison of - Traits, ``None`` may no longer be permitted. + If ``True``, ``None`` instances are permitted. The default is + ``False``. **metadata: dict Additional metadata. """ @@ -4718,7 +4674,7 @@ def __init__( self, default_value=None, *, - allow_none=None, + allow_none=False, **metadata, ): super().__init__(default_value, **metadata) @@ -4730,17 +4686,6 @@ def validate(self, object, name, value): if value is None: if self.allow_none: return value - elif self.allow_none is None: - warnings.warn( - ( - "In the future, None will no longer be accepted by " - "this trait type. To allow None and silence this " - "warning, use Time(allow_none=True)." - ), - DeprecationWarning, - stacklevel=2, - ) - return value elif isinstance(value, datetime.time): return value @@ -4751,7 +4696,7 @@ def info(self): """ Return text description of this trait. """ - if self.allow_none or self.allow_none is None: + if self.allow_none: none_qualifier = " or None" else: none_qualifier = ""