Skip to content

Commit

Permalink
style: use ruff for linting instead of flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
rnousia committed Oct 17, 2023
1 parent 39fd995 commit dc64811
Show file tree
Hide file tree
Showing 22 changed files with 224 additions and 140 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ __pycache__
*.coverage
*.pyc
*.egg-info
.ruff_cache

*logs/
*.log
Expand Down
17 changes: 8 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@ repos:
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.292
hooks:
- id: isort
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- repo: https://github.com/psf/black
rev: 22.3.0
rev: 23.7.0
hooks:
- id: black
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.931
rev: v1.4.1
hooks:
- id: mypy
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear==22.1.11
- pep8-naming==0.12.1
- flake8-annotations==2.7.0
- flake8-pyproject==1.2.3
- flake8-qgis==1.0.0
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v2.1.1
Expand Down
8 changes: 3 additions & 5 deletions pickLayer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-

# noqa: N999
# Copyright (C) 2014-2019 Enrico Ferreguti (enricofer@gmail.com)
# Copyright (C) 2021-2022 National Land Survey of Finland
# Copyright (C) 2021-2023 National Land Survey of Finland
# (https://www.maanmittauslaitos.fi/en).
#
#
Expand All @@ -21,8 +20,7 @@
# along with PickLayer. If not, see <https://www.gnu.org/licenses/>.


# noinspection PyPep8Naming
def classFactory(iface): # noqa N802
def classFactory(iface): # noqa: ANN201, ANN001, N802
"""Load pickLayer class from file picklayer.
:param iface: A QGIS interface instance.
Expand Down
3 changes: 2 additions & 1 deletion pickLayer/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# noqa: N999
# Copyright (C) 2014-2019 Enrico Ferreguti (enricofer@gmail.com)
# Copyright (C) 2021-2022 National Land Survey of Finland
# Copyright (C) 2021-2023 National Land Survey of Finland
# (https://www.maanmittauslaitos.fi/en).
#
#
Expand Down
6 changes: 3 additions & 3 deletions pickLayer/core/identifygeometry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (C) 2014-2019 Enrico Ferreguti (enricofer@gmail.com)
# Copyright (C) 2021-2022 National Land Survey of Finland
# Copyright (C) 2021-2023 National Land Survey of Finland
# (https://www.maanmittauslaitos.fi/en).
#
#
Expand Down Expand Up @@ -37,14 +37,14 @@ class IdentifyGeometry(QgsMapToolIdentify):
geom_identified = pyqtSignal(QgsVectorLayer, QgsFeature)

def __init__(
self, canvas: QgsMapCanvas, layerType: str = "AllLayers" # noqa N803
self, canvas: QgsMapCanvas, layerType: str = "AllLayers" # noqa: N803
) -> None:
self.layer_type = getattr(QgsMapToolIdentify, layerType)
self.canvas = canvas
QgsMapToolIdentify.__init__(self, canvas)
self.setCursor(QCursor())

def canvasReleaseEvent(self, mouse_event) -> None: # noqa N802
def canvasReleaseEvent(self, mouse_event) -> None: # noqa: ANN001, N802
orig_search_radius = Settings.identify_tool_search_radius.get()
try:
search_radius = Settings.search_radius.get()
Expand Down
14 changes: 6 additions & 8 deletions pickLayer/core/picklayer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-

# Copyright (C) 2014-2019 Enrico Ferreguti (enricofer@gmail.com)
# Copyright (C) 2021-2022 National Land Survey of Finland
# Copyright (C) 2021-2023 National Land Survey of Finland
# (https://www.maanmittauslaitos.fi/en).
#
#
Expand Down Expand Up @@ -82,22 +80,22 @@ def transform_to_wgs84(self, p_point: core.QgsPointXY, srs: int) -> core.QgsPoin

def populate_attributes_menu(self, attribute_menu: QtWidgets.QMenu) -> None:
field_names = [field.name() for field in self.selected_layer.fields()]
for n in range(0, len(field_names)):
for n in range(len(field_names)):
field_name = field_names[n]
attribute_value = self.selected_feature.attributes()[n]
try: # cut long strings
self.attribute_action = attribute_menu.addAction(
"%s: %s" % (field_name, attribute_value[:40])
f"{field_name}: {attribute_value[:40]}"
)
except Exception:
self.attribute_action = attribute_menu.addAction(
"%s: %s" % (field_name, attribute_value)
f"{field_name}: {attribute_value}"
)
self.attribute_action.triggered.connect(
partial(self.copy_to_clipboard, attribute_value)
)

def context_menu_request(self) -> None:
def context_menu_request(self) -> None: # noqa: C901, PLR0915
context_menu = QtWidgets.QMenu()
self.clipboard_layer_action = context_menu.addAction(
tr("Layer: {}", self.selected_layer.name())
Expand Down Expand Up @@ -426,7 +424,7 @@ def paste_geom_func(self) -> None:
self.selected_layer.triggerRepaint()

def paste_attrs_func(self) -> None:
for attr_id in range(0, len(self.clip_attrs_values)):
for attr_id in range(len(self.clip_attrs_values)):
if self.selected_layer.pendingFields().field(
str(self.clip_attrs_fieldnames[attr_id])
):
Expand Down
20 changes: 8 additions & 12 deletions pickLayer/core/set_active_layer_tool.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-

# Copyright (C) 2022 National Land Survey of Finland
# Copyright (C) 2023 National Land Survey of Finland
# (https://www.maanmittauslaitos.fi/en).
#
#
Expand All @@ -19,7 +17,7 @@
# You should have received a copy of the GNU General Public License
# along with PickLayer. If not, see <https://www.gnu.org/licenses/>.
import logging
from typing import List, Optional
from typing import Optional

from qgis.core import (
QgsFeature,
Expand Down Expand Up @@ -66,9 +64,9 @@ def __init__(
super().__init__(canvas)
self.setCursor(QCursor())
self.previous_map_tool: Optional[QgsMapTool] = None
self.search_layer_ids: Optional[List[str]] = None
self.search_layer_ids: Optional[list[str]] = None

def canvasReleaseEvent(self, mouse_event: QgsMapMouseEvent) -> None: # noqa N802
def canvasReleaseEvent(self, mouse_event: QgsMapMouseEvent) -> None: # noqa: N802
try:
self.set_active_layer_using_closest_feature(
self.toMapCoordinates(QPoint(mouse_event.x(), mouse_event.y()))
Expand All @@ -82,9 +80,8 @@ def set_active_layer_using_closest_feature(
self,
location: QgsPointXY,
search_radius: Optional[float] = None,
search_layer_ids: Optional[List[str]] = None,
search_layer_ids: Optional[list[str]] = None,
) -> None:

if search_radius is None:
search_radius = self._get_default_search_radius()

Expand All @@ -101,8 +98,8 @@ def set_active_layer_using_closest_feature(
self._activate_layer_and_previous_map_tool(layer_to_activate)

def _get_identify_results(
self, location: QgsPointXY, search_layer_ids: Optional[List[str]] = None
) -> List[QgsMapToolIdentify.IdentifyResult]:
self, location: QgsPointXY, search_layer_ids: Optional[list[str]] = None
) -> list[QgsMapToolIdentify.IdentifyResult]:
layers = []
if search_layer_ids:
for layer_id in search_layer_ids:
Expand Down Expand Up @@ -161,10 +158,9 @@ def _get_distance_to_feature_on_layer(

def _choose_layer_from_identify_results(
self,
results: List[QgsMapToolIdentify.IdentifyResult],
results: list[QgsMapToolIdentify.IdentifyResult],
origin_map_coordinates: QgsPointXY,
) -> Optional[QgsMapLayer]:

geom_type_preference = {
QgsWkbTypes.PointGeometry: 1,
QgsWkbTypes.LineGeometry: 2,
Expand Down
3 changes: 2 additions & 1 deletion pickLayer/definitions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# noqa: N999
# Copyright (C) 2014-2019 Enrico Ferreguti (enricofer@gmail.com)
# Copyright (C) 2021-2022 National Land Survey of Finland
# Copyright (C) 2021-2023 National Land Survey of Finland
# (https://www.maanmittauslaitos.fi/en).
#
#
Expand Down
4 changes: 2 additions & 2 deletions pickLayer/definitions/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (C) 2014-2019 Enrico Ferreguti (enricofer@gmail.com)
# Copyright (C) 2021-2022 National Land Survey of Finland
# Copyright (C) 2021-2023 National Land Survey of Finland
# (https://www.maanmittauslaitos.fi/en).
#
#
Expand Down Expand Up @@ -29,7 +29,7 @@ class Settings(enum.Enum):
# No default value, if this is not set, use the same value as identify tool
search_radius = -1.0

def get(self, typehint: type = str) -> Any:
def get(self, typehint: type = str) -> Any: # noqa: ANN401
"""Gets the value of the setting"""
if self == Settings.identify_tool_search_radius:
value = get_setting(self.value, internal=False)
Expand Down
17 changes: 8 additions & 9 deletions pickLayer/plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2021-2022 National Land Survey of Finland
# Copyright (C) 2021-2023 National Land Survey of Finland
# (https://www.maanmittauslaitos.fi/en).
#
#
Expand All @@ -18,7 +18,7 @@
# along with PickLayer. If not, see <https://www.gnu.org/licenses/>.

import logging
from typing import Callable, List, Optional
from typing import Callable, Optional

from qgis.core import QgsApplication, QgsPointXY
from qgis.gui import QgsGui, QgsMapTool
Expand Down Expand Up @@ -54,7 +54,7 @@ def __init__(self) -> None:
else:
pass

self.actions: List[QAction] = []
self.actions: list[QAction] = []
self.toolbar: Optional[QToolBar] = None
self.menu = plugin_name()
self.pick_layer_tool: Optional[PickLayer] = None
Expand All @@ -71,7 +71,7 @@ def get_set_active_layer_tool_action(self) -> QAction:
return self.set_active_layer_action

def set_search_layers_for_set_active_layer_tool(
self, search_layers: Optional[List[str]] = None
self, search_layers: Optional[list[str]] = None
) -> None:
"""
Public method for setting search layers for set active layer tool.
Expand All @@ -89,7 +89,7 @@ def set_active_layer_using_closest_feature(
self,
point_xy: QgsPointXY,
search_radius: Optional[float] = None,
search_layers: Optional[List[str]] = None,
search_layers: Optional[list[str]] = None,
) -> None:
"""
Public method for setting layer active based on given map coordinates.
Expand All @@ -110,7 +110,7 @@ def set_active_layer_using_closest_feature(
point_xy, search_radius, search_layers
)

def initGui(self) -> None: # noqa N802
def initGui(self) -> None: # noqa: N802
"""Create the menu entries and toolbar icons inside the QGIS GUI."""

self.toolbar = iface.addToolBar(plugin_name())
Expand Down Expand Up @@ -147,9 +147,8 @@ def initGui(self) -> None: # noqa N802

self.set_active_layer_tool.setAction(self.set_active_layer_action)

def onClosePlugin(self) -> None: # noqa N802
def onClosePlugin(self) -> None: # noqa: N802
"""Cleanup necessary items here when plugin dockwidget is closed"""
pass

def unload(self) -> None:
"""Removes the plugin menu item and icon from QGIS GUI."""
Expand All @@ -163,7 +162,7 @@ def unload(self) -> None:
# Remove toolbar from QGIS by deleting it
del self.toolbar

def _add_action(
def _add_action( # noqa: PLR0913
self,
icon_path: str,
text: str,
Expand Down
3 changes: 2 additions & 1 deletion pickLayer/ui/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (C) 2021-2022 National Land Survey of Finland
# noqa: N999
# Copyright (C) 2021-2023 National Land Survey of Finland
# (https://www.maanmittauslaitos.fi/en).
#
#
Expand Down
43 changes: 43 additions & 0 deletions picklayer.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"extensions": {
"recommendations": [
"editorconfig.editorconfig",
"mikestead.dotenv",
"ms-python.python",
"ms-python.mypy-type-checker",
"ms-python.black-formatter",
"ms-python.flake8",
"charliermarsh.ruff",
]
},
"folders": [
{
"path": "."
}
],
"settings": {
"python.languageServer": "Pylance",
// Tests
"python.testing.pytestEnabled": true,
// Linting
"flake8.importStrategy": "fromEnvironment",
"ruff.importStrategy": "fromEnvironment",
"mypy-type-checker.importStrategy": "fromEnvironment",
// Formatting
"isort.check": false,
"black-formatter.importStrategy": "fromEnvironment",
"[python]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports.ruff": true,
"source.fixAll": true
},
"editor.defaultFormatter": "ms-python.black-formatter",
},
"files.associations": {
"*.ts": "xml"
},
"editor.bracketPairColorization.enabled": true,
"editor.guides.bracketPairs": "active",
}
}
Loading

0 comments on commit dc64811

Please sign in to comment.