【问题标题】:How to tell futurize (python-future) to not touch a specific line?如何告诉 futurize (python-future) 不要触及特定行?
【发布时间】:2019-12-21 06:05:54
【问题描述】:

我正在尝试使用python-future 将 Python 2.7 代码库转换为 3.x。有时我需要保留一小部分特定于 Python-2 的代码,直到迁移完成。例如,

import sys

def f(x):
    if sys.version_info[0] == 2:
        if type(x) == unicode:  # futurize: please don't change this line.
            call_some_py2_specific_logic()
    the_rest_of_logic()

但是,如果我通过futurize 运行此代码,它会将unicode 替换为str。有什么方法可以标记该行以便futurize“理解”我上面写的评论?

【问题讨论】:

    标签: python-3.x python-2.7


    【解决方案1】:

    尚未(截至 2022 年 1 月),尽管使用 -x libfuturize.fixers.fix_unicode_keep_u 参数运行 futurize 将跳过 unicode 替换修复程序,如果您将其作为 CI/CD 管道的一部分运行以防止回归,这可能很有用。

    另一种选择是将输出包装在一个脚本中,该脚本检查您的魔术注释是否存在于 futurize 的输出中,如果存在则忽略它(您可能希望将其写成答案,因为您写了一个!)

    【讨论】:

    • 唉,我已经搬到另一个地方,我不再有这个脚本,但想法和你的建议基本相同。
    猜你喜欢
    • 1970-01-01
    • 2013-12-28
    • 2010-12-19
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 2011-02-23
    相关资源
    最近更新 更多