Skip to main content

Flake8 plugin for QGIS python plugins.

Project description

flake8-qgis

PyPI version Downloads CI Code on Github Code style: black Imports: isort pre-commit

A flake8 plugin for QGIS3 python plugins written in Python.

Made with Cookiecutter template cookiecutter-flake8-plugin. Inspired by flake8-simplify.

Installation

Install with pip:

pip install flake8-qgis

Usage

Just call flake8 . in your package or flake your.py.

Rules

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

QGS101

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.

Why is this bad?

Protected members are potentially unstable across software versions. Future changes in protected members might cause problems.

Example

# Bad
from qgis._core import QgsMapLayer, QgsVectorLayer
from qgis._core import QgsApplication

# Good
from qgis.core import QgsMapLayer, QgsVectorLayer
from qgis.core import QgsApplication

QGS102

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.

Why is this bad?

Protected members are potentially unstable across software versions. Future changes in protected members might cause problems.

Example

# Bad
import qgis._core.QgsVectorLayer as QgsVectorLayer

# Good
import qgis.core.QgsVectorLayer as QgsVectorLayer

QGS103

Avoid using from-imports from PyQt directly

Why is this bad?

Importing directly from PyQt might create conflict with QGIS bundled PyQt version

Example

# Bad
from PyQt5.QtCore import pyqtSignal

# Good
from qgis.PyQt.QtCore import pyqtSignal

QGS104

Avoid using imports from PyQt directly

Why is this bad?

Importing directly from PyQt might create conflict with QGIS bundled PyQt version

Example

# Bad
import PyQt5.QtCore.pyqtSignal as pyqtSignal

# Good
import qgis.PyQt.QtCore.pyqtSignal as pyqtSignal

QGS105

Avoid passing QgisInterface as an argument

Why is this bad?

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.

Example

# 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

QGS106

Avoid importing gdal directly, import it from osgeo package

Why is this bad?

Importing directly from gdal might create conflict with different gdal versions

Example

# Bad
import gdal
import ogr

# Good
from osgeo import gdal

Development

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

Updating dependencies

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8_qgis-1.1.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flake8_qgis-1.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file flake8_qgis-1.1.0.tar.gz.

File metadata

  • Download URL: flake8_qgis-1.1.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for flake8_qgis-1.1.0.tar.gz
Algorithm Hash digest
SHA256 1c689040eee52edb4c8636167a0bde792a8bfc0252e5ae702ff86a88438096cb
MD5 192f2600229bb10269c4a76850141822
BLAKE2b-256 f778d889b892ce22c0ad10e3bfeff416a0eebc6918a638b4a3abb094c91c43c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for flake8_qgis-1.1.0.tar.gz:

Publisher: release.yml on GispoCoding/flake8-qgis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file flake8_qgis-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: flake8_qgis-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for flake8_qgis-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c29e0544ebe4ce3e0dea6f05442e29b03c70aa33d59d451394772b0b4c34a105
MD5 fddd4f2237d7469d3bfdeee959f2c13c
BLAKE2b-256 9d332a5d9cf751d6a3e5b0c04ef9d0b46768d90f05eb856cd2f1b02c80c3a749

See more details on using hashes here.

Provenance

The following attestation bundles were made for flake8_qgis-1.1.0-py3-none-any.whl:

Publisher: release.yml on GispoCoding/flake8-qgis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page