Skip to content

[FEATURE] Improve Backward Compatibility for pickling #4111

@Bibo-Joshi

Description

@Bibo-Joshi

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

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

  1. first try set_attr
  2. check if the attribute name is a property
  3. if yes, try setting _attributename (unless the name already starts with underscore)
  4. 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

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.

Additional context

https://t.me/pythontelegrambotgroup/723964

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions