python - flake8 - E999 语法错误与 python3 元类参数

标签 python metaclass flake8

我正在使用 vim 以 flake8 作为 linter 进行 python 开发。下面是包含元类的示例代码。 Flake8 在 class Spam(metaclass=MyMeta) 行上显示错误 E999 SyntaxError: invalid syntax (E)。我正在使用 python3,这是在 python3 中指定自定义元类的正确语法。

class MyMeta(type):

    def __new__(cls, clsname, bases, clsbody):
        upper_case = {}
        for k, v in clsbody.items():
            if not k.startswith('__'):
                upper_case[k.upper()] = v
        return super().__new__(cls, clsname, bases, upper_case)


class Spam(metaclass=MyMeta):
    foo = 'bar'

有办法解决这个问题吗?

最佳答案

好吧,您正在编辑 Python3 代码,而您的 flake8 显然正在检查 Python2 的语法。

环顾互联网,强制 flake8 检查 Python3 的直接方法是从 Python3 运行它。

您可能在 Linux 或其他 Unix 上(我从 VIM 用法中得到提示),因此,如果系统范围内安装了 flake8,请卸载它,然后将其安装在 Python3 中(在 fedora 和 redhatish 发行版中,它是 dnf uninstall python2-flake8 dnf install python3-flake8).

更合适的方法可能是设置一个 virtualenv对于你的 Python 项目,使用所需的 Python 版本,在这个 virtualenv 中安装 flake8,使用 pip install flake8从你的 virtualenv 中启动 VIM,这样任何 Python 脚本或者它运行的程序在同一个环境中,甚至高级自动完成之类的东西也可以检查你的项目实际使用的库。

关于python - flake8 - E999 语法错误与 python3 元类参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52715767/

相关文章:

python - 结合 Qgraphics 和 sqlalchemy 时出现元类错误

python - 如何在没有元类冲突的情况下将泛型类型与 PyQt 子类一起使用?

python - 更新子类中的父字典

python - Vim Flake8 忽略项目配置文件

python - 如何将这个被黑在一起的 pandas 数据框子集化?

python - 在 Python 中读取文件时有没有办法跳到特定的行号?

python - 如何在 Sphinx 中交叉引用 autodoc 生成的函数?

python - flake8:仅忽略整个文件中的 F401 规则

python - Flake8 错误 : E712 comparison to True should be 'if cond is True:' or 'if cond:'

Python:连接四个交替的转弯