Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre-commit for code linting #221

Merged
merged 12 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[flake8]
# https://pep8.readthedocs.io/en/latest/intro.html#error-codes
ignore =
# line break after operator
W504
# Max width of Github code review is 79 characters
max-line-length = 79
max-complexity = 18
per-file-ignores =
*/__init__.py: F401
exclude =
.git,
docs,
.idea,
.mypy_cache,
.pytest_cache,
28 changes: 28 additions & 0 deletions .github/workflows/build_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,34 @@ env:
PATHS_IGNORE: '["**/README.md", "**/docs/**"]'

jobs:
pre-commit-hooks:
name: lint with pre-commit
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- id: skip_check
uses: fkirc/skip-duplicate-actions@master
with:
cancel_others: ${{ env.CANCEL_OTHERS }}
paths_ignore: ${{ env.PATHS_IGNORE }}

- if: ${{ steps.skip_check.outputs.should_skip != 'true' }}
name: Checkout Code Repository
uses: actions/checkout@v4

- if: ${{ steps.skip_check.outputs.should_skip != 'true' }}
name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: "3.10"

- if: ${{ steps.skip_check.outputs.should_skip != 'true' }}
# Run all pre-commit hooks on all the files.
# Getting only staged files can be tricky in case a new PR is opened
# since the action is run on a branch in detached head state
name: Install and Run Pre-commit
uses: pre-commit/[email protected]

build:
name: test geometric_features - python ${{ matrix.python-version }}
runs-on: ubuntu-latest
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/docs_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:
release:
types: [published]

env:
PYTHON_VERSION: "3.10"

jobs:
publish-docs:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -40,14 +43,14 @@ jobs:
channels: conda-forge
channel-priority: strict
auto-update-conda: true
python-version: ${{ matrix.python-version }}
python-version: ${{ env.PYTHON_VERSION }}

- if: ${{ steps.skip_check.outputs.should_skip != 'true' }}
name: Install geometric_features
run: |
git config --global url."https://github.com/".insteadOf "[email protected]:"
conda create -n geometric_features_dev --file dev-spec.txt \
python=${{ matrix.python-version }}
python=${{ env.PYTHON_VERSION }}
conda activate geometric_features_dev
python -m pip install -vv --no-deps --no-build-isolation -e .

Expand Down
44 changes: 44 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
exclude: "docs|.git|geometric_data|geometric_features/features_and_tags.json"
default_stages: [pre-commit]
fail_fast: true

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer

# Can run individually with `pre-commit run isort --all-files`
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
exclude: "geometric_features/__init__.py"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xylar isort reordering the imports for this file broke everything, so I've excluded it here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's follow up on that in a separate PR. We don't need all the __future__ stuff anymore. That was for python 2. Maybe that will help. But if that's not the problem, we maybe need to add missing imports somewhere else.


# Can run individually with `flynt [file]` or `flynt [source]`
- repo: https://github.com/ikamensh/flynt
rev: '1.0.1'
hooks:
- id: flynt
args: ["--fail-on-change", "--verbose"]
require_serial: true

# Can run individually with `pre-commit run flake8 --all-files`
# Need to use flake8 GitHub mirror due to CentOS git issue with GitLab
# https://github.com/pre-commit/pre-commit/issues/1206
- repo: https://github.com/pycqa/flake8
rev: 7.1.1
hooks:
- id: flake8
args: ["--config=.flake8"]
additional_dependencies: [flake8-isort]

# Can run individually with `pre-commit run mypy --all-files`
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.13.0
hooks:
- id: mypy
args: ["--config=pyproject.toml", "--show-error-codes"]
verbose: true
additional_dependencies: ['types-requests', 'types-PyYAML']
xylar marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Copyright (c) 2013-2018, Los Alamos National Security, LLC (LANS) (Ocean: LA-CC-13-047;
Land Ice: LA-CC-13-117) and the University Corporation for Atmospheric Research (UCAR).

All rights reserved.
All rights reserved.

LANS is the operator of the Los Alamos National Laboratory under Contract No.
DE-AC52-06NA25396 with the U.S. Department of Energy. UCAR manages the National
Expand Down
1 change: 0 additions & 1 deletion contributors/CONTRIBUTORS_HEADER
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@

Data sources and authors of the regions may be updated from the repository via
this [script](list_contributors.py):

9 changes: 4 additions & 5 deletions contributors/list_contributors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
02/23/2019
"""

import os
import subprocess
import shutil
import datetime
import os
import re
import shutil
import subprocess


def append_to_file(aline, afile):
Expand All @@ -37,8 +37,7 @@ def build_contrib_file():
shutil.copyfile(contribdir + '/CONTRIBUTORS_HEADER', contribfile)

append_to_file(
'List populated on {}:'.format(
datetime.datetime.now().strftime("%Y-%m-%d %H:%M")),
f"List populated on {datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}:",
contribfile)
append_to_file('\n', contribfile)

Expand Down
3 changes: 3 additions & 0 deletions dev-spec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ shapely>=2.0,<3.0
pip
pytest
setuptools>=60
pre-commit
isort
flake8

# Documentation
sphinx
Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ python package to perform various operations on the ``geometric_data``
contained in this repository. To use these scripts, you should either
install the ``geometric_features`` conda package (see README.md in the
root of the repo) or copy the scripts into the root directory. Each script
includes detailed comments that should help you adapt them to your own needs.
includes detailed comments that should help you adapt them to your own needs.
8 changes: 4 additions & 4 deletions examples/setup_antarctic_basins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"""

# stuff to make scipts work for python 2 and python 3
from __future__ import absolute_import, division, print_function, \
unicode_literals

from geometric_features import GeometricFeatures
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import matplotlib.pyplot as plt

from geometric_features import GeometricFeatures

plot = True

# create a GeometricFeatures object that points to a local cache of geometric
Expand Down
12 changes: 6 additions & 6 deletions examples/setup_extended_antarctic_basins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"""

# stuff to make scipts work for python 2 and python 3
from __future__ import absolute_import, division, print_function, \
unicode_literals

from geometric_features import GeometricFeatures, FeatureCollection
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import matplotlib.pyplot as plt

from geometric_features import FeatureCollection, GeometricFeatures

plot = True

# create a GeometricFeatures object that points to a local cache of geometric
Expand All @@ -23,8 +23,8 @@
# 27 IMBIE basin tags tags
fc = FeatureCollection()
for basin in range(1, 28):
print('Adding feature from IMBIE basin {:d}'.format(basin))
basinName = 'Antarctica_IMBIE{:d}'.format(basin)
print(f'Adding feature from IMBIE basin {basin:d}')
basinName = f'Antarctica_IMBIE{basin:d}'
tags = [basinName]
# load the iceshelf regions for one IMBIE basin
fcBasin = gf.read(componentName='iceshelves', objectType='region',
Expand Down
12 changes: 6 additions & 6 deletions examples/setup_ice_shelves.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"""

# stuff to make scipts work for python 2 and python 3
from __future__ import absolute_import, division, print_function, \
unicode_literals

from geometric_features import GeometricFeatures, FeatureCollection
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import matplotlib.pyplot as plt

from geometric_features import FeatureCollection, GeometricFeatures

plot = True

iceShelfNames = ['Abbot',
Expand Down Expand Up @@ -125,8 +125,8 @@

nIMBIEBasins = 27
for basinNumber in range(1, nIMBIEBasins+1):
basinName = 'Antarctica_IMBIE{}'.format(basinNumber)
combinedIceShelves['IMBIE{}'.format(basinNumber)] = [basinName]
basinName = f'Antarctica_IMBIE{basinNumber}'
combinedIceShelves[f'IMBIE{basinNumber}'] = [basinName]

# create a GeometricFeatures object that points to a local cache of geometric
# data and knows which branch of geometric_feature to use to download
Expand Down
4 changes: 2 additions & 2 deletions examples/setup_ocean_critical_passages.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"""

# stuff to make scipts work for python 2 and python 3
from __future__ import absolute_import, division, print_function, \
unicode_literals
from __future__ import (absolute_import, division, print_function,
unicode_literals)

from geometric_features import GeometricFeatures

Expand Down
8 changes: 4 additions & 4 deletions examples/setup_ocean_land_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"""

# stuff to make scipts work for python 2 and python 3
from __future__ import absolute_import, division, print_function, \
unicode_literals

from geometric_features import GeometricFeatures
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import matplotlib.pyplot as plt

from geometric_features import GeometricFeatures

plot = True
withCavities = False

Expand Down
24 changes: 12 additions & 12 deletions examples/setup_ocean_region_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
"""

# stuff to make scipts work for python 2 and python 3
from __future__ import absolute_import, division, print_function, \
unicode_literals
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import matplotlib.pyplot as plt
import copy

import matplotlib.pyplot as plt
import shapely

from geometric_features import GeometricFeatures, FeatureCollection
from geometric_features import FeatureCollection, GeometricFeatures


def build_ocean_basins(gf, plot):
Expand Down Expand Up @@ -50,7 +51,7 @@ def build_ocean_basins(gf, plot):
for oceanName in ['Atlantic', 'Pacific', 'Indian', 'Arctic',
'Southern_Ocean', 'Mediterranean']:

basinName = '{}_Basin'.format(oceanName)
basinName = f'{oceanName}_Basin'
print(oceanName)

print(' * merging features')
Expand Down Expand Up @@ -111,19 +112,19 @@ def build_MOC_basins(gf):
# build MOC basins from regions with the appropriate tags
for basinName in MOCSubBasins:

print('{} MOC'.format(basinName))
print(f'{basinName} MOC')

print(' * merging features')
tags = ['{}_Basin'.format(basin) for basin in MOCSubBasins[basinName]]
tags = [f'{basin}_Basin' for basin in MOCSubBasins[basinName]]

fcBasin = gf.read(componentName='ocean', objectType='region',
tags=tags, allTags=False)

print(' * combining features')
fcBasin = fcBasin.combine(featureName='{}_MOC'.format(basinName))
fcBasin = fcBasin.combine(featureName=f'{basinName}_MOC')

print(' * masking out features south of MOC region')
maskName = 'MOC mask {}'.format(MOCSouthernBoundary[basinName])
maskName = f'MOC mask {MOCSouthernBoundary[basinName]}'
fcMask = gf.read(componentName='ocean', objectType='region',
featureNames=[maskName])
# mask out the region covered by the mask
Expand Down Expand Up @@ -219,10 +220,9 @@ def remove_small_polygons(fc, minArea):
if add:
fcOut.add_feature(copy.deepcopy(feature))
else:
print("{} has been removed.".format(
feature['pproperties']['name']))
print(f"{feature['pproperties']['name']} has been removed.")

print(' * Removed {} small polygons'.format(removedCount))
print(f' * Removed {removedCount} small polygons')

return fcOut

Expand Down
5 changes: 2 additions & 3 deletions examples/setup_ocean_standard_transport_sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
"""

# stuff to make scipts work for python 2 and python 3
from __future__ import absolute_import, division, print_function, \
unicode_literals
from __future__ import (absolute_import, division, print_function,
unicode_literals)

from geometric_features import GeometricFeatures


# create a GeometricFeatures object that points to a local cache of geometric
# data and knows which branch of geometric_feature to use to download
# missing data
Expand Down
Loading
Loading