本文介绍了DeprecationWarning,PendingDeprecationWarning和FutureWarning之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DeprecationWarning PendingDeprecationWarning FutureWarning 有什么区别?我在 Python 3文档中看到了一个区别在目标受众"方面,尤其是我不理解开发人员"和最终用户"之间的区别.这个概念对我来说有点模糊.谁能解释并举例?

What is the difference between DeprecationWarning, PendingDeprecationWarning and FutureWarning? I saw in the Python 3 documentation that there is a difference in term of target "audience", especially I don't understand the difference between "developers" and "end users". This notion is a little blur for me. Can anyone explain and give examples?

我做了一张小桌子来总结用例:

I made a little table to summarize the use cases:

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

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

Is there a "deprecation warning" for end users?

如果我开发自己的库.使用这些警告是一个好主意,还是应该使用其他内容的子类?在哪种情况下?

If I develop my own library. Is it a good idea to use those warnings or should I use a subclass of something else? In which use case?

推荐答案

观众的问题很大程度上与以下想法有关:一些Python被编写为库,供其他编写python脚本的人使用,而另一些Python被编写为旨在供可能不了解任何程序的人使用的应用程序.

The audience question is largely related to the idea that some Python is written to be a library, intended to be used by other people who write python scripts, whereas some Python is written to be applications that are intended to be used by people who might not know any programming.

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

The specific description you're referring to was a change in Python 3.7 You can read the whole description of the change at https://www.python.org/dev/peps/pep-0565/, but here's a particularly relevant section, with example use cases:

  • PendingDeprecationWarning:默认情况下对所有代码隐藏.目标受众是对确保软件的未来兼容性非常感兴趣的Python开发人员(例如,具有特定支持义务的专业Python应用程序开发人员).
  • DeprecationWarning:默认情况下报告直接在 __ main __ 模块中运行的代码(因为这样的代码被认为不太可能具有专用的测试套件),但默认情况下对于其他模块中的代码隐藏.预期的受众是那些有可能升级其依赖项(包括升级到Python本身)并破坏其软件的Python开发人员(例如,使用Python编写脚本的环境中其他人可以控制依赖项升级的时间的开发人员).
  • FutureWarning:默认情况下报告所有代码.目标受众是使用Python编写的应用程序的用户,而不是其他Python开发人员(例如,警告使用配置文件格式的不赞成使用的设置).
  • 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).

我认为您的表格不太准确-据我了解, FutureWarning 应该用于现在已经不赞成使用的东西.据我了解, DeprecationWarning 表示现在更改您的代码,否则将很快中断", PendingDeprecationWarning 表示您最终将不得不更改某些内容",并且 FutureWarning 的意思是您使用的方式不正确,以后可能会导致失败."

I don't think your table is quite accurate -- FutureWarning, as I understand it, should be for things that are deprecated now. As I understand these, DeprecationWarning means "change your code now or it will break soon", PendingDeprecationWarning means "you're going to have to change something eventually", and FutureWarning means "something in the way you're using this isn't correct, and may lead to failure later."

FutureWarning 还可用于警告您,即使它们将是有效的代码,在将来的更新中它们的行为也将有所不同.这可能与开发人员和用户有关.例如,我在实践中看到的许多 FutureWarning 都是某些便利函数的含义可能会发生变化的事物(就像两个数组的 == 会返回一个数组一样)每个元素的True/False,或者仅当所有元素都相等时才返回True/False,True?当numpy想要更改此值时,它们会生成一个 FutureWarning )

FutureWarning is also used to warn you that things won't behave the same in a future update, even though they will be valid code. This can relevant to both developers and users. For example, many of the FutureWarnings I've seen in practice are things where the meaning of some convenience function may change (like does == for two arrays return an array of True/False for each element, or does it return a single True/False, True only if all the elements are equal? When numpy wants to change this, they make a FutureWarning)

在开发库时,一定要使用这些或它们的子类.如果存在潜在问题(更准确地说,测试工具可能会专门寻找这些问题),那么使用您的库编写代码的人们将期望他们的集成测试会发出 DeprecationWarning s.

In developing your library, definitely use these or subclasses of them. People writing code using your library will expect their integration tests to issue DeprecationWarnings if there's a potential problem (more accurately, testing tools may specifically look for these).

这篇关于DeprecationWarning,PendingDeprecationWarning和FutureWarning之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 11:31