Skip to content

Commit

Permalink
style: Replace black . with ruff format for improved user experie…
Browse files Browse the repository at this point in the history
…nce to match grass repo (#1298)
  • Loading branch information
echoix authored Feb 4, 2025
1 parent 74cefd6 commit e046eb4
Show file tree
Hide file tree
Showing 29 changed files with 235 additions and 270 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/black.yml → .github/workflows/linting.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
name: Python Black Formatting
name: Linting & formatting
on:
push:
branches:
Expand All @@ -13,20 +13,20 @@ concurrency:
${{ github.ref_protected != true }}
env:
# renovate: datasource=python-version depName=python
PYTHON_VERSION: "3.12"
# renovate: datasource=pypi depName=black
BLACK_VERSION: "25.1.0"
PYTHON_VERSION: "3.13"
# renovate: datasource=pypi depName=ruff
RUFF_VERSION: "0.9.4"
permissions: {}
jobs:
run-black:
name: Black formatting
run-ruff:
name: Ruff formatting

# Using matrix just to get variables which are not environmental variables
# and also to sync with other workflows which use matrix.
strategy:
matrix:
include:
- os: ubuntu-22.04
- os: ubuntu-24.04

runs-on: ${{ matrix.os }}
steps:
Expand All @@ -42,15 +42,15 @@ jobs:
- name: Install
run: |
python -m pip install --upgrade pip
pip install black[jupyter]==${{ env.BLACK_VERSION }}
pip install ruff==${{ env.RUFF_VERSION }}
- name: Run Black
- name: Run ruff formatter
run: |
black .
ruff format
- name: Create and uploads code suggestions to apply
id: diff
uses: OSGeo/grass/.github/actions/create-upload-suggestions@main
with:
tool-name: black
tool-name: ruff
# To keep repo's file structure in formatted changes artifact
extra-upload-changes: .clang-format
extra-upload-changes: pyproject.toml
30 changes: 25 additions & 5 deletions .github/workflows/post-pr-reviews.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ name: Post PR code suggestions

on:
workflow_run:
workflows: ["ClangFormat Check", "Python Black Formatting"]
workflows:
["ClangFormat Check", "Python Black Formatting", "Linting & formatting"]
types:
- completed
permissions: {}
Expand Down Expand Up @@ -34,19 +35,20 @@ jobs:
for tool_name in $INPUT_TOOL_NAMES
do
INPUT_TOOL_NAME_FILE="diff-${tool_name}.patch"
echo "Checking if tool ${tool_name} left suggestions in ${INPUT_TOOL_NAME_FILE}"
echo "Checking if tool ${tool_name} left suggestions in ${INPUT_TOOL_NAME_FILE}..."
if [[ -f "${INPUT_TOOL_NAME_FILE}" ]]; then
echo "${INPUT_TOOL_NAME_FILE} was found for tool ${tool_name}"
echo " ${INPUT_TOOL_NAME_FILE} was found for tool ${tool_name}"
echo "$tool_name=true" >> "${GITHUB_OUTPUT}"
else
echo "${INPUT_TOOL_NAME_FILE} was not found for tool ${tool_name}"
echo " ${INPUT_TOOL_NAME_FILE} was not found for tool ${tool_name}"
echo "$tool_name=false" >> "${GITHUB_OUTPUT}"
fi
done
env:
INPUT_TOOL_NAMES: >-
black
clang-format
ruff
- name: Post Black suggestions
if: ${{ steps.tools.outputs.black == 'true' }}
run: |
Expand All @@ -65,7 +67,7 @@ jobs:
CI_REPO_OWNER: ${{ github.event.workflow_run.repository.owner.login }}
CI_REPO_NAME: ${{ github.event.workflow_run.repository.name }}
# CI_PULL_REQUEST: "" # Populated from reviewdog's "-guess" flag since hard to get
- name: Post Clang-format suggestions
- name: Post ClangFormat suggestions
if: ${{ steps.tools.outputs.clang-format == 'true' }}
run: |
TMPFILE="diff-${INPUT_TOOL_NAME}.patch"
Expand All @@ -83,3 +85,21 @@ jobs:
CI_REPO_OWNER: ${{ github.event.workflow_run.repository.owner.login }}
CI_REPO_NAME: ${{ github.event.workflow_run.repository.name }}
# CI_PULL_REQUEST: "" # Populated from reviewdog's "-guess" flag since hard to get
- name: Post Ruff suggestions
if: ${{ steps.tools.outputs.ruff == 'true' }}
run: |
TMPFILE="diff-${INPUT_TOOL_NAME}.patch"
GITHUB_ACTIONS="" reviewdog \
-name="${INPUT_TOOL_NAME:-reviewdog-suggester}" \
-f=diff \
-f.diff.strip=1 \
-filter-mode=nofilter \
-guess \
-reporter="github-pr-review" < "${TMPFILE}"
env:
INPUT_TOOL_NAME: ruff
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CI_COMMIT: ${{ github.event.workflow_run.head_sha }}
CI_REPO_OWNER: ${{ github.event.workflow_run.repository.owner.login }}
CI_REPO_NAME: ${{ github.event.workflow_run.repository.name }}
# CI_PULL_REQUEST: "" # Populated from reviewdog's "-guess" flag since hard to get
2 changes: 1 addition & 1 deletion .github/workflows/super-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
VALIDATE_MARKDOWN_PRETTIER: false # Until issues are fixed
VALIDATE_NATURAL_LANGUAGE: false # Until issues are fixed, lots of valid suggestions
VALIDATE_PERL: false # Until issues are fixed
VALIDATE_PYTHON_BLACK: false # Until code is upgraded to a newer black version
VALIDATE_PYTHON_BLACK: false # We are using ruff format
VALIDATE_PYTHON_FLAKE8: false
VALIDATE_PYTHON_ISORT: false # Until issues are fixed
VALIDATE_PYTHON_MYPY: false # Issue with module name wx.wms
Expand Down
11 changes: 6 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
---
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.9.4
hooks:
# Run the formatter.
- id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
Expand Down Expand Up @@ -27,11 +33,6 @@ repos:
rev: v0.44.0
hooks:
- id: markdownlint-fix
# Using this mirror lets us use mypyc-compiled black, which is about 2x faster
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 25.1.0
hooks:
- id: black-jupyter
- repo: https://github.com/pycqa/flake8
rev: 7.1.1
hooks:
Expand Down
29 changes: 27 additions & 2 deletions src/gui/wxpython/wx.metadata/mdlib/cswlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,20 @@ def __init__(self, parent, main, giface=None):
wx.Panel.__init__(self, parent)

try:
global BBox, CatalogueServiceWeb, Environment, ExceptionReport, FileSystemLoader, GError, GMessage, GUI, GWarning, HtmlFormatter, PropertyIsLike, XmlLexer, highlight
global \
BBox, \
CatalogueServiceWeb, \
Environment, \
ExceptionReport, \
FileSystemLoader, \
GError, \
GMessage, \
GUI, \
GWarning, \
HtmlFormatter, \
PropertyIsLike, \
XmlLexer, \
highlight

from jinja2 import Environment, FileSystemLoader

Expand Down Expand Up @@ -1059,7 +1072,19 @@ class CSWConnectionPanel(wx.Panel):
def __init__(self, parent, main, cswBrowser=True):
wx.Panel.__init__(self, parent)
try:
global BBox, CatalogueServiceWeb, Environment, ExceptionReport, FileSystemLoader, GError, GMessage, GWarning, HtmlFormatter, PropertyIsLike, XmlLexer, highlight
global \
BBox, \
CatalogueServiceWeb, \
Environment, \
ExceptionReport, \
FileSystemLoader, \
GError, \
GMessage, \
GWarning, \
HtmlFormatter, \
PropertyIsLike, \
XmlLexer, \
highlight

from jinja2 import Environment, FileSystemLoader

Expand Down
11 changes: 10 additions & 1 deletion src/gui/wxpython/wx.metadata/mdlib/mdeditorfactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,16 @@ def __init__(self, parent, profilePath, xmlMdPath, templateEditor=False):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

try:
global CI_Date, CI_OnlineResource, CI_ResponsibleParty, DQ_DataQuality, EX_Extent, EX_GeographicBoundingBox, GError, MD_Distribution, MD_ReferenceSystem
global \
CI_Date, \
CI_OnlineResource, \
CI_ResponsibleParty, \
DQ_DataQuality, \
EX_Extent, \
EX_GeographicBoundingBox, \
GError, \
MD_Distribution, \
MD_ReferenceSystem

from owslib.iso import (
CI_Date,
Expand Down
14 changes: 13 additions & 1 deletion src/gui/wxpython/wx.metadata/mdlib/mdgrass.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,19 @@ class GrassMD:

def __init__(self, map, type):
try:
global CI_Date, CI_OnlineResource, CI_ResponsibleParty, DQ_DataQuality, Environment, etree, EX_Extent, EX_GeographicBoundingBox, FileSystemLoader, MD_Distribution, MD_ReferenceSystem, RunCommand
global \
CI_Date, \
CI_OnlineResource, \
CI_ResponsibleParty, \
DQ_DataQuality, \
Environment, \
etree, \
EX_Extent, \
EX_GeographicBoundingBox, \
FileSystemLoader, \
MD_Distribution, \
MD_ReferenceSystem, \
RunCommand

from owslib.iso import (
CI_Date,
Expand Down
19 changes: 17 additions & 2 deletions src/gui/wxpython/wx.metadata/mdlib/mdpdftheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ class MySheet:

def __init__(self):
try:
global ParagraphStyle, TA_CENTER, _baseFontName, _baseFontNameB, _baseFontNameBI, _baseFontNameI, colors, inch
global \
ParagraphStyle, \
TA_CENTER, \
_baseFontName, \
_baseFontNameB, \
_baseFontNameBI, \
_baseFontNameI, \
colors, \
inch

from reportlab.lib.styles import (
ParagraphStyle,
Expand Down Expand Up @@ -351,7 +359,14 @@ class Pdf(object):

def __init__(self, title, author):
try:
global Image, KeepTogether, LongTable, Paragraph, SimpleDocTemplate, Spacer, Table
global \
Image, \
KeepTogether, \
LongTable, \
Paragraph, \
SimpleDocTemplate, \
Spacer, \
Table

from reportlab.platypus.doctemplate import SimpleDocTemplate
from reportlab.platypus.flowables import Image
Expand Down
4 changes: 3 additions & 1 deletion src/gui/wxpython/wx.mwprecip/mw3.py
Original file line number Diff line number Diff line change
Expand Up @@ -1783,11 +1783,13 @@ def firstPreparation(self):
self.connection.executeSql(sql, False, True)

logger.info("Computing column lenght")
sql = "UPDATE link SET lenght = get_earth_distance1(n1.long,n1.lat,n2.long,n2.lat) \
sql = (
"UPDATE link SET lenght = get_earth_distance1(n1.long,n1.lat,n2.long,n2.lat) \
FROM node AS n1 JOIN \
link AS l ON n1.nodeid = fromnodeid \
JOIN node AS n2 ON n2.nodeid = tonodeid \
WHERE link.linkid = l.linkid; "
)
self.connection.executeSql(sql, False, True)

# logger.info("Add column precip")
Expand Down
26 changes: 13 additions & 13 deletions src/imagery/i.ann.maskrcnn/maskrcnnlib/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1527,10 +1527,10 @@ def build_detection_targets(rpn_rois, gt_class_ids, gt_boxes, gt_masks, config):
# Fill the rest with repeated bg rois.
keep_extra_ids = np.random.choice(keep_bg_ids, remaining, replace=True)
keep = np.concatenate([keep, keep_extra_ids])
assert (
keep.shape[0] == config.TRAIN_ROIS_PER_IMAGE
), "keep doesn't match ROI batch size {}, {}".format(
keep.shape[0], config.TRAIN_ROIS_PER_IMAGE
assert keep.shape[0] == config.TRAIN_ROIS_PER_IMAGE, (
"keep doesn't match ROI batch size {}, {}".format(
keep.shape[0], config.TRAIN_ROIS_PER_IMAGE
)
)

# Reset the gt boxes assigned to BG ROIs.
Expand Down Expand Up @@ -2876,9 +2876,9 @@ def detect(self, images, verbosity=0):
verbose form in case of verbosity == 3, not 1
"""
assert self.mode == "inference", "Create model in inference mode."
assert (
len(images) == self.config.BATCH_SIZE
), "len(images) must be equal to BATCH_SIZE"
assert len(images) == self.config.BATCH_SIZE, (
"len(images) must be equal to BATCH_SIZE"
)

if verbosity == 3:
log("Processing {} images".format(len(images)))
Expand All @@ -2892,9 +2892,9 @@ def detect(self, images, verbosity=0):
# All images in a batch MUST be of the same size
image_shape = molded_images[0].shape
for g in molded_images[1:]:
assert (
g.shape == image_shape
), "After resizing, all images must have the same size. Check IMAGE_RESIZE_MODE and image sizes."
assert g.shape == image_shape, (
"After resizing, all images must have the same size. Check IMAGE_RESIZE_MODE and image sizes."
)

# Anchors
anchors = self.get_anchors(image_shape)
Expand Down Expand Up @@ -2950,9 +2950,9 @@ def detect_molded(self, molded_images, image_metas, verbose=0):
masks: [H, W, N] instance binary masks
"""
assert self.mode == "inference", "Create model in inference mode."
assert (
len(molded_images) == self.config.BATCH_SIZE
), "Number of images must be equal to BATCH_SIZE"
assert len(molded_images) == self.config.BATCH_SIZE, (
"Number of images must be equal to BATCH_SIZE"
)

if verbose:
log("Processing {} images".format(len(molded_images)))
Expand Down
6 changes: 3 additions & 3 deletions src/imagery/i.eodag/i.eodag.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,9 +1313,9 @@ def main():
]
)
else:
dag.providers_config["creodias"].auth.credentials[
"totp"
] = creodias_otp
dag.providers_config["creodias"].auth.credentials["totp"] = (
creodias_otp
)
dag._plugins_manager.get_auth_plugin("creodias").authenticate()
custom_config = {
"timeout": int(options["timeout"]),
Expand Down
4 changes: 1 addition & 3 deletions src/imagery/i.landsat/i.landsat.qa/i.landsat.qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ def check_user_input(user_input):
grass.fatal(
_(
"""All conditions for {} specified as
unacceptable, this will result in an empty map.""".format(
o
)
unacceptable, this will result in an empty map.""".format(o)
)
)
# Check if valid combination of options if provided
Expand Down
6 changes: 3 additions & 3 deletions src/imagery/i.landsat8.swlst/split_window_lst.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ def __init__(self, landcover):

if landcover in EMISSIVITIES.keys() or landcover == "Random":
# a fixed land cover class requested
assert self._landcover_string_validity(
landcover_class
), "Unknown land cover class name!"
assert self._landcover_string_validity(landcover_class), (
"Unknown land cover class name!"
)
self.landcover_class = landcover

# retrieve & set avg emissivities for channels t10, t11
Expand Down
Loading

0 comments on commit e046eb4

Please sign in to comment.