levitsky / pyteomics

Pyteomics is a collection of lightweight and handy tools for Python that help to handle various sorts of proteomics data. Pyteomics provides a growing set of modules to facilitate the most common tasks in proteomics data analysis.
http://pyteomics.readthedocs.io
Apache License 2.0
105 stars 34 forks source link

Add a __version__ method to pyteomics #13

Closed sugatoray closed 3 years ago

sugatoray commented 3 years ago

Typically python packages have a dunder method for version: package.__version__.

It helps to check and log the version of the package under use in an analysis. However, I could not find any such pyteomics.__version__ method to access the version. I see that you have a file VERSION in the repo and the setup.py file accesses the current version information from here.

Please add a pyteomics.__version__ method to dynamically provide the version of the package.

If however, I have missed out on something or am mistaken please let me know.

Thank you.

levitsky commented 3 years ago

Hi, thank you for the feedback!

Indeed, there is no method or attribute that captures the current version in pyteomics. It should be fairly easy to add one for convenience.

However, version info can be extracted fairly easily in Python, like with any other package. See e.g. this answer on SO.

Demo:

In [1]: import pkg_resources

In [2]: pkg_resources.get_distribution('pyteomics').version
Out[2]: '4.4.0.dev1'

Also, on Python 3.8 and newer:

In [1]: from importlib.metadata import version

In [2]: version('pyteomics')
Out[2]: '4.4.0.dev1'
levitsky commented 3 years ago

Looking more into this, I find that I cannot easily add anything to pyteomics/__init__.py, as Pyteomics is a namespace package. Best I can do then is make it available under pyteomics.auxiliary (which is not as easy to find) or perhaps create a module called pyteomics.version. Does anyone have any suggestions? @sugatoray @mobiusklein

mobiusklein commented 3 years ago

I think a pyteomics.version submodule is probably the safest and cleanest way if we want to support this without requiring the user import the those tools (not that we can't try to import and use them ourselves to get it). The top-level module having a __version__ attribute is convenient, but not all packages can support it anyway, and it's not actually part of Python, just a convention.

If there absolutely has to be a __version__ attribute on pyteomics, we can force that to happen if the user imports at least one submodule first, and then have submodules import the following monkey-patching module

import sys
import pkg_resources
version = pkg_resources.get_distribution('pyteomics').version
sys.modules['pyteomics'].__version__ = version

But this would lead to unexpected behavior when the user just tries to read that attribute off a top-level import of pyteomics alone.

sugatoray commented 3 years ago

I tried to find a repo that has some similarity to pyteomics. Though I am not sure if it is a good example or not, I would suggest you take a look at mendley repo as an alternative.

They also use a version file (version.py) that introduces a package.__version__ attribute.