-
Notifications
You must be signed in to change notification settings - Fork 6k
Description
What kind of feature are you missing? Where do you notice a shortcoming of PTB?
When TG deprecates attributes, we usually convert them to properties in our classes and issue a warning on access. For this, we rename Class.attribute to Class.._attribute. This leads to problems on un-pickling data that was pickled with previous versions. While this is okay acccording to our stability policy, it would still be nice to handle this better for the users
Describe the solution you'd like
In TelegramObjejct.__setstate__, the current logic is
python-telegram-bot/telegram/_telegramobject.py
Lines 311 to 322 in 1cf63c2
| for key, val in state.items(): | |
| try: | |
| setattr(self, key, val) | |
| except AttributeError: | |
| # catch cases when old attributes are removed from new versions | |
| api_kwargs[key] = val # add it to api_kwargs as fallback | |
| # For api_kwargs we first apply any kwargs that are already attributes of the object | |
| # and then set the rest as MappingProxyType attribute. Converting to MappingProxyType | |
| # is necessary, since __getstate__ converts it to a dict as MPT is not pickable. | |
| self._apply_api_kwargs(api_kwargs) | |
| self.api_kwargs = MappingProxyType(api_kwargs) |
I would like to change this to
- first try set_attr
- check if the attribute name is a property
- if yes, try setting _attributename (unless the name already starts with underscore)
- if that fails or the attribute is not a property, put the attribute into
api_kwargs- unless the attribute has a leading underscore. This last part is for when we finally completely remove the property - pickling with the property in place and unpickling without the property in the new version will then simply remove the attribute.
Moreover, in _apply_api_kwargs
python-telegram-bot/telegram/_telegramobject.py
Lines 492 to 494 in 1cf63c2
| for key in list(api_kwargs.keys()): | |
| if getattr(self, key, True) is None: | |
| setattr(self, key, api_kwargs.pop(key)) |
we can first check if tha attribute is a property
Describe alternatives you've considered
Do nothing as we didn't promise that this works.