#111 Preventing flake8 E402 warnings
Opened 2 years ago by ferdnyc. Modified 2 years ago

In the README under "Coding style" it asks,

For our Python code base, regarding coding style we largely follow PEP 8 with a maximum line length of 100 characters for code (please wrap doc strings and comments at a "traditional" 72 characters regardless).

We make an exception regarding import order if a specific version or range of a module is required (e.g. for placing calls to gi.require_version() or pkg_resources.require() before the respective import statements). Please add a noqa comment so that flake8 doesn't flag them:

import gi
gi.require_version('Modulemd', '1.0')  # noqa: E402
from gi.repository import Modulemd

Does that actually work? In my experience it doesn't seem to — it's the import lines that need to be excepted since they're the ones that are flagged. So, instead:

import gi
gi.require_version('Modulemd', '1.0')
from gi.repository import Modulemd  # noqa: E402

Additionally, every import statement following the first non-import call has to be protected the same way. Which sucks if you have a lot of imports that use or subclass GI modules, since you really want to set up the GI versioning first. So you end up with this kind of mess:

import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")

from gi.repository import Gtk  # noqa: E402
from gi.repository import Gdk  # noqa: E402

from my.module import AGtkSubclass  # noqa: E402
from my.other.module import (  # noqa: E402
    GdkSubclass1, GdkSubclass2)

(The comment only working if it's on the first line of that last import is probably the most ugly thing about this.)


Login to comment on this ticket.

Metadata