-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@cached_var may store wrong values if method has arguments #148
Comments
Hi, could you elaborate which method exactly? And what your output was? Very much appreciated |
This seems like a bug in |
As of 4cf6969, the minimal supported Python version is 3.8, and |
- replace `@cached_var` with `@functools.lru_cache` and `@cached_const` with `@functools.cached_property` - refactor calls of any `cached_property`, now not callable anymore, substitute with `self.set_link_attribute(property)` where necessary - remove `cache_helper` and `cache` attribute in `core.network` - adapt `clear_cache()` methods accordingly, introduce `clear_cache_startswith()` - remove `Network.clear_link_attribute()` - adapt `Network.{del|set}_link_attribute()` accordingly, drop clearing cache of `path_lengths()` in `set_link_attribute()` - related issue: #148 - locally disable pylint false positive `not-callable` on `multiprocess.Pool()`
hi @ntfrgl, I had started getting into this but it turned out to be a bit more work than I had first thought. With fe23f23 I basically substituted the caching in
Any other objections/additions/comments from your side? |
- replace removed `clear_paths_cache()` with new `clear_cache_startswith('path_')` - related: #148
Thank you for making progress on this issue! Your approach mostly follows the recommendations in the official documentation, which suggest that
|
Thanks a lot for all your ideas, hints and corrections! For now, as it will require a good bit of work to implement this accordingly, I will postpone this issue and prioritize the smaller issues. Once those are solved, we can see if we still have time and resources to include a thorough fix of this issue in the release. |
Noticed an Issue related to memoization: Several methods of |
Improve the approach in reverted commits fe23f23, 0166cd2: - introduce `@Cached.method()` based on `@functools.lru_cache()` - track mutable dependencies for cache invalidation - fix cache clearing helper - add behavioural spec in `tests/test_core/test_cache.py` Adjustments in existing classes in this commit: - define `__cache_state__()` for all subclasses of `Cached` (might require updates when adding further methods to the cache) - define `@Cached.method(attrs=(...))` where appropriate - remove obsolete class-specific caching mechanisms - factor out cached worker: `Network.nsi_betweenness()` - define helper: `Network.find_link_attribute()` - define helper: `ClimateNetwork._weighted_metric()` Adding this behaviour to classes without caching should be straightforward. Classes that remain to be adjusted (still have a `clear_cache()` method or subclass `Cached` without conforming to its protocoll): - `ClimateNetwork` and its subclasses: The recursive interaction between `__init__()`, `_similarity_measure` and `_regenerate_network()` across the class hierarchy is very stateful and does not fit into the regular OO dependency pattern assumed by `Cached`. A redesign of this logic would be advisable, but is left for future work. - `RecurrenceNetwork` and its subclasses, and `Surrogate`: Left as an exercise for the reader.
A method decorated with @cached_var whose return value depends on some method arguments will wrongly return the cached results of the first call even for calls with different argument values!
The text was updated successfully, but these errors were encountered: