Skip to content

Instantly share code, notes, and snippets.

@ncoghlan
Created July 24, 2018 15:28
Show Gist options
  • Save ncoghlan/0f7727d7a32114899b18b963ce172322 to your computer and use it in GitHub Desktop.
Save ncoghlan/0f7727d7a32114899b18b963ce172322 to your computer and use it in GitHub Desktop.
API renames, aliasing, and cross-version pickle compatibility
>>> class C:
... def example(self): pass
...
>>> import pickle
>>> data = pickle.dumps(C.example)
>>> pickle.loads(C.example)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'function'
>>> pickle.loads(data)
<function C.example at 0x7f2fb7786048>
>>> del C.example # Pretend we're sending to an old version with no "example" method
>>> pickle.loads(data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'C' has no attribute 'example'
@ncoghlan
Copy link
Author

The reason this is annoying when it comes to API renames is that you have to choose between the following undesirable options:

  1. Having introspection on new versions continue to report the "old" API name (by making the new name an alias for the old one, so introspection picks up the old name)
  2. Having pickles generated on new versions be incompatible with old versions lacking the alias (by making the old name an alias for the new one, so pickling picks up the new name)
  3. Running with option 1 for a while, then eventually switching to option 2 (this is basically how you have to do it if you want to avoid a hard compatibility break)

Bonus level: if you ever want to actually deprecate and remove the old name, you don't even get to start that process until after adopting option 2 above :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment