diff --git a/CHANGES.rst b/CHANGES.rst index 45da96a44..d5fb9b8d1 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,14 @@ Traits CHANGELOG ================ +Upcoming release X.Y.Z +---------------------- + +Removals +~~~~~~~~ +* Make ``NoDefaultSpecified`` private and remove it from the public API.(#1380) + + Release 6.1.1 ------------- diff --git a/docs/source/traits_api_reference/trait_type.rst b/docs/source/traits_api_reference/trait_type.rst index ac04c8134..73b55cf5b 100644 --- a/docs/source/traits_api_reference/trait_type.rst +++ b/docs/source/traits_api_reference/trait_type.rst @@ -7,8 +7,6 @@ Classes ------- -.. autoclass:: NoDefaultSpecified - .. autoclass:: TraitType Private Functions diff --git a/traits-stubs/traits-stubs/trait_type.pyi b/traits-stubs/traits-stubs/trait_type.pyi index b5ae8b312..ec9a5201b 100644 --- a/traits-stubs/traits-stubs/trait_type.pyi +++ b/traits-stubs/traits-stubs/trait_type.pyi @@ -14,11 +14,6 @@ from .base_trait_handler import BaseTraitHandler as BaseTraitHandler trait_types: Dict[str, int] - -class NoDefaultSpecified: - ... - - _Accepts = TypeVar('_Accepts') _Stores = TypeVar('_Stores') diff --git a/traits/tests/test_readonly.py b/traits/tests/test_readonly.py new file mode 100644 index 000000000..aa5dd4103 --- /dev/null +++ b/traits/tests/test_readonly.py @@ -0,0 +1,48 @@ +# (C) Copyright 2005-2021 Enthought, Inc., Austin, TX +# All rights reserved. +# +# This software is provided without warranty under the terms of the BSD +# license included in LICENSE.txt and may be redistributed only under +# the conditions described in the aforementioned license. The license +# is also available online at http://www.enthought.com/licenses/BSD.txt +# +# Thanks for using Enthought open source! + +""" +Tests for the ReadOnly trait type. + +""" +import unittest + +from traits.api import ( + HasTraits, + ReadOnly, + TraitError, +) + + +class ObjectWithReadOnlyText(HasTraits): + """ A dummy object that set the readonly trait in __init__ + + There exists such usage in TraitsUI. + """ + + text = ReadOnly() + + def __init__(self, text, **traits): + self.text = text + super(ObjectWithReadOnlyText, self).__init__(**traits) + + +class TestReadOnlyTrait(unittest.TestCase): + """ Test ReadOnly TraitType. """ + + def test_set_readonly_trait_in_init(self): + + obj = ObjectWithReadOnlyText(text="ABC") + self.assertEqual(obj.text, "ABC") + + with self.assertRaises(TraitError): + obj.text = "XYZ" + + self.assertEqual(obj.text, "ABC") diff --git a/traits/trait_type.py b/traits/trait_type.py index 88403d3ee..7757590ab 100644 --- a/traits/trait_type.py +++ b/traits/trait_type.py @@ -69,11 +69,11 @@ def _read_only(object, name, value): # Create a singleton object for use in the TraitType constructor: -class NoDefaultSpecified(object): +class _NoDefaultSpecified(object): pass -no_default_specified = NoDefaultSpecified() +_no_default_specified = _NoDefaultSpecified() class TraitType(BaseTraitHandler): @@ -173,7 +173,7 @@ class TraitType(BaseTraitHandler): #: The metadata for the trait. metadata = {} - def __init__(self, default_value=no_default_specified, **metadata): + def __init__(self, default_value=_no_default_specified, **metadata): """ TraitType initializer This is the only method normally called directly by client code. @@ -183,7 +183,7 @@ def __init__(self, default_value=no_default_specified, **metadata): Override this method whenever a different method signature or a validated default value is needed. """ - if default_value is not no_default_specified: + if default_value is not _no_default_specified: self.default_value = default_value if len(metadata) > 0: @@ -257,7 +257,7 @@ def get_default_value(self): return (dvt, dv) - def clone(self, default_value=no_default_specified, **metadata): + def clone(self, default_value=_no_default_specified, **metadata): """ Copy, optionally modifying default value and metadata. Clones the contents of this object into a new instance of the same @@ -294,7 +294,7 @@ def clone(self, default_value=no_default_specified, **metadata): new._metadata.update(metadata) - if default_value is not no_default_specified: + if default_value is not _no_default_specified: new.default_value = default_value if self.validate is not None: try: