youknowone / ring

Python cache interface with clean API and built-in memcache & redis + asyncio support.
http://ring-cache.readthedocs.io/en/latest/
Other
480 stars 37 forks source link

ring.key - missing x required positional argument when using class #184

Closed matthieudesprez closed 3 years ago

matthieudesprez commented 3 years ago

This issue happened after upgrading from 0.7.3 to 0.8.0

0.7.3

!pip install ring==0.7.3
import ring

class A:
  @ring.dict({})
  def get_entity(self, id):
      return {"test": "ok"}

  @get_entity.ring.key
  def get_entity_key(self, id):
      return f"test:{id}"

a = A()
a.get_entity("123")     

outputs

{'test': 'ok'}

0.8.0

!pip install ring==0.8.0
import ring

class A:
  @ring.dict({})
  def get_entity(self, id):
      return {"test": "ok"}

  @get_entity.ring.key
  def get_entity_key(self, id):
      return f"test:{id}"

a = A()
a.get_entity("123")   

outputs

TypeError                                 Traceback (most recent call last)
<ipython-input-1-6a194b8b4dec> in <module>()
     12 
     13 a = A()
---> 14 a.get_entity("123")

5 frames
/usr/local/lib/python3.7/dist-packages/ring/func/base.py in <lambda>(pargs)
    709 
    710     def key(self, func):
--> 711         self._rope.compose_key = lambda pargs: func(*pargs.args, **pargs.kwargs)
    712 
    713     def encode(self, func):

TypeError: get_entity_key() missing 1 required positional argument: 'id'

Solution

remove self from ring.key annotated functions if they are defined in class

youknowone commented 3 years ago

thank you, I will look in it

youknowone commented 3 years ago

I released 0.9.1 with bugfix: https://pypi.org/project/ring/0.9.1/

Thank you!

majorgilles commented 2 years ago

Hello we still have that issue with 0.9.1, but not in 0.9.0

youknowone commented 2 years ago

@majorgilles This issue is covered by test_customize::test_override_key_class. Could you share the result of pip freeze and the code actually make problems in your code?