enthought / enthought/traits

Observe mini-language: use "?" for optional traits

Open
#1,535 1 comment 0 reactions 0 assignees View on GitHub
component: core topic: traits listener rework type: enhancement
Dominant language
Python
Stars
462
Forks
90
PR merge metrics
No merged PRs in 30d

Description

Currently the observe machinery raises an exception when there's no trait matching the given name. For example, in

```python
from traits.api import HasTraits, Instance, observe

class Foo(HasTraits):
pass

class A(HasTraits):
foo = Instance(Foo)

@observe("foo:updated")
def _do_something(self, event):
pass

a = A()
a.foo = Foo()
```

the last line gives the error:
```
ValueError: Trait named 'updated' not found on <__main__.Foo object at 0x108b7cf90>.
```

This is valuable: it catches naming errors in observe mini-language strings. However, it's not always desirable.

Proposal: allow using `observe("foo:updated?")` to match `updated` if the trait exists, but not complain if it doesn't exist. This could be especially useful in combination with `*` in something like `observe("*:updated?")`, if we re-allow use of `*` in a non-terminal position.

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.