Setting SQLALCHEMY_SILENCE_UBER_WARNING to avoid deprecation warning does not work.

huangapple go评论60阅读模式
英文:

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 &quot;sqlalchemy&lt;2.0&quot;. 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=&#39;public&#39;))

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[&#39;SQLALCHEMY_SILENCE_UBER_WARNING&#39;] = &#39;1&#39;

PublicBase = sqlalchemy.ext.declarative.declarative_base(metadata=sqlalchemy.schema.MetaData(schema=&#39;public&#39;))

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 中,其值为 TrueFalse。参考:

https://github.com/sqlalchemy/sqlalchemy/blob/8e465ce2788a726c614c060c11ae77b05d0b99af/lib/sqlalchemy/util/deprecations.py#L26

然而,我们似乎可以自行调整这个变量:

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:

https://github.com/sqlalchemy/sqlalchemy/blob/8e465ce2788a726c614c060c11ae77b05d0b99af/lib/sqlalchemy/util/deprecations.py#L26

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.

huangapple
  • 本文由 发表于 2023年5月22日 23:00:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307478.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定