enthought / enthought/traits

Acessing a non existing instance attribute create a trait instance in the class

Open
#358 7 comments 0 reactions 0 assignees View on GitHub
type: bug
Dominant language
Python
Stars
462
Forks
90
PR merge metrics
No merged PRs in 30d

Description

The following code creates two instances of `HasTrait` in `an_instance` and `another_instance` (problem is the same with a derived class in a real case). Then, accessing `an_instance.i_do_not_exists` creates a new trait in the class that is accessible via `another_instance`. Any attempt to read a non existing instance attribute creates a new item in the class `__dict__`.

Moreover, a subsequent use of `add_trait` to create a trait named `i_do_not_exists` in any instance does not add it to the list of traits returned by `traits()`.

```
from traits.api import HasTraits, String

an_instance = HasTraits()
another_instance = HasTraits()

try:
an_instance.i_do_not_exist
except AttributeError:
pass

print another_instance.trait('i_do_not_exist')
# ->

another_instance.add_trait('i_do_not_exist', String)
print another_instance.traits().keys()
# -> ['trait_added', 'trait_modified']
```

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.