enthought / enthought/traits

Failed assignment to HasStrictTraits instance modifies the class.

Open
#58 3 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

I encountered the following behaviour while (foolishly) trying to monkeypatch a `HasStrictTraits` subclass. The fact that the monkeypatching doesn't work isn't really surprising. What _is_ suprising is that the attempt to monkeypatch one instance of the class breaks the class itself.

```
>>> from traits.api import *
>>> class A(HasStrictTraits):
... def start(self): print "Starting"
...
>>> try:
... A().start = 3.2 # Monkey-patching fails; too bad.
... except Exception:
... pass
...
>>> A().start # Surprise: the failure above affects new A instances!
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'A' object has no attribute 'start'
```

What appears to be happening is that the assignment to `A().start` creates a new class trait `start` for `A` and puts it into the `A.__class_traits__` dictionary, _before_ the failure occurs. After the failure, that new class trait still exists.

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.