英文:
Setting SQLALCHEMY_SILENCE_UBER_WARNING to avoid deprecation warning does not work
问题
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.
英文:
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
答案1
得分: 2
SQLAlchemy 不会每次需要检查是否发出“uber”警告时都查找环境变量。它只在需要时检查一次,并将结果缓存到变量 SILENCE_UBER_WARNING
中,其值为 True
或 False
。参考:
然而,我们似乎可以自行调整这个变量:
from sqlalchemy.util import deprecations
deprecations.SILENCE_UBER_WARNING = True
这样似乎可以抑制警告。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论