Skip to content

Commit

Permalink
PR changes & raise DeprecationWarning on instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
midhun-pm committed Feb 18, 2020
1 parent 26dfe84 commit b0c1b98
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/source/traits_api_reference/has_traits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Functions
Deprecated Classes
------------------

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

.. autoclass:: SingletonHasTraits
Expand Down
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()

0 comments on commit b0c1b98

Please sign in to comment.