DeprecationWarning、PendingDeprecationWarning 和 FutureWarning 之间的区别

Difference between DeprecationWarning, PendingDeprecationWarning and FutureWarning

DeprecationWarningPendingDeprecationWarningFutureWarning有什么区别?我在 Python 3 documentation 中看到目标“受众”的术语有所不同,尤其是我不理解“开发人员”和“最终用户”之间的区别。这个概念对我来说有点模糊。谁能解释并举例说明?

我做了一点table总结用例:

+---------------+---------------------------+---------------+
|               | Developers                | End Users     |
+---------------+---------------------------+---------------+
| Now           | DeprecationWarning        | ???           |
| In the future | PendingDeprecationWarning | FutureWarning |
+---------------+---------------------------+---------------+

是否有针对最终用户的“弃用警告”?

如果我开发自己的库。使用这些警告是个好主意还是我应该使用其他东西的子类?在哪个用例中?

观众的问题很大程度上与这样一种想法有关,即一些 Python 被编写成一个库,旨在供编写 python 脚本的其他人使用,而一些 Python 被编写为旨在供可能不懂任何编程的人使用的应用程序。

您所指的具体描述是 Python 3.7 中的更改您可以在 https://www.python.org/dev/peps/pep-0565/ 阅读更改的完整描述,但这里有一个特别相关的部分,其中包含示例用例:

This will give the following three distinct categories of backwards compatibility warning, with three different intended audiences:

  • PendingDeprecationWarning: hidden by default for all code. The intended audience is Python developers that take an active interest in ensuring the future compatibility of their software (e.g. professional Python application developers with specific support obligations).
  • DeprecationWarning: reported by default for code that runs directly in the __main__ module (as such code is considered relatively unlikely to have a dedicated test suite), but hidden by default for code in other modules. The intended audience is Python developers that are at risk of upgrades to their dependencies (including upgrades to Python itself) breaking their software (e.g. developers using Python to script environments where someone else is in control of the timing of dependency upgrades).
  • FutureWarning: reported by default for all code. The intended audience is users of applications written in Python, rather than other Python developers (e.g. warning about use of a deprecated setting in a configuration file format).

我认为您的 table 不太准确 -- FutureWarning,据我了解,应该针对已弃用的内容 现在 .据我了解,DeprecationWarning 表示 "change your code now or it will break soon",PendingDeprecationWarning 表示 "you're going to have to change something eventually",FutureWarning 表示 "something in the way you're using this isn't correct, and may lead to failure later."

FutureWarning 也用于警告您在未来的更新中不会有相同的行为,即使它们将是有效代码。这可能与开发人员和用户都相关。例如,我在实践中看到的许多 FutureWarnings 是一些方便函数的含义可能会改变的东西(就像 == 对于两个数组 return 和 True/False 对于每个元素,还是 return 一个 True/False,只有当所有元素都相等时才为真?当 numpy 想要改变它时,他们会做一个 FutureWarning)

在开发你的库时,一定要使用这些或者它们的子类。使用您的库编写代码的人希望他们的集成测试在存在潜在问题时发出 DeprecationWarnings(更准确地说,测试工具可能会专门寻找这些问题)。