i wanna monkey patch some other class with my own class.
i tried using Tomonkeypatch.some_func = some_func
. it works but i want to neat method to do so (i.e classes).
I am trying to Monkey patch Message Object in pyrogram
Here is my code:
import pyrogram
class Message(pyrogram.types.messages_and_media.Message):
def __init__(self, **kwargs):
super().__init__(**kwargs)
@property
def test(self):
return "test"
While i use the code in my handler
from pyrogram import filters
@client.on_message(filters.private)
async def sometest(client, message):
s = message.test
await message.reply(s)
i get :
AttributeError: 'Message' object has no attribute 'test'
but, i monkey patched in pyrogram? then why?
Thank you, in advance!
You have to monkey patch the original Message object that pyrogram uses.
from pyrogram.types import Message
@property
def test(self):
return "test"
Message.test = test
If you really wanna update the Message class when changing the subclass (NOT RECOMMENDED!) you can do this:
from pyrogram.types import Message
Message.__init_subclass__ = classmethod(lambda sub: [setattr(Message, k, v) for (k,v) in sub.__dict__.items() if k[:2] != '__'])
class MyMessage(Message):
def __init__(self, **kwargs):
super().__init__(**kwargs)
@property
def test(self):
return "test"