A Python package I am working on was built with SQLAlchemy v1.4 and there is no plan to upgrade to v2 in the foreseeable future.
I have set up the installation requirements accordingly, so that SQLA version can be >=1.3 and < 2.0.
Now I just want to get rid of the deprecation warning when I run my tests. Currently I get this warning:
[...]/src/db/base.py:12: MovedIn20Warning: Deprecated API features detected! These feature(s) are not compatible with SQLAlchemy 2.0. To prevent incompatible upgrades prior to updating applications, ensure requirements files are pinned to "sqlalchemy<2.0". Set environment variable SQLALCHEMY_WARN_20=1 to show all deprecation warnings. Set environment variable SQLALCHEMY_SILENCE_UBER_WARNING=1 to silence this message. (Background on SQLAlchemy 2.0 at: https://sqlalche.me/e/b8d9)
The line 12 that triggers the warning is this:
PublicBase = sqlalchemy.ext.declarative.declarative_base(metadata=sqlalchemy.schema.MetaData(schema='public'))
which makes sense because that is the first time SQLA is called to do some work.
I want to silence the warning by setting the env variable SQLALCHEMY_SILENCE_UBER_WARNING
as mentioned in the warning message.
When I do this from the shell like this, it works and the warning is gone:
export SQLALCHEMY_SILENCE_UBER_WARNING=1
but when I insert the following line just before the line that triggered the warning (base.py line 11) so that I have this in my code:
os.environ['SQLALCHEMY_SILENCE_UBER_WARNING'] = '1'
PublicBase = sqlalchemy.ext.declarative.declarative_base(metadata=sqlalchemy.schema.MetaData(schema='public'))
the warning is still issued. I have dropped into pdb and checked that the env variable is indeed set to 1 at the point where I define PublicBase so I'm not sure why this doesn't seem to take effect. Can anyone help?
EDIT: To be clear: what does NOT work is setting the env variable from the script in Python itself. If I set the variable in the shell environment, it DOES work but that would require every user who might run the tests to set the variable which is not what I want. Thanks
SQLAlchemy does not look up the envar every time it needs to check whether to emit the uber warning. It checks the envar once and caches the result to the variable SILENCE_UBER_WARNING
as True
or False
. ref:
However, it appears that we can tweak the variable ourselves:
from sqlalchemy.util import deprecations
deprecations.SILENCE_UBER_WARNING = True
That seems to suppress the warning.