Skip to content

Commit 52ce039

Browse files
authored
Fix Bug in BasePersistence.insert/replace_bot for Objects with __dict__ in their slots (python-telegram-bot#2561)
* Handle objects with __dict__ in __slots__ * Rework
1 parent ac47681 commit 52ce039

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

telegram/ext/basepersistence.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,8 @@ def _replace_bot(cls, obj: object, memo: Dict[int, object]) -> object: # pylint
276276
new_obj[cls._replace_bot(k, memo)] = cls._replace_bot(val, memo)
277277
memo[obj_id] = new_obj
278278
return new_obj
279-
if hasattr(obj, '__dict__'):
280-
for attr_name, attr in new_obj.__dict__.items():
281-
setattr(new_obj, attr_name, cls._replace_bot(attr, memo))
282-
memo[obj_id] = new_obj
283-
return new_obj
279+
# if '__dict__' in obj.__slots__, we already cover this here, that's why the
280+
# __dict__ case comes below
284281
if hasattr(obj, '__slots__'):
285282
for attr_name in new_obj.__slots__:
286283
setattr(
@@ -290,6 +287,11 @@ def _replace_bot(cls, obj: object, memo: Dict[int, object]) -> object: # pylint
290287
)
291288
memo[obj_id] = new_obj
292289
return new_obj
290+
if hasattr(obj, '__dict__'):
291+
for attr_name, attr in new_obj.__dict__.items():
292+
setattr(new_obj, attr_name, cls._replace_bot(attr, memo))
293+
memo[obj_id] = new_obj
294+
return new_obj
293295

294296
return obj
295297

@@ -364,11 +366,8 @@ def _insert_bot(self, obj: object, memo: Dict[int, object]) -> object: # pylint
364366
new_obj[self._insert_bot(k, memo)] = self._insert_bot(val, memo)
365367
memo[obj_id] = new_obj
366368
return new_obj
367-
if hasattr(obj, '__dict__'):
368-
for attr_name, attr in new_obj.__dict__.items():
369-
setattr(new_obj, attr_name, self._insert_bot(attr, memo))
370-
memo[obj_id] = new_obj
371-
return new_obj
369+
# if '__dict__' in obj.__slots__, we already cover this here, that's why the
370+
# __dict__ case comes below
372371
if hasattr(obj, '__slots__'):
373372
for attr_name in obj.__slots__:
374373
setattr(
@@ -378,6 +377,11 @@ def _insert_bot(self, obj: object, memo: Dict[int, object]) -> object: # pylint
378377
)
379378
memo[obj_id] = new_obj
380379
return new_obj
380+
if hasattr(obj, '__dict__'):
381+
for attr_name, attr in new_obj.__dict__.items():
382+
setattr(new_obj, attr_name, self._insert_bot(attr, memo))
383+
memo[obj_id] = new_obj
384+
return new_obj
381385

382386
return obj
383387

tests/test_persistence.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,14 +561,15 @@ class MyUpdate:
561561

562562
def test_bot_replace_insert_bot(self, bot, bot_persistence):
563563
class CustomSlottedClass:
564-
__slots__ = ('bot',)
564+
__slots__ = ('bot', '__dict__')
565565

566566
def __init__(self):
567567
self.bot = bot
568+
self.not_in_dict = bot
568569

569570
def __eq__(self, other):
570571
if isinstance(other, CustomSlottedClass):
571-
return self.bot is other.bot
572+
return self.bot is other.bot and self.not_in_dict is other.not_in_dict
572573
return False
573574

574575
class CustomClass:
@@ -587,6 +588,7 @@ def replace_bot():
587588
cc = CustomClass()
588589
cc.bot = BasePersistence.REPLACED_BOT
589590
cc.slotted_object.bot = BasePersistence.REPLACED_BOT
591+
cc.slotted_object.not_in_dict = BasePersistence.REPLACED_BOT
590592
cc.list_ = [1, 2, BasePersistence.REPLACED_BOT]
591593
cc.tuple_ = tuple(cc.list_)
592594
cc.set_ = set(cc.list_)

0 commit comments

Comments
 (0)