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

Update imports for sqlalchemy v2.0 changes #126

Closed RalfG closed 3 months ago

RalfG commented 9 months ago

In sqlalchemy version 2.0, declarative_base and DeclarativeMeta moved to another module. This adds a try-except around the imports to retain compatibility with sqlalchemy <2.0 and >2.0. Before, the import still worked, but resulted in MovedIn20Warning (for example: https://github.com/compomics/psm_utils/actions/runs/6239222484/job/16936699966?pr=50#step:7:35)

levitsky commented 9 months ago

Hi @RalfG, thanks for the patch! I'm fine with it but I'm a bit confused that I don't see these warnings with the latest SQLAlchemy, nor with the latest pre-2.0 version. Not sure if I'm missing something, but there is a different set of unimod warnings in my tests over here. Also, if the import works, then how do expect to catch an ImportError? Or is it for some future version?

RalfG commented 3 months ago

Not sure why the warning only shows up sometimes. I still have it in more recent test runs:

../../../../../opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/pyteomics/mass/unimod.py:53
  /opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/pyteomics/mass/unimod.py:53: MovedIn20Warning: The ``declarative_base()`` function is now available as sqlalchemy.orm.declarative_base(). (deprecated since: 2.0) (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
    Base = declarative_base(metaclass=SubclassRegisteringDeclarativeMeta)

https://github.com/compomics/psm_utils/actions/runs/8454522238/job/23159731768#step:7:36

RalfG commented 3 months ago

Also, if the import works, then how do expect to catch an ImportError? Or is it for some future version?

The original location was deprecated in 2.0, so I expect there to be ImportErrors in future versions.

levitsky commented 3 months ago

Thank you @RalfG ! It seems like SQLAlchemy has a convoluted decorator system responsible for those warnings, and so this particular warning is not emitted at import time, rather at call time. Also, the default Python warning filters ignore this warning, which is why I wasn't able to see it at all outside of pytest.

I will just merge the PR now, thank you for contributing!