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

CI: Add linting action that uses pre-commit and ruff. #797

Merged
merged 8 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
33 changes: 33 additions & 0 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: linting

on:
push:
pull_request:
workflow_dispatch:

jobs:
pre-job:
runs-on: ubuntu-latest
outputs:
should_skip: ${{ steps.skip_check.outputs.should_skip }}
steps:
- id: skip_check
uses: fkirc/skip-duplicate-actions@master
with:
concurrent_skipping: 'same_content'
skip_after_successful_duplicate: 'false'
do_not_skip: '["workflow_dispatch", "schedule"]'
linting:
needs: pre-job
runs-on: ubuntu-latest
if: ${{ needs.pre-job.outputs.should_skip != 'true' }}
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}
- uses: actions/checkout@v2
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- uses: pre-commit/[email protected]
33 changes: 8 additions & 25 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -247,39 +247,22 @@ An example:
Code Style
----------

ACT follows PEP8 coding standards. To make sure your code follows the
PEP8 style, you can use a variety of tools that can check for you. Two
popular PEP8 check modules are flake8 and pylint. (Note: ACT's continuous
integration uses flake8).
Py-ART uses pre-commit for linting, which applies a variety of pep8 and other
code style rules.

For more on pep8 style:

- https://www.python.org/dev/peps/pep-0008/

To install flake8::
To install pre-commit hooks for the Py-ART repo::

conda install -c conda-forge flake8
pre-commit install

To use flake8::
Afterwards, pre-commit will run on every commit to the repository. It will
re-format files as neccessary.

flake8 path/to/code/to/check.py

To install pylint::

conda install pylint

To use pylint::

pylint path/to/code/to/check.py

Both of these tools are highly configurable to suit a user's taste. Refer to
the tools documentation for details on this process.

- https://flake8.pycqa.org/en/latest/
- https://www.pylint.org/

Naming Convenction
----------------------------------------
Naming Convention
-----------------

Discovery
~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion act/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
register_matplotlib_converters()

# Import early so these classes are available to the object
from .qc import QCFilter, QCTests, clean
from .qc import QCFilter, QCTests, clean # noqa

# Import the lazy loaded modules
submodules = [
Expand Down
3 changes: 2 additions & 1 deletion act/io/hysplit.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ def read_hysplit(filename, base_year=2000):
var_list.append(variable)

input_df = pd.read_csv(
filename, sep='\s+', index_col=False, names=var_list, skiprows=12) # noqa W605
filename, sep=r'\s+', index_col=False, names=var_list, skiprows=12
) # noqa W605
input_df['year'] = base_year + input_df['year']
input_df['time'] = pd.to_datetime(
input_df[["year", "month", "day", "hour", "minute"]], format='%y%m%d%H%M'
Expand Down
4 changes: 2 additions & 2 deletions act/plotting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import lazy_loader as lazy

# Load colormaps
import cmweather
import cmweather # noqa

# Eagerly load in common
from . import common
from . import common # noqa

__getattr__, __dir__, __all__ = lazy.attach(
__name__,
Expand Down
19 changes: 14 additions & 5 deletions act/utils/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ def __init__(self, ds):
self._ds = ds

def change_units(
self, variables=None, desired_unit=None, skip_variables=None, skip_standard=True,
verbose=False, raise_error=False
self,
variables=None,
desired_unit=None,
skip_variables=None,
skip_standard=True,
verbose=False,
raise_error=False,
):
"""
Parameters
Expand Down Expand Up @@ -111,10 +116,14 @@ def change_units(
np.core._exceptions.UFuncTypeError,
):
if raise_error:
raise ValueError(f"Unable to convert '{var_name}' to units of '{desired_unit}'.")
raise ValueError(
f"Unable to convert '{var_name}' to units of '{desired_unit}'."
)
elif verbose:
print(f"\n Unable to convert '{var_name}' to units of '{desired_unit}'. "
f"Skipping unit converstion for '{var_name}'.\n")
print(
f"\n Unable to convert '{var_name}' to units of '{desired_unit}'. "
f"Skipping unit converstion for '{var_name}'.\n"
)

return self._ds

Expand Down
3 changes: 2 additions & 1 deletion continuous_integration/environment_actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ dependencies:
- coveralls
- pandas
- shapely
- pip
- lazy_loader
- cmweather
- arm-test-data
- moviepy
- ruff
- pip
- pip:
- mpl2nc
- metpy
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
# |version| and |release|, also used in various other places throughout the
# built documents.
#
import act
import act # noqa

# The short X.Y version.
version = act.__version__
Expand Down
2 changes: 1 addition & 1 deletion examples/plotting/plot_daytime_averages.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
ds = ds.where(ds['sun_variable'] == 1)

# Take daily mean using xarray features
ds = ds.resample(time='1d', skipna=True, keep_attrs=True).mean()
ds = ds.resample(time='1d', skipna=True).mean()

# Creat Plot Display
display = act.plotting.TimeSeriesDisplay(ds, figsize=(15, 10))
Expand Down
4 changes: 2 additions & 2 deletions examples/plotting/plot_scatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
'ground_speed',
m_field='ambient_temp',
marker='x',
cbar_label=r'Ambient Temperature ($^\circ$C)', # noqa W605
cbar_label=r'Ambient Temperature ($^\circ$C)', # noqa W605
)

# Set the range of the field on the x-axis
Expand All @@ -47,7 +47,7 @@
display.axes[0].plot(ds['true_airspeed'], p(ds['true_airspeed']), 'r', linewidth=2)

# Display the line equation
display.axes[0].text(45, 135, "y = {:.3f}x + ({:.3f})".format(z[0], z[1]), color='r', fontsize=12)
display.axes[0].text(45, 135, f"y = {z[0]:.3f}x + ({z[1]:.3f})", color='r', fontsize=12)

# Calculate Pearson Correlation Coefficient
cc_conc = pearsonr(ds['true_airspeed'], ds['ground_speed'])
Expand Down
2 changes: 1 addition & 1 deletion examples/qc/plot_arm_qc.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
# to give to other users.
# There is a file in the same directory called sgpmfrsr7nchE11.b1.yaml with times of
# incorrect or suspect values that can be read and applied to the Dataset.
from act.qc.add_supplemental_qc import apply_supplemental_qc
from act.qc.add_supplemental_qc import apply_supplemental_qc # noqa

apply_supplemental_qc(ds, 'sgpmfrsr7nchE11.b1.yaml')

Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ ignore = ["act/io/arm.py"]

[tool.check-manifest]
ignore = ["docs/*", "ci/*"]

[tool.ruff]
target-version = "py39"
ignore = [
"E501",
]
2 changes: 1 addition & 1 deletion tests/plotting/test_geodisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from act.tests import sample_files

try:
import cartopy
import cartopy # noqa

CARTOPY_AVAILABLE = True
except ImportError:
Expand Down
2 changes: 1 addition & 1 deletion tests/plotting/test_xsectiondisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from act.tests import sample_files

try:
import cartopy
import cartopy # noqa

CARTOPY_AVAILABLE = True
except ImportError:
Expand Down
2 changes: 1 addition & 1 deletion tests/qc/test_qcfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from act.tests import EXAMPLE_MET1, EXAMPLE_METE40, EXAMPLE_IRT25m20s

try:
import scikit_posthocs
import scikit_posthocs # noqa

SCIKIT_POSTHOCS_AVAILABLE = True
except ImportError:
Expand Down
2 changes: 1 addition & 1 deletion tests/retrievals/test_sp2_retrievals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import act

try:
import pysp2
import pysp2 # noqa

PYSP2_AVAILABLE = True
except ImportError:
Expand Down
6 changes: 4 additions & 2 deletions tests/utils/test_data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ def test_convert_units():
with redirect_stdout(f):
ds.utils.change_units('home_signal_15', 'not_a_real_unit_string', verbose=True)
s = f.getvalue()
assert s.strip() == f"Unable to convert '{var_name}' to units of '{unit}'. Skipping unit converstion for '{var_name}'."

assert (
s.strip()
== f"Unable to convert '{var_name}' to units of '{unit}'. Skipping unit converstion for '{var_name}'."
)
ds.close()
del ds

Expand Down
3 changes: 2 additions & 1 deletion tests/utils/test_io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from act.tests import sample_files

try:
import moviepy.video.io.ImageSequenceClip
import moviepy.video.io.ImageSequenceClip # noqa

MOVIEPY_AVAILABLE = True
except ImportError:
MOVIEPY_AVAILABLE = False
Expand Down
Loading