如何通過一個package觸發棄用警告?

2024-04-19 14:06:24 发布

您现在位置:Python中文网/ 问答频道 /正文

在单元测试中,我刚得到

werkzeug/local.py:347: DeprecationWarning: json is deprecated.  Use get_json() instead.

我将其追溯到相关文件venv/lib/python3.6/site-packages/werkzeug/local.py。有问题的行是以下方法中的最后一行。你知道吗

def __getattr__(self, name):
    if name == '__members__':
        return dir(self._get_current_object())
    return getattr(self._get_current_object(), name)  # Line 347

request.json转移到request.get_json()似乎已经很老了(2013年?),Werkzeug文件中的快速递归grep显示request.json没有使用。 使用request.json而不是request.get_json()的有问题的代码似乎是我自己的。我的静脉中的Werkzeug和烧瓶都是0.12.2。你知道吗

如何通过包触发不推荐使用的警告,而不pytest甚至指向实际使用不推荐使用的接口的行?你知道吗


Tags: 文件namepyselfjsongetreturnobject
1条回答
网友
1楼 · 发布于 2024-04-19 14:06:24

我认为软件包本身并没有触发DeprecationWarning。Pytest正在查看代码并警告您代码已被弃用,这可能在您的控制之下,也可能不在您的控制之下。你知道吗

By default pytest will display DeprecationWarning and PendingDeprecationWarning warnings from user code and third-party libraries, as recommended by PEP-0565. This helps users keep their code modern and avoid breakages when deprecated warnings are effectively removed.

Sometimes it is useful to hide some specific deprecation warnings that happen in code that you have no control over (such as third-party libraries), in which case you might use the warning filters options (ini or marks) to ignore those warnings.

至于deprecation警告是如何触发的,以及它应该归属于何处,似乎PEP 565对此有一些看法:

In Python 2.7 and Python 3.2, the default warning filters were updated to hide DeprecationWarning by default, such that deprecation warnings in development tools that were themselves written in Python (e.g. linters, static analysers, test runners, code generators), as well as any other applications that merely happened to be written in Python, wouldn't be visible to their users unless those users explicitly opted in to seeing them.

However, this change has had the unfortunate side effect of making DeprecationWarning markedly less effective at its primary intended purpose: providing advance notice of breaking changes in APIs (whether in CPython, the standard library, or in third party libraries) to users of those APIs.

To improve this situation, this PEP proposes a single adjustment to the default warnings filter: displaying deprecation warnings attributed to the main module by default.

至于您的json弃用警告来自何处,pytest将您指向Werzkug似乎很奇怪,而且根本没有在其中使用它。也许这是由于上面的一些限制?你知道吗

相关问题 更多 >