Title: Memoize key isn't always invertible · Issue #313 · grantjenks/python-diskcache · GitHub
Open Graph Title: Memoize key isn't always invertible · Issue #313 · grantjenks/python-diskcache
X Title: Memoize key isn't always invertible · Issue #313 · grantjenks/python-diskcache
Description: Hey, really appreciate this library! I have a small feature request (and an accompanying PR), which is just to make the .__cache_key__ of a memoized function invertible (which also means making args_to_key actually produce unique keys fo...
Open Graph Description: Hey, really appreciate this library! I have a small feature request (and an accompanying PR), which is just to make the .__cache_key__ of a memoized function invertible (which also means making arg...
X Description: Hey, really appreciate this library! I have a small feature request (and an accompanying PR), which is just to make the .__cache_key__ of a memoized function invertible (which also means making arg...
Opengraph URL: https://github.com/grantjenks/python-diskcache/issues/313
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Memoize key isn't always invertible","articleBody":"Hey, really appreciate this library!\r\n\r\nI have a small feature request (and an accompanying PR), which is just to make the `.__cache_key__` of a `memoize`d function invertible (which also means making `args_to_key` actually produce unique keys for different arguments).\r\n\r\nA use case is migrating many different cached functions due to a source changes, e.g. suppose we had some caches that have been populated using:\r\n\r\n```python\r\n@cache.memoize\r\ndef foo(a: OldClass):\r\n ...\r\n\r\n@cache.memoize\r\ndef bar(b: OldClass, c: OldClass | None = None):\r\n ...\r\n```\r\n\r\nand want to roll out new versions of these\r\n\r\n```python\r\n@cache.memoize\r\ndef foo(anew: NewClass):\r\n ...\r\n\r\n@cache.memoize\r\ndef bar(*, bnew: NewClass, c: NewClass | None = None):\r\n ...\r\n```\r\n\r\nand want to update migrate all of `OldClass` arguments to some `NewClass`, as well as changing the signatures appropriately, so that we can keep our old caches, e.g. via something like\r\n\r\n```python\r\nimport diskcache as dc\r\n\r\n...\r\n\r\nfor old_key in old_cache.iterkeys():\r\n # some random function to deal with qualname changes\r\n new_key_base = get_new_qualname(old_key[0])\r\n # some random function to update any values/types and the signature\r\n new_args, new_kwargs = get_new_args_kwargs(old_key)\r\n new_key = dc.core.args_to_key(\r\n new_key_base, new_args, new_kwargs, typed=typed, ignore=ignore\r\n )\r\n new_cache.add(new_key, old_cache.get(old_key))\r\n```\r\n\r\nSince https://github.com/grantjenks/python-diskcache/issues/195 / https://github.com/grantjenks/python-diskcache/commit/d55a50ee083784afa9c85e14e41c4a2d132f3111, then the `args` and `kwargs` delimiter in `args_to_key` is no longer a special sentinel, and so `get_new_args_kwargs` needs to know the signature of whichever function it was previously caching to faithfully find out the arguments used for that key. Namely, there's signatures where we couldn't faithfully determine where the `(None,)` delimiter between `args` and `kwargs` in `args_to_key` is placed (if `typed=False`), e.g. for\r\n\r\n```python\r\n@cache.memoize\r\ndef f(*args, b=None, c=0):\r\n ...\r\n\r\nassert f.__cache_key__(None, \"b\", c=1) == f.__cache_key__(None, b=None, c=1)\r\n```\r\n\r\nMy proposed fix (https://github.com/grantjenks/python-diskcache/pull/312) is just to change the `key` accumulation of `kwargs` to use its (key, value) pairs.\r\n\r\n","author":{"url":"https://github.com/liammcinroy","@type":"Person","name":"liammcinroy"},"datePublished":"2024-04-16T13:57:56.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/313/python-diskcache/issues/313"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:6fab94b2-2087-204a-5c51-6951c730bee6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C5F4:3053B5:DA3567:1236E8E:69698B1A |
| html-safe-nonce | 326e5674163a99d666d591198093a48fa6ba4e83c8bd58a7ab18e37b267e0cf6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNUY0OjMwNTNCNTpEQTM1Njc6MTIzNkU4RTo2OTY5OEIxQSIsInZpc2l0b3JfaWQiOiIzMjE5OTYwMjQwODI3NjMyNDEwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | bb4397771f363d3d9429064f488b6ece1f78935e8ea7ab6920da50f3c6c5b582 |
| hovercard-subject-tag | issue:2246139342 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/grantjenks/python-diskcache/313/issue_layout |
| twitter:image | https://opengraph.githubassets.com/533bfb0cee85e2ae246bb288ab6b78078901f392984d7be3eb41a343e31663b9/grantjenks/python-diskcache/issues/313 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/533bfb0cee85e2ae246bb288ab6b78078901f392984d7be3eb41a343e31663b9/grantjenks/python-diskcache/issues/313 |
| og:image:alt | Hey, really appreciate this library! I have a small feature request (and an accompanying PR), which is just to make the .__cache_key__ of a memoized function invertible (which also means making arg... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | liammcinroy |
| hostname | github.com |
| expected-hostname | github.com |
| None | 533e7cac596c452090972c1150d587fd0b36531b8dc4e8bbfe4ab694aca02408 |
| turbo-cache-control | no-preview |
| go-import | github.com/grantjenks/python-diskcache git https://github.com/grantjenks/python-diskcache.git |
| octolytics-dimension-user_id | 118304 |
| octolytics-dimension-user_login | grantjenks |
| octolytics-dimension-repository_id | 51039722 |
| octolytics-dimension-repository_nwo | grantjenks/python-diskcache |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 51039722 |
| octolytics-dimension-repository_network_root_nwo | grantjenks/python-diskcache |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 63d27af10eea2ccab520b162530cf6c7b739e767 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width