Skip to content

Commit

Permalink
Fix error message for a list length violation (#1173)
Browse files Browse the repository at this point in the history
* Fix error message for a list length violation

* Update changelog entry

(cherry picked from commit 41fad6a)
  • Loading branch information
mdickinson committed Jun 2, 2020
1 parent 4c66b08 commit 8e706d4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Features
* Add ``Map`` and ``PrefixMap`` trait types. (#886, #953, #956, #970, #1139)
* Add ``TraitList`` as the base list object that can perform validation
and emit change notifications. (#912, #981, #984, #989, #999, #1003, #1011,
#1026, #1009, #1040)
#1026, #1009, #1040, #1173)
* Add ``TraitDict`` as the base dict object that can perform validation and
emit change notifications. (#913)
* Add ``TraitSet`` as the base set object that can perform validation and
Expand Down
12 changes: 12 additions & 0 deletions traits/tests/test_trait_list_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,18 @@ def test_remove_too_small(self):
foo.at_least_two.remove(10)
self.assertEqual(foo.at_least_two, [1, 2])

def test_length_violation_error_message(self):
# Regression test for enthought/traits#1170
foo = HasLengthConstrainedLists(at_least_two=[1, 2])
with self.assertRaises(TraitError) as exc_cm:
foo.at_least_two.remove(1)

exc_message = str(exc_cm.exception)
self.assertIn("'at_least_two' trait", exc_message)
self.assertIn("HasLengthConstrainedLists instance", exc_message)
self.assertIn("an integer", exc_message)
self.assertIn("at least 2 items", exc_message)

def test_dead_object_reference(self):
foo = HasLengthConstrainedLists(at_most_five=[1, 2, 3, 4])
list_object = foo.at_most_five
Expand Down
4 changes: 2 additions & 2 deletions traits/trait_list_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,8 +889,8 @@ def _validate_length(self, new_length):
"but you attempted to change its length to %d %s."
% (
self.name,
class_of(object),
self.trait.full_info(object, self.name, Undefined),
class_of(self.object()),
self.trait.full_info(self.object(), self.name, Undefined),
new_length,
"element" if new_length == 1 else "elements",
)
Expand Down

0 comments on commit 8e706d4

Please sign in to comment.