Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate SingletonHasTraits & related classes #887

Merged
merged 2 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions docs/source/traits_api_reference/has_traits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ Classes

.. autoclass:: HasPrivateTraits

.. autoclass:: SingletonHasTraits

.. autoclass:: SingletonHasStrictTraits

.. autoclass:: SingletonHasPrivateTraits

.. autoclass:: Vetoable

.. autoclass:: Interface
Expand Down Expand Up @@ -74,3 +68,16 @@ Functions
.. autofunction:: provides

.. autofunction:: weak_arg

Deprecated Classes
------------------

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

.. autoclass:: SingletonHasTraits

.. autoclass:: SingletonHasStrictTraits

.. autoclass:: SingletonHasPrivateTraits

6 changes: 6 additions & 0 deletions traits/has_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -3274,6 +3274,8 @@ class SingletonHasTraits(HasTraits):
""" Singleton class that support trait attributes.
"""

@deprecated("SingletonHasTraits has been deprecated and will be removed "
"in the future. Avoid using it")
def __new__(cls, *args, **traits):
if "_the_instance" not in cls.__dict__:
cls._the_instance = HasTraits.__new__(cls, *args, **traits)
Expand All @@ -3286,6 +3288,8 @@ class SingletonHasStrictTraits(HasStrictTraits):
Non-trait attributes generate an exception.
"""

@deprecated("SingletonHasStrictTraits has been deprecated and will be "
"removed in the future. Avoid using it")
def __new__(cls, *args, **traits):
return SingletonHasTraits.__new__(cls, *args, **traits)

Expand All @@ -3295,6 +3299,8 @@ class SingletonHasPrivateTraits(HasPrivateTraits):
being unchecked.
"""

@deprecated("SingletonHasPrivateTraits has been deprecated and will be "
"removed in the future. Avoid using it")
def __new__(cls, *args, **traits):
return SingletonHasTraits.__new__(cls, *args, **traits)

Expand Down
24 changes: 24 additions & 0 deletions traits/tests/test_has_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
ListenerTraits,
InstanceTraits,
HasTraits,
SingletonHasTraits,
SingletonHasStrictTraits,
SingletonHasPrivateTraits,
)
from traits.ctrait import CTrait
from traits.traits import ForwardProperty, generic_trait
Expand Down Expand Up @@ -321,3 +324,24 @@ def test__trait_set_inited(self):
foo._trait_set_inited()

self.assertTrue(foo.traits_inited())


class TestDeprecatedHasTraits(unittest.TestCase):
def test_deprecated(self):
class TestSingletonHasTraits(SingletonHasTraits):
pass

class TestSingletonHasStrictTraits(SingletonHasStrictTraits):
pass

class TestSingletonHasPrivateTraits(SingletonHasPrivateTraits):
pass

with self.assertWarns(DeprecationWarning):
TestSingletonHasTraits()

with self.assertWarns(DeprecationWarning):
TestSingletonHasStrictTraits()

with self.assertWarns(DeprecationWarning):
TestSingletonHasPrivateTraits()