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

Allow mutable values in Constant trait, add tests #929

Merged
merged 7 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
54 changes: 54 additions & 0 deletions traits/tests/test_constant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# (C) Copyright 2005-2020 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!


import unittest

from traits.api import Constant, HasTraits, TraitError


class TestConstantTrait(unittest.TestCase):

def test_initial_value(self):
class TestClass(HasTraits):
c_atr = Constant(5)

self.assertEqual(5, TestClass().c_atr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, but let's switch the order of arguments on all these self.assertEqual calls: the usual order in Python-land is self.assertEqual(actual, expected) rather than self.assertEqual(expected, actual).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was the other way around, because when I have :
self.assertEqual(4, TestClass().c_atr)
I get the following error message (in PyCharm).

FAILED (failures=1)


5 != 4

Expected :4
Actual   :5
<Click to see difference>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm; interesting. It's not clear cut, and some languages make other decisions, but for Traits, let's be consistent with the existing code and with core Python style (and the examples in the unittest documentation) and use (actual, expected). It makes code harder to read if we use a mix of the two styles.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(If you want some fun historical reading, including Guido arguing with himself, see https://mail.python.org/pipermail/python-dev/2010-December/106875.html)


def test_mutable_initial_value(self):
class TestClass(HasTraits):
c_atr_1 = Constant([1, 2, 3, 4, 5])
c_atr_2 = Constant({"a": 1, "b": 2})

obj = TestClass()

self.assertEqual([1, 2, 3, 4, 5], obj.c_atr_1)
self.assertEqual({"a": 1, "b": 2}, obj.c_atr_2)

def test_assign_fails(self):
class TestClass(HasTraits):
c_atr = Constant(5)

with self.assertRaises(TraitError):
TestClass(c_atr=5)
with self.assertRaises(TraitError):
del TestClass().c_atr

def test_mutate_succeeds(self):
class TestClass(HasTraits):
c_atr_1 = Constant([1, 2, 3, 4, 5])
c_atr_2 = Constant({"a": 1, "b": 2})

obj = TestClass()
obj.c_atr_1.append(6)
obj.c_atr_2["c"] = 3

self.assertEqual([1, 2, 3, 4, 5, 6], obj.c_atr_1)
self.assertEqual({"a": 1, "b": 2, "c": 3}, obj.c_atr_2)
9 changes: 0 additions & 9 deletions traits/trait_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@

# Constants

MutableTypes = (list, dict)
midhun-pm marked this conversation as resolved.
Show resolved Hide resolved
SetTypes = SequenceTypes + (set,)

# Numeric type fast validator definitions
Expand Down Expand Up @@ -1029,14 +1028,6 @@ class Constant(TraitType):
#: The standard metadata for the trait:
metadata = {"type": "constant", "transient": True}

def __init__(self, value, **metadata):
if type(value) in MutableTypes:
midhun-pm marked this conversation as resolved.
Show resolved Hide resolved
raise TraitError(
"Cannot define a constant using a mutable list or dictionary"
)

super(Constant, self).__init__(value, **metadata)


class Delegate(TraitType):
""" A trait type whose value is delegated to a trait on another object.
Expand Down