不提高 I201 和 I202 就无法通过 flake8-import-order

Can't pass flake8-import-order without raising I201 and I202

我目前正在为一个很酷的小 python 项目 namey PhotoCollage 做贡献,维护者要求贡献者在考虑拉取请求之前通过 flake8 检查(足够公平)。

我的问题是我无法通过这些检查:在我的计算机上,没有投诉。但是在 Travis 上,我总是 get the following errors:

$ flake8 .
./photocollage/gtkgui.py:29:1: I202 Additional newline in a section of imports.
./photocollage/gtkgui.py:32:1: I202 Additional newline in a section of imports.
./photocollage/render.py:25:1: I202 Additional newline in a section of imports.

The command "flake8 ." exited with 1.

但是,我的代码是这样的:

gtkgui.py

# -*- coding: utf-8 -*-
# Copyright (C) 2013 Adrien Vergé
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import copy
import gettext
from io import BytesIO
import math
import os.path
import random
import sys

import cairo
import gi
gi.require_version('Gtk', '3.0')  # noqa
from gi.repository import Gtk, Gdk, GObject, GdkPixbuf
from six.moves import urllib  # Python 2 backward compatibility

from photocollage import APP_NAME, artwork, collage, render
from photocollage.render import PIL_SUPPORTED_EXTS as EXTS


gettext.textdomain(APP_NAME)
_ = gettext.gettext
_n = gettext.ngettext

(...)

我没有更改进口 wrt。以前的提交,所以我怀疑 flake8-import-order 以某种方式改变了。有什么想法吗?

在您的计算机上获取这些错误

尝试使用 flake8flake8-import-order 的最新版本:

pip3 install --user --upgrade flake8 flake8-import-order

此外,由于这是 Python 3 项目,您是否 运行宁 flake8 与 Python 3?根据您的 OS,运行 的命令可能是:

flake8 .

python3 -m flake8 .

关于这些新错误

你是对的:flake8-import-order 最近似乎有所改变,现在检测到误报。这是由于 gi.repository 设置导入版本的奇怪方式(必须在导入之间调用 gi.require_version())。

我想除了在这些特定行上禁用这些 flake8 规则外,您无能为力:

import gi
gi.require_version('Gtk', '3.0')  # noqa: E402
from gi.repository import Gtk, Gdk, GObject, GdkPixbuf  # noqa: I202