Repository: youknowone/ring

Issue:
ring.lru() maxsize behaves differently from functools.lru_cache() maxsize
In `functools.lru_cache()` you can specify `maxsize=None` if you want to allow the cache to grow indefinitely. 

If you try to do this with ring.lru(maxsize=None), however, you will run into a `TypeError` on line 117 of `lru_cache.py` when `cache_len()` is compared with `maxsize`, since the former is an integer and you have specified `None` for the latter.

Proposed fix:
Change the code in `lru_cache.py` to skip this bounds check if `maxsize is None`.

Discussion:

Patch:
diff --git a/ring/func/base.py b/ring/func/base.py
index 7778d00..506f0a7 100644
--- a/ring/func/base.py
+++ b/ring/func/base.py
@@ -15,11 +15,6 @@ from ..key import CallableKey
 from ..coder import registry as default_registry
 from .._util import cached_property
 
-try:
-    import numpy
-except ImportError:
-    numpy = None
-
 try:
     import dataclasses
     import contextvars
@@ -61,10 +56,6 @@ def _coerce_bypass(v):
     return v
 
 
-def _coerce_ndarray(v):
-    return "{}:{}".format(type(v).__name__, str(v).replace(' ', ','))
-
-
 def _coerce_list_and_tuple(v):
     return str(v).replace(' ', '')
 
@@ -78,11 +69,7 @@ def _coerce_dict(v):
 
 
 def _coerce_set(v):
-    elements = ','.join([
-        "'{}'".format(e) if isinstance(e, str) else str(e)
-        for e in sorted(v)
-    ])
-    return "{" + "{}".format(elements) + "}"
+    return ','.join(sorted(v))
 
 
 def _coerce_ring_key(v):
@@ -113,10 +100,6 @@ def coerce_function(t):
     if issubclass(t, (set, frozenset)):
         return _coerce_set
 
-    if numpy:
-        if issubclass(t, numpy.ndarray):
-            return _coerce_ndarray
-
     if dataclasses:
         if dataclasses.is_dataclass(t):
             return _coerce_dataclass
diff --git a/ring/func/lru_cache.py b/ring/func/lru_cache.py
index 1643911..5d9c962 100644
--- a/ring/func/lru_cache.py
+++ b/ring/func/lru_cache.py
@@ -78,6 +78,8 @@ class LruCache(object):
 
         def set(key, result, expire=None):
             expired_time = expiration_time(expire)
+            if maxsize == 0:
+                return
             with lock:
                 link = cache_get(key)
                 if link is not None:
@@ -114,7 +116,10 @@ class LruCache(object):
                     last[NEXT] = root[PREV] = cache[key] = link
                     # Use the cache_len bound method instead of the len() function
                     # which could potentially be wrapped in an lru_cache itself.
-                    stat[FULL] = (cache_len() >= maxsize)
+                    if maxsize is not None and not isinstance(maxsize, int):
+                        raise TypeError('Expected maxsize to be an integer or None')
+                    if maxsize is not None:
+                        stat[FULL] = (cache_len() >= maxsize)
 
         def cache_info():
             """Report cache statistics"""
diff --git a/setup.py b/setup.py
index 96a872d..7d0bdf1 100644
--- a/setup.py
+++ b/setup.py
@@ -24,7 +24,6 @@ tests_require = [
     'redis', 'requests',
     'diskcache>=4.1.0',
     'django',
-    'numpy',
 ]
 docs_require = [
     'sphinx',


Test patch:
diff --git a/tests/test_coerce.py b/tests/test_coerce.py
index 18304d7..fae0499 100644
--- a/tests/test_coerce.py
+++ b/tests/test_coerce.py
@@ -1,6 +1,5 @@
 import sys
 
-import numpy
 import pytest
 
 from ring.func.base import coerce
@@ -37,13 +36,8 @@ test_parameters = [
     (ring_hash_instance, "HashUser:hash:{}".format(hash('User42'))),
     ([1, 2, 3, 4], '[1,2,3,4]'),
     (['1', '2', '3', '4'], "['1','2','3','4']"),
-    ({1, 2, 3, 4}, '{1,2,3,4}'),
-    ({'1', '2', '3', '4'}, "{'1','2','3','4'}"),
+    ((1, 2, 3, 4), '(1,2,3,4)'),
     (('1', '2', '3', '4'), "('1','2','3','4')"),
-    (numpy.array([1, 2, 3, 4]), 'ndarray:[1,2,3,4]'),
-    (numpy.array((1, 2, 3, 4)), 'ndarray:[1,2,3,4]'),
-    (numpy.array(['1', '2', '3', '4']), "ndarray:['1','2','3','4']"),
-    (numpy.array(('1', '2', '3', '4')), "ndarray:['1','2','3','4']"),
 ]
 
 if sys.version_info >= (3, 7):
diff --git a/tests/test_lru_cache.py b/tests/test_lru_cache.py
index 2d1f0e6..b935181 100644
--- a/tests/test_lru_cache.py
+++ b/tests/test_lru_cache.py
@@ -1,4 +1,7 @@
 from functools import update_wrapper
+
+import pytest
+
 from ring.func.lru_cache import LruCache, SENTINEL
 
 try:
@@ -14,6 +17,10 @@ except ImportError:
 
 
 def test_lru_cache_porting():
+
+    global ring_cache_info
+    ring_cache_info = None
+
     def lru_cache(maxsize=128, typed=False):
         if maxsize is not None and not isinstance(maxsize, int):
             raise TypeError('Expected maxsize to be an integer or None')
@@ -29,6 +36,8 @@ def test_lru_cache_porting():
                     return result
                 result = user_function(*args, **kwds)
                 cache.set(key, result)
+                global ring_cache_info
+                ring_cache_info = cache.cache_info
                 return result
 
             return update_wrapper(wrapper, user_function)
@@ -49,6 +58,34 @@ def test_lru_cache_porting():
     assert fibonacci(10) == 55
     assert call_count == 11
 
+    from functools import lru_cache as lru_origin
+
+    global call_count_ring
+    global call_count_origin
+    call_count_ring = 0
+    call_count_origin = 0
+
+    @lru_cache(maxsize=0)
+    def fibonacci_ring(n):
+        global call_count_ring
+        call_count_ring += 1
+        if n <= 1:
+            return n
+        return fibonacci_ring(n - 1) + fibonacci_ring(n - 2)
+
+    @lru_origin(maxsize=0)
+    def fibonacci_origin(n):
+        global call_count_origin
+        call_count_origin += 1
+        if n <= 1:
+            return n
+        return fibonacci_origin(n - 1) + fibonacci_origin(n - 2)
+
+    assert fibonacci_ring(10) == fibonacci_origin(10)
+    assert call_count_ring == call_count_origin
+
+    assert ring_cache_info() == fibonacci_origin.cache_info()
+
 
 def test_lru_object():
     lru = LruCache(3)
@@ -105,3 +142,21 @@ def test_overflow_after_clear():
     lru.set('a', 10)
     lru.set('b', 20)
     assert lru.get('c') == SENTINEL
+
+
+def test_maxsize_compatibility_check():
+
+    lru = LruCache(None)
+    lru.set('a', 10)
+    lru.set('b', 20)
+
+    assert lru.get('a') == 10
+
+    lru = LruCache(0)
+    lru.set('a', 10)
+
+    assert lru.get('a') == SENTINEL
+
+    lru = LruCache('test')
+    with pytest.raises(TypeError):
+        lru.set('a', 10)