[New-bugs-announce] [issue44992] functools.lru_cache does not consider strings and numpy strings as equivalent

Brian Lee report at bugs.python.org
Tue Aug 24 09:36:59 EDT 2021


New submission from Brian Lee <brian.kihoon.lee at gmail.com>:

This seems like unexpected behavior: Two keys that are equal and have equal hashes should yield cache hits, but they do not.


Python 3.9.6 (default, Aug 18 2021, 19:38:01) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import functools
>>> 
>>> import numpy as np
>>> 
>>> @functools.lru_cache(maxsize=None)
... def f(x):
...   return x
... 
>>> py_str = 'hello world'
>>> np_str = np.str_(py_str)
>>> 
>>> assert py_str == np_str
>>> assert hash(py_str) == hash(np_str)
>>> 
>>> assert f.cache_info().currsize == 0
>>> f(py_str)
'hello world'
>>> assert f.cache_info().currsize == 1
>>> f(np_str)
'hello world'
>>> assert f.cache_info().currsize == 2
>>> print(f.cache_info())
CacheInfo(hits=0, misses=2, maxsize=None, currsize=2)

----------
components: Library (Lib)
messages: 400209
nosy: brilee
priority: normal
severity: normal
status: open
title: functools.lru_cache does not consider strings and numpy strings as equivalent
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44992>
_______________________________________


More information about the New-bugs-announce mailing list