clarete / forbiddenfruit

Patch built-in python objects
https://clarete.li/forbiddenfruit/
GNU General Public License v3.0
826 stars 52 forks source link

object has no attribute '__dict__' #66

Closed 1Mark closed 2 years ago

1Mark commented 2 years ago

I'm using 0.1.4 and get

E       AttributeError: 'cimpl.Consumer' object has no attribute '__dict__'

when trying to mock https://docs.confluent.io/platform/current/clients/confluent-kafka-python/html/index.html#confluent_kafka.Consumer.poll but im faced with this error


>       forbiddenfruit.curse(consumer, "poll", MockKafkaMessage())

tests/test_consumer.py:179: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../site-packages/forbiddenfruit/__init__.py:428: in curse
    dikt = patchable_builtin(klass)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

klass = <cimpl.Consumer object at 0x1149baa60>

    def patchable_builtin(klass):
>       refs = gc.get_referents(klass.__dict__)
E       AttributeError: 'cimpl.Consumer' object has no attribute '__dict__'

../site-packages/forbiddenfruit/__init__.py:223: AttributeError

Where consumer is an instance of

confluent_kafka.Consumer
1Mark commented 2 years ago

My bad forbiddenfruit patches attributes of a class and not an instance of a class.

I fixed this by doing

from confluent_kafka import Consumer
forbiddenfruit.curse(Consumer, "poll", MockKafkaMessage())
consumer = Consumer(...)

instead of

from confluent_kafka import Consumer
consumer = Consumer(...)
forbiddenfruit.curse(consumer, "poll", MockKafkaMessage())