Skip to content

feat: raise TypeError when @cachier is applied to an instance method#368

Merged
shaypal5 merged 7 commits intomasterfrom
feat/170
Mar 20, 2026
Merged

feat: raise TypeError when @cachier is applied to an instance method#368
shaypal5 merged 7 commits intomasterfrom
feat/170

Conversation

@Borda
Copy link
Contributor

@Borda Borda commented Mar 16, 2026

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

Resolves #170

…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>
@Borda Borda requested a review from shaypal5 as a code owner March 16, 2026 10:01
Copilot AI review requested due to automatic review settings March 16, 2026 10:01
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 (default False) in global Params and as an optional @cachier(...) argument; raises TypeError at 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
Copy link

codecov bot commented Mar 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.94%. Comparing base (5c4f121) to head (7b33fc3).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@           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           
Flag Coverage Δ
local 59.30% <100.00%> (+0.09%) ⬆️
mongodb 40.69% <66.66%> (+0.13%) ⬆️
postgres 43.40% <33.33%> (-0.05%) ⬇️
redis 46.80% <66.66%> (+0.12%) ⬆️
s3 42.47% <33.33%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/cachier/config.py 100.00% <100.00%> (ø)
src/cachier/core.py 100.00% <100.00%> (ø)
src/cachier/cores/base.py 100.00% <100.00%> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5c4f121...7b33fc3. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Borda and others added 2 commits March 16, 2026 16:09
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Borda added 3 commits March 16, 2026 16:23
…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.
Copy link
Member

@shaypal5 shaypal5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good change. It's a good change!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

weak assumptions on class methods

3 participants