Skip to content

Let memoize's ignore set match a positional arg by name - #368

Open
afonsojanu wants to merge 1 commit into
grantjenks:masterfrom
afonsojanu:fix/memoize-ignore-matches-positional-by-name
Open

Let memoize's ignore set match a positional arg by name#368
afonsojanu wants to merge 1 commit into
grantjenks:masterfrom
afonsojanu:fix/memoize-ignore-matches-positional-by-name

Conversation

@afonsojanu

Copy link
Copy Markdown

Fixes #240.

ignore={'session'} only filters the argument out of the cache key when session is passed as a keyword. Call the same function with session passed positionally and diskcache builds a key that includes the raw value, so two calls that should be treated the same end up as two different cache entries (or, if the value isn't picklable, a crash). That's the footgun the issue describes: whether ignore actually works depends on how the caller happens to invoke the function, not on what you wrote in the decorator.

This grabs the wrapped function's parameter names once, in memoize's decorator, via inspect.signature, and passes them into args_to_key. A positional argument now gets filtered if either its index or its parameter name is in ignore. Keyword matching already worked and isn't touched.

args_to_key's new arg_names parameter defaults to (), so the two other places that call it directly (DjangoCache.memoize and the memoize_stampede recipe) keep their current index-only behavior. Happy to extend the fix there too if that's wanted, just didn't want to change more than the reported case in one PR.

Test uses an unpicklable value (a threading.Lock) for the ignored argument, so if it ever leaks into the key the test fails with a clear pickling error rather than silently comparing cache-hit counts.

Ran the full test_core.py and test_recipes.py suites locally, both green (88 and 5 passing respectively).

ignore={'session'} only worked when session was passed as a keyword.
Call the same function with session passed positionally instead and
diskcache tried to build a cache key that included the raw argument,
which is exactly the footgun described in grantjenks#240: a caller has to know
whether they always call with keywords, or ignore has to spell out the
positional index too.

memoize() now grabs the wrapped function's parameter names once via
inspect.signature and passes them through to args_to_key, which checks
a positional argument's name against ignore in addition to its index.
Keyword matching was already fine and is untouched. args_to_key's new
arg_names parameter defaults to an empty tuple, so the two other
callers (DjangoCache.memoize, the memoize_stampede recipe) keep their
existing index-only behavior unless they're updated separately.

Added a regression test using an unpicklable value for the ignored
argument, so a caller passing it positionally raises immediately if it
leaks into the key instead of silently caching under a wrong key.
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.

memoization: with named parameter but positional argument, memoization ignore={} doesn't apply

1 participant