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.

classification
Title: bounded _lru_cache_wrapprer behaves as if typed=True when it wasn't
Type: Stage: resolved
Components: Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: bup, rhettinger
Priority: normal Keywords:

Created on 2021-03-08 17:12 by bup, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg388271 - (view) Author: Dan Snider (bup) * Date: 2021-03-08 17:12
Isn't the point of setting typed=True to make it so that e.g. True doesn't register as a hit when there is already a cache entry for 1.0? Assuming that is the case, although this report specifically targets 3.8 I found no indication that what I believe is the cause of this has been fixed in the interim.

def test():
    from functools import lru_cache
    class No1:
        __eq__ = 0 .__eq__
        __hash__ = 0 .__hash__
    class No2:
        __eq__ = (0,).__contains__
        def __hash__(self, /): return hash(0)
    @lru_cache(256, typed=False)
    def test(v): return [v]
    test(No1()), test(No1()), test(0.0), test(0)
    
    print(test.cache_info())
    @lru_cache(256, typed=False)
    def test(v): return [v]
    test(No2()), test(No2()), test(0.0), test(0)
    print(test.cache_info())


    CacheInfo(hits=0, misses=4, maxsize=256, currsize=4)
CacheInfo(hits=2, misses=2, maxsize=256, currsize=2)
msg388285 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-03-08 19:10
Thanks for the report, but this isn't a bug.

Setting typed=True guarantees that types are treated as distinct entries.

Setting typed=False means that the implementation MAY ignore the type and only use equality, but it is not required to do so.   If it is convenient for the implementation, it may use type to save space.  Currently int and str are treated as distinct from other types that may be equal to them.

Likewise, the implementation is allowed to treat different argument patterns as distinct even if they are equal.  Currently, we treat pow(2, 5) as distinct from pow(base=2, exp=5) which is itself distinct from pow(exp=5, base=2).
History
Date User Action Args
2022-04-11 14:59:42adminsetgithub: 87602
2021-03-08 19:10:18rhettingersetstatus: open -> closed
messages: + msg388285

assignee: rhettinger
resolution: not a bug
stage: resolved
2021-03-08 17:25:33xtreaksetnosy: + rhettinger
2021-03-08 17:12:28bupcreate