enthought / enthought/traits

Cache invalidation for cached properties doesn't always occur early enough

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

Description

This arose from a discussion in internal chat. Given a cached property `y` depending on `x`, you might expect that whenever `x` and `y` are observed, they should be consistent. But that's not always true in practice.

It's not clear to me whether this qualifies as a bug, as a documentation issue, or as a "well, don't do that then" issue. If we decide it's a bug, it's not likely to be an easy fix.

With the following code:

```python
from traits.api import HasTraits, Int, Property, cached_property
from traits.api import Instance, on_trait_change

class Foo(HasTraits):

x = Int(5)

y = Property(Int, observe='x')

@on_trait_change('x')
def _report_changes(self):
print("x is: ", self.x)
print("y is: ", self.y)

@cached_property
def _get_y(self):
return 2 * self.x

foo = Foo()
foo.y
foo.x = 7
```

I get:
```
mdickinson@mirzakhani traits % python ~/Desktop/test.py
x is: 7
y is: 10
```

The naive expectation would be that whenever both `x` and `y` are observed together, the value of `y` is twice that of `x`.

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.