You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: raise TypeError when @cachier is applied to an instance method (#368)
* feat: raise TypeError when @cachier is applied to an instance method (#170)
Applying @cachier to an instance method silently produces wrong results
because 'self' is excluded from the cache key, causing all instances to
share a single cached value. This change raises TypeError at decoration
time by default.
- Add allow_non_static_methods: bool = False to config.Params
- Add allow_non_static_methods parameter to cachier() decorator
- Guard fires in _cachier_decorator after core.set_func(); follows the
existing mongo/redis/sql guard pattern
- @staticmethod is unaffected (self is not its first parameter)
- Opt out per-decorator: allow_non_static_methods=True
- Opt out globally: set_global_params(allow_non_static_methods=True)
- Declare func_is_method: bool = False in _BaseCore.__init__
- Update set_func docstring with the name-based heuristic and cls gap
- Remove unreachable assert max_age is not None dead code (x2)
- Fix test_config.py global state leak (stale_after=60 int pollution)
- Update all existing instance-method tests to use the opt-out flag
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Apply suggestions from code review
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* feat: add async guard and improve handling for instance methods in @cachier
- Raise TypeError for async instance methods without `allow_non_static_methods` opt-in.
- Document `allow_non_static_methods` usage in README and config.
- Generalize hashing utilities to reduce redundancy in test code.
* docs: document `allow_non_static_methods` in README and add test coverage
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy file name to clipboardExpand all lines: README.rst
+5-3Lines changed: 5 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -94,7 +94,7 @@ You can add a default, pickle-based, persistent cache to your function - meaning
94
94
"""Your function now has a persistent cache mapped by argument values!"""
95
95
return {'arg1': arg1, 'arg2': arg2}
96
96
97
-
Class and object methods can also be cached. Cachier will automatically ignore the `self` parameter when determining the cache key for an object method. **This means that methods will be cached across all instances of an object, which may not be what you want.**
97
+
Class and object methods can also be cached. Cachier will automatically ignore the ``self`` parameter when determining the cache key for an object method. **This means that methods will be cached across all instances of an object, which may not be what you want.** Because this is a common source of bugs, ``@cachier`` raises a ``TypeError`` by default when applied to an instance method (a function whose first parameter is named ``self``). This error is raised when ``@cachier`` is applied (at class definition time), not when the method is called. To opt in to cross-instance cache sharing, pass ``allow_non_static_methods=True``.
98
98
99
99
.. code-block:: python
100
100
@@ -107,17 +107,18 @@ Class and object methods can also be cached. Cachier will automatically ignore t
107
107
return arg_1 + arg_2
108
108
109
109
# Instance method does not depend on object's internal state, so good to cache
110
-
@cachier()
110
+
@cachier(allow_non_static_methods=True)
111
111
defgood_usage_1(self, arg_1, arg_2):
112
112
return arg_1 + arg_2
113
113
114
114
# Instance method is calling external service, probably okay to cache
115
-
@cachier()
115
+
@cachier(allow_non_static_methods=True)
116
116
defgood_usage_2(self, arg_1, arg_2):
117
117
result =self.call_api(arg_1, arg_2)
118
118
return result
119
119
120
120
# Instance method relies on object attribute, NOT good to cache
121
+
# @cachier() would raise TypeError here -- this is intentional
121
122
@cachier()
122
123
defbad_usage(self, arg_1, arg_2):
123
124
return arg_1 + arg_2 +self.arg_3
@@ -148,6 +149,7 @@ The following parameters will only be applied to decorators defined after `set_d
148
149
* `pickle_reload`
149
150
* `separate_files`
150
151
* `entry_size_limit`
152
+
* `allow_non_static_methods`
151
153
152
154
These parameters can be changed at any time and they will apply to all decorators:
0 commit comments