
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
A flake8 plugin for QGIS3 python plugins written in Python.
Made with Cookiecutter template cookiecutter-flake8-plugin. Inspired by flake8-simplify.
Install with pip
:
pip install flake8-qgis
Just call flake8 .
in your package or flake your.py
.
Rule | Description |
---|---|
QGS101 | Avoid using from-imports from qgis protected members |
QGS102 | Avoid using imports from qgis protected members |
QGS103 | Avoid using from-imports from PyQt directly |
QGS104 | Avoid using imports from PyQt directly |
QGS105 | Avoid passing QgisInterface as an argument |
QGS106 | Avoid importing gdal directly, import it from osgeo package |
Please check the Examples section below for good and bad usage examples for each rule.
While it's important to adhere to these rules, there might be good reasons to ignore some of them. You can do so by using the standard Flake8 configuration. For example, in the setup.cfg
file:
[flake8]
ignore = QGS101, QGS102
Avoid using from-imports from qgis protected members
An exception is made for importing qgis._3d
(since flake-qgis 1.1.0). The underscore in the package name is used to prevent the name from starting with a number, ensuring it is a valid package name.
Protected members are potentially unstable across software versions. Future changes in protected members might cause problems.
# Bad
from qgis._core import QgsMapLayer, QgsVectorLayer
from qgis._core import QgsApplication
# Good
from qgis.core import QgsMapLayer, QgsVectorLayer
from qgis.core import QgsApplication
Avoid using imports from qgis protected members
An exception is made for importing qgis._3d
(since flake-qgis 1.1.0). The underscore in the package name is used to prevent the name from starting with a number, ensuring it is a valid package name.
Protected members are potentially unstable across software versions. Future changes in protected members might cause problems.
# Bad
import qgis._core.QgsVectorLayer as QgsVectorLayer
# Good
import qgis.core.QgsVectorLayer as QgsVectorLayer
Avoid using from-imports from PyQt directly
Importing directly from PyQt might create conflict with QGIS bundled PyQt version
# Bad
from PyQt5.QtCore import pyqtSignal
# Good
from qgis.PyQt.QtCore import pyqtSignal
Avoid using imports from PyQt directly
Importing directly from PyQt might create conflict with QGIS bundled PyQt version
# Bad
import PyQt5.QtCore.pyqtSignal as pyqtSignal
# Good
import qgis.PyQt.QtCore.pyqtSignal as pyqtSignal
Avoid passing QgisInterface as an argument
It is much easier to import QgisInterface, and it's easier to mock it as well when writing tests. This approach is not however documented properly, so the API might change at some point to exclude this.
This rule can be excluded safely since this is only a matter of preference. Passing iface as an argument is the documented way of getting QgisInterface in plugins. However, it requires writing more code.
# Bad: iface passed as argument
def some_function(somearg, iface):
# do something with iface
# Good: iface imported
from qgis.utils import iface
def some_function(somearg):
# do something with iface
# in classFactory the passing is OK, since QGIS injects it
def classFactory(iface):
# preferably do not pass the iface to plugin
Avoid importing gdal directly, import it from osgeo package
Importing directly from gdal might create conflict with different gdal versions
# Bad
import gdal
import ogr
# Good
from osgeo import gdal
Install development dependencies
python -m venv .venv
# activate the venv
python -m pip install -U pip
pip install pip-tools
pip-sync requirements.txt requirements-dev.txt requirements-lint.txt
Edit .in
dependency files then run
uv pip compile --universal --python 3.9 setup.cfg -o requirements.txt
uv pip compile --universal --python 3.9 requirements-dev.in -o requirements-dev.txt
uv pip compile --universal --python 3.9 requirements-lint.in -o requirements-lint.txt
FAQs
Flake8 plugin for QGIS python plugins.
We found that flake8-qgis demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.