This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: support maxsize argument for lru_cache's user_function option
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List:, eric.smith, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2020-03-30 11:37 by, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 19226 closed, 2020-03-30 11:38
Messages (8)
msg365304 - (view) Author: brendon zhang ( * Date: 2020-03-30 11:52
Existing implementation of lru_cache user_function option supports typed_code argument, but does not support maxsize argument, because when we pass in the user function as a positional argument, it gets assigned to the maxsize parameter, and therefore there is no way to pass the actual maxsize value.

The PR implementation supports both signatures
lru_cache(maxsize=128, typed=False)
lru_cache(user_function, maxsize=128, typed=False)
msg365305 - (view) Author: brendon zhang ( * Date: 2020-03-30 11:57
Two concerns is

1. the method signature (inspect.signature(lru_cache)) is not (maxsize=128, typed=False) anymore, and so it is poor documentation.
2. not good reuse of TypeErrors related to incorrect number of inputs / type of inputs.
msg365314 - (view) Author: brendon zhang ( * Date: 2020-03-30 13:42
ignore concern #1. I used __text_signature__ attribute to reset the inspect signature to something useful
msg365339 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-03-30 18:51
Since the current signature allows *maxsize* and *typed* to be either positional or keyword arguments, we couldn't do this cleanly the way dataclasses did.  

The code in the PR is valiant attempt but is way too tricky for my tastes -- we would likely regret going down this path.
msg365342 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-03-30 19:02
You can use lru_cache(maxsize=128, typed=False)(user_function). lru_cache should support a function as an argument on if it is a single positional argument, this is the purpose. So you can write

def func(...):

instead of

def func(...):

When you pass maxsize and typed you do not pass a function as an argument of lru_cache, you pass it as an argument of the decorator created by the lru_cache() call.

@lru_cache(maxsize=128, typed=False)
def func(...):
msg365363 - (view) Author: brendon zhang ( * Date: 2020-03-31 02:25
"lru_cache(maxsize=128, typed=False)(user_function)"

oh ok! I overlooked that you could do this. There isn't really a need then for supporting user_function w/ the maxsize argument in single call.

"The code in the PR is valiant attempt but is way too tricky for my tastes"

Yeah. I think only after looking back at the code after writing it I see it is too convoluted. I guess the code can remain as some artifact to show why not to down this path (unless some convenient way to implement it is introduced in the future).
msg365364 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-03-31 02:28
Thanks for putting some thought into this.
msg365365 - (view) Author: brendon zhang ( * Date: 2020-03-31 02:29
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84295
2020-03-31 02:29:32brendon-zhang@hotmail.comsetmessages: + msg365365
2020-03-31 02:28:40rhettingersetmessages: + msg365364
2020-03-31 02:27:52brendon-zhang@hotmail.comsetstatus: open -> closed
resolution: rejected
stage: patch review -> resolved
2020-03-31 02:25:45brendon-zhang@hotmail.comsetmessages: + msg365363
2020-03-30 19:02:36serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg365342
2020-03-30 18:51:09rhettingersetnosy: + eric.smith
messages: + msg365339
2020-03-30 17:36:23rhettingersetassignee: rhettinger
2020-03-30 13:42:56brendon-zhang@hotmail.comsetmessages: + msg365314
2020-03-30 11:57:05brendon-zhang@hotmail.comsetmessages: + msg365305
2020-03-30 11:52:54brendon-zhang@hotmail.comsetmessages: + msg365304
2020-03-30 11:38:50brendon-zhang@hotmail.comsetkeywords: + patch
stage: patch review
pull_requests: + pull_request18588
2020-03-30 11:37:25brendon-zhang@hotmail.comcreate