简体   繁体   English

“if False:”在这里的目的是什么?

[英]What is the purpose of "if False:" here?

https://github.com/asottile/pyupgrade/blob/fecacc91e57c224a0bd2564579ef01238650126c/pyupgrade.py#L53 https://github.com/asottile/pyupgrade/blob/fecacc91e57c224a0bd2564579ef01238650126c/pyupgrade.py#L53

if False:  # pragma: no cover (mypy)
    from typing import Type
    if sys.version_info >= (3,):
        AsyncFunctionDef = ast.AsyncFunctionDef
    else:
        AsyncFunctionDef = ast.stmt

The commit is not revealing: https://github.com/asottile/pyupgrade/commit/fecacc91e57c224a0bd2564579ef01238650126c#diff-8213eba6a28bcc759225cd8cf49b2fd1提交没有透露: https : //github.com/asottile/pyupgrade/commit/fecacc91e57c224a0bd2564579ef01238650126c#diff-8213eba6a28bcc759225cd8cf49b2fd1

False can be truthy in Python 2 (where it can be re-defined) but not in Python 3. It might be a joke, or work in progress, or way of commenting out the code, but this is quite a mature tool- am I missing something? False在 Python 2(可以重新定义)中是真实的,但在 Python 3 中不是。它可能是一个笑话,或者正在进行中,或者注释掉代码的方式,但这是一个非常成熟的工具 - 我我错过了什么?

The value of AsyncFunctionDef is never needed at runtime, only by mypy in two Python-2-compatible type hints (at lines 1298 and 1318).在运行时永远不需要AsyncFunctionDef的值,只有在两个 Python-2 兼容类型提示中的mypy才需要(在第 1298 和 1318 行)。 The if False prevents the assignments from occurring at run-time, but lets mypy see the correct underlying type to use during type-checking. if False阻止分配在运行时发生,但让mypy看到在类型检查期间使用的正确基础类型。 (It also prevents an ImportError from being raised at the attempt to import the typing module under Python 2.) (它还可以防止在尝试在 Python 2 下导入typing模块时引发ImportError 。)

It would be clearer to use typing.TYPE_CHECKING (whose value is False at run-time, but True when mypy runs) here, except typing.TYPE_CHECKING is also unavailable in Python 2.在这里使用typing.TYPE_CHECKING (其值在运行时为False ,但在mypy运行时mypy True )会更清晰,除了typing.TYPE_CHECKING在 Python 2 中也不可用。

if False is used over if typing.TYPE_CHECKING because python3.5.0-3.5.2 is supported by pyupgrade and does not have typing.TYPE_CHECKING -- you can find more information in flake8-typing-imports (I'm also the author of this flake8 plugin) if False用于if typing.TYPE_CHECKING因为 pyupgrade 支持 python3.5.0-3.5.2 并且没有typing.TYPE_CHECKING你可以在flake8-typing-imports 中找到更多信息(我也是这个的作者flake8 插件)

in 3.5.3+ and the typing backport (available in anything <3.5) TYPE_CHECKING is available在 3.5.3+ 和typing backport(在任何 <3.5 中可用) TYPE_CHECKING可用

disclaimer: I'm the author of pyupgrade免责声明:我是 pyupgrade 的作者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM