Conversation
…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>
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Pull request overview
Adds a safety guard to prevent accidental cross-instance cache sharing by default when @cachier is applied to instance methods (where the first parameter is named self), while providing explicit per-decorator and global opt-outs.
Changes:
- Introduces
allow_non_static_methods(defaultFalse) in globalParamsand as an optional@cachier(...)argument; raisesTypeErrorat decoration time for instance methods unless opted in. - Tracks whether the decorated callable is an instance method via
_BaseCore.func_is_method, based on a signature/name heuristic. - Updates docs and tests to reflect the new default behavior, and fixes a global-state leak in
tests/test_config.py.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_smoke.py | Adds smoke coverage for classmethod behavior and instance-method guard behavior. |
| tests/test_general.py | Updates method-decorated tests to opt in to non-static methods. |
| tests/test_config.py | Restores global params after deprecation-warning test to prevent state pollution. |
| tests/test_caching_regression.py | Updates regression scenario to opt in for instance method decoration. |
| tests/test_async_core.py | Updates async method tests to opt in and clarifies intent via comments. |
| tests/redis_tests/test_async_redis_core.py | Opts in for async redis instance-method tests. |
| tests/mongo_tests/test_async_mongo_core.py | Opts in for async mongo instance-method tests. |
| src/cachier/cores/base.py | Adds func_is_method flag and documents/implements name-based detection in set_func. |
| src/cachier/core.py | Adds allow_non_static_methods arg + decoration-time guard; removes unreachable asserts. |
| src/cachier/config.py | Adds allow_non_static_methods to Params and documents it as decoration-time. |
| README.rst | Documents the new default TypeError and the opt-in flag for instance methods. |
You can also share your feedback on Copilot code review. Take the survey.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #368 +/- ##
=======================================
Coverage 99.94% 99.94%
=======================================
Files 12 12
Lines 1731 1735 +4
Branches 216 218 +2
=======================================
+ Hits 1730 1734 +4
Partials 1 1
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
for more information, see https://pre-commit.ci
…achier - 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.
shaypal5
left a comment
There was a problem hiding this comment.
It's a good change. It's a good change!
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.
Resolves #170