smarie/python-pep-ideas

An API to get the version of anything (instance, class, function, module, package) reliably

Opened this issue · 0 comments

Our original need for an industrial project was to persist python objects (actually, machine learning models) with the guarantee that they could be deserialized again later. We knew that the environment would change as the libraries used in the production environment were already old at the time of development.

As libraries evolve, their object models and public APIs change (even if this is not recommended), and it is difficult to be sure that module names, class names, attribute names... will continue to be the same ones in the deserialization environment. So I wrote a json serialization library (not yet published) that actually stores the version and namespace of the class when serializing an object, in order for the deserialization code to be able to make the most informed decision. For example maybe a field did not exist in a previous version of the library and now exists, but it can safely be set with a default value. Or a field changed from a private name to a public name, the value just has to be routed.

A prerequisite for such automation was to be able to get the version of every package and module. As it turned out, this was absolutely not an easy thing to do, as PEP396 and PEP345+PEP427 only cover part of the problem. I wrote getversion as a reference implementation of what I would have loved to find in the stdlib.

Is this a potential PEP ? Well I don't know. Maybe. But at the same time since the version information can come from Source Code Management tools (git, svn, ...) we should be very caution to propose reference practices so that PEP396 __version__ is easy to create in real-world development cycles.

See also this stackoverflow post and this other one.