enthought / enthought/traits

Obscure error when attaching extended listener to Property(List)

Open
#1,590 0 comments 0 reactions 0 assignees View on GitHub
component: core type: enhancement
Dominant language
Python
Stars
462
Forks
90
PR merge metrics
No merged PRs in 30d

Description

In the example below, attaching a listener to a `Property(List)` produces an obscure, hard-to-diagnose error:

```python
from traits.api import HasStrictTraits, Instance, List, Property, Str

class Person(HasStrictTraits):
name = Str()

class Classroom(HasStrictTraits):
teachers = List(Instance(Person))
students = List(Instance(Person))
people = Property(List(), observe="teachers.items, students.items")

def _get_people(self):
return self.teachers + self.students

def print_event(object, name, old, new):
print("Event:", object, name, old, new)

classroom = Classroom()
classroom.on_trait_change(print_event, "people.-")
classroom.students.append(Person(name="Nicola"))
```

Results from running the above script on my machine:
```
(traits) mdickinson@mirzakhani traits % python bug.py
Event: <__main__.Classroom object at 0x1014d0450> people [<__main__.Person object at 0x1014e7f40>]
Exception occurred in traits notification handler for object: <__main__.Classroom object at 0x1014d0450>, trait: people, old value: , new value: [<__main__.Person object at 0x1014e7f40>]
Traceback (most recent call last):
File "/Users/mdickinson/Enthought/ETS/traits/traits/trait_notifiers.py", line 583, in _dispatch_change_event
self.dispatch(handler, *args)
File "/Users/mdickinson/Enthought/ETS/traits/traits/trait_notifiers.py", line 486, in dispatch
handler(*args)
File "/Users/mdickinson/Enthought/ETS/traits/traits/traits_listener.py", line 478, in handle_list
for obj in old:
TypeError: '_Undefined' object is not iterable
```

Traits is attaching a list handler to something that can't be handled as a list (a `Property(List)`). It would be good to see if there's a way to catch the issue earlier with a clearer error message.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.