Skip to content

Commit c8b4ee2

Browse files
committed
Make "self" positional-only in @cachedmethod decorators and keys.
1 parent f806754 commit c8b4ee2

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

‎src/cachetools/_cachedmethod.py‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __reduce__(self):
3535
# wrappers
3636
return (self.__descriptor.__get__, (self._obj,))
3737

38-
def __call__(self, *args, **kwargs):
38+
def __call__(self, /, *args, **kwargs):
3939
raise NotImplementedError() # pragma: no cover
4040

4141
def cache_clear(self):
@@ -72,7 +72,7 @@ def __init__(self, obj, descriptor):
7272
self.__hits = self.__misses = 0
7373
self.__pending = set()
7474

75-
def __call__(self, *args, **kwargs):
75+
def __call__(self, /, *args, **kwargs):
7676
cache = self.cache
7777
lock = self.cache_lock
7878
cond = self.cache_condition
@@ -121,7 +121,7 @@ def __init__(self, obj, descriptor):
121121
super().__init__(obj, descriptor, method, cache, key, lock)
122122
self.__hits = self.__misses = 0
123123

124-
def __call__(self, *args, **kwargs):
124+
def __call__(self, /, *args, **kwargs):
125125
cache = self.cache
126126
lock = self.cache_lock
127127
key = self.cache_key(*args, **kwargs)
@@ -161,7 +161,7 @@ def __init__(self, obj, descriptor):
161161
super().__init__(obj, descriptor, method, cache, key)
162162
self.__hits = self.__misses = 0
163163

164-
def __call__(self, *args, **kwargs):
164+
def __call__(self, /, *args, **kwargs):
165165
cache = self.cache
166166
key = self.cache_key(*args, **kwargs)
167167
try:
@@ -220,7 +220,7 @@ def cache_clear(self):
220220
with lock(self):
221221
c.clear()
222222

223-
def classmethod_wrapper(self, *args, **kwargs):
223+
def classmethod_wrapper(self, /, *args, **kwargs):
224224
p = pending.setdefault(self, set())
225225
return wrapper(self, p, *args, **kwargs)
226226

@@ -230,7 +230,7 @@ def __init__(self, obj, descriptor):
230230
super().__init__(obj, descriptor, method, cache, key, lock, cond)
231231
self.__pending = set()
232232

233-
def __call__(self, *args, **kwargs):
233+
def __call__(self, /, *args, **kwargs):
234234
return wrapper(self._obj, self.__pending, *args, **kwargs)
235235

236236
# objtype: backward-compatible @classmethod handling with Python < 3.13
@@ -241,7 +241,7 @@ def cache_clear(self, _objtype=None):
241241

242242

243243
def _locked(method, cache, key, lock):
244-
def wrapper(self, *args, **kwargs):
244+
def wrapper(self, /, *args, **kwargs):
245245
c = cache(self)
246246
k = key(self, *args, **kwargs)
247247
with lock(self):
@@ -269,7 +269,7 @@ class Wrapper(_WrapperBase):
269269
def __init__(self, obj, descriptor):
270270
super().__init__(obj, descriptor, method, cache, key, lock)
271271

272-
def __call__(self, *args, **kwargs):
272+
def __call__(self, /, *args, **kwargs):
273273
return wrapper(self._obj, *args, **kwargs)
274274

275275
# objtype: backward-compatible @classmethod handling with Python < 3.13
@@ -280,7 +280,7 @@ def cache_clear(self, _objtype=None):
280280

281281

282282
def _unlocked(method, cache, key):
283-
def wrapper(self, *args, **kwargs):
283+
def wrapper(self, /, *args, **kwargs):
284284
c = cache(self)
285285
k = key(self, *args, **kwargs)
286286
try:
@@ -303,7 +303,7 @@ class Wrapper(_WrapperBase):
303303
def __init__(self, obj, descriptor):
304304
super().__init__(obj, descriptor, method, cache, key)
305305

306-
def __call__(self, *args, **kwargs):
306+
def __call__(self, /, *args, **kwargs):
307307
return wrapper(self._obj, *args, **kwargs)
308308

309309
# objtype: backward-compatible @classmethod handling with Python < 3.13

‎src/cachetools/keys.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def hashkey(*args, **kwargs):
5454
return _HashedTuple(args)
5555

5656

57-
def methodkey(self, *args, **kwargs):
57+
def methodkey(self, /, *args, **kwargs):
5858
"""Return a cache key for use with cached methods."""
5959
return hashkey(*args, **kwargs)
6060

@@ -72,6 +72,6 @@ def typedkey(*args, **kwargs):
7272
return key
7373

7474

75-
def typedmethodkey(self, *args, **kwargs):
75+
def typedmethodkey(self, /, *args, **kwargs):
7676
"""Return a typed cache key for use with cached methods."""
7777
return typedkey(*args, **kwargs)

‎tests/test_keys.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ def test_methodkey(self, key=cachetools.keys.methodkey):
3838
# untyped keys compare equal
3939
self.assertEqual(key("x", 1, 2, 3), key("y", 1.0, 2.0, 3.0))
4040
self.assertEqual(hash(key("x", 1, 2, 3)), hash(key("y", 1.0, 2.0, 3.0)))
41+
# "self" is positional-only, so it may also be a keyword argument
42+
self.assertEqual(key("x", self=1), key("y", self=1))
43+
self.assertNotEqual(key("x", self=1), key("x"))
4144

4245
def test_typedkey(self, key=cachetools.keys.typedkey):
4346
self.assertEqual(key(), key())
@@ -75,6 +78,9 @@ def test_typedmethodkey(self, key=cachetools.keys.typedmethodkey):
7578
self.assertNotEqual(key("x", 1, 2, 3), key("x", 1.0, 2.0, 3.0))
7679
self.assertNotEqual(key("x", 1, 2, 3, x=0), key("x", 1, 2, 3, x=0.0))
7780
self.assertNotEqual(key("x", 1, 2, 3, x=0), key("x", 1, 2, 3, x=False))
81+
# "self" is positional-only, so it may also be a keyword argument
82+
self.assertEqual(key("x", self=1), key("y", self=1))
83+
self.assertNotEqual(key("x", self=1), key("x", self=1.0))
7884

7985
def test_addkeys(self, key=cachetools.keys.hashkey):
8086
self.assertIsInstance(key(), tuple)

0 commit comments

Comments
 (0)