Skip to content

Commit

Permalink
chore: remove ruff commands and use pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtanker committed Nov 18, 2024
1 parent 7514e3a commit e73a310
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 46 deletions.
8 changes: 1 addition & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,13 @@ test_fetch:
####
####

format:
ruff format $(STYLE_CHECK_FILES)

check:
ruff check --fix $(STYLE_CHECK_FILES)

lint:
pre-commit run --all-files

pylint:
pylint $(PROJECT)

style: format check lint pylint
style: check pylint

mypy:
mypy src/$(PROJECT)
Expand Down
10 changes: 3 additions & 7 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,11 @@ This environment now contains your local, editable version of PolarToolkit, mean
### Code style and linting

We use [Ruff](https://docs.astral.sh/ruff/) to format the code so we don't have to
think about it. This allows you to not think about proper indentation, line length, or aligning your code while to development. Before committing, or periodically while you code, run the following to automatically format your code:
```
make format
```
Some formatting changes can't be applied automatically. Running the following to see these.
We use [pre-commit](https://pre-commit.com/) to check code style. This can be used locally, by installing pre-commit, or can be used as a pre-commit hook, where it is automatically run by git for each commit to the repository. This pre-commit hook wont add or commit any changes, but will just inform your of what should be changed. Pre-commit is setup within the `.pre-commit-config.yaml` file. There are lots of hooks (processes) which run for each pre-commit call, including [Ruff](https://docs.astral.sh/ruff/) to format and lint the code. This allows you to not think about proper indentation, line length, or aligning your code during development. Before committing, or periodically while you code, run the following to automatically format your code:
```
make check
```

Go through the output of this and try to change the code based on the errors. Search the error codes on the [Ruff documentation](https://docs.astral.sh/ruff/), which should give suggestions. Re-run the check to see if you've fixed it. Somethings can't be resolved (unsplittable urls longer than the line length). For these, add `# noqa: []` at the end of the line and the check will ignore it. Inside the square brackets add the specific error code you want to ignore.

We also use [Pylint](https://pylint.readthedocs.io/en/latest/), which performs static-linting on the code. This checks the code and catches many common bugs and errors, without running any of the code. This check is slightly slower the the `Ruff` check. Run it with the following:
Expand All @@ -168,7 +164,7 @@ make pylint
```
Similar to using `Ruff`, go through the output of this, search the error codes on the [Pylint documentation](https://pylint.readthedocs.io/en/latest/) for help, and try and fix all the errors and warnings. If there are false-positives, or your confident you don't agree with the warning, add ` # pylint: disable=` at the end of the lines, with the warning code following the `=`.

To run all three of the code checks, use:
To run both pre-commit and pylint together use:
```
make style
```
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ docs = [
]
dev = [
"polartoolkit[interactive,viz,test,docs]",
"ruff",
"nox",
"pre-commit",
"pylint>=3.2",
Expand Down
42 changes: 14 additions & 28 deletions src/polartoolkit/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ def basemap(
add_box(
fig,
show_region,
pen=kwargs.get("region_pen"),
pen=kwargs.get("region_pen"), # type: ignore[arg-type]
)

# add datapoints
Expand Down Expand Up @@ -1087,7 +1087,7 @@ def plot_grd(
add_box(
fig,
show_region,
pen=kwargs.get("region_pen"),
pen=kwargs.get("region_pen"), # type: ignore[arg-type]
)

# plot groundingline and coastlines
Expand Down Expand Up @@ -1293,7 +1293,7 @@ def add_colorbar(
# if no region supplied, get region of current PyGMT figure
if region is None:
with pygmt.clib.Session() as lib:
region = list(lib.extract_region())
region = tuple(lib.extract_region())
assert len(region) == 4

# clip grid to plot region
Expand Down Expand Up @@ -2200,35 +2200,24 @@ def subplots(
for i, j in enumerate(grids):
with fig.set_panel(panel=i):
# if list of cmaps provided, use them
if kwargs.get("cmaps") is not None:
cmap = kwargs.get("cmaps")[i]
# if not, use viridis
else:
cmap = "viridis"
cmaps = kwargs.get("cmaps")
cmap = cmaps[i] if cmaps is not None else "viridis"

# if list of titles provided, use them
if kwargs.get("subplot_titles") is not None:
sub_title = kwargs.get("subplot_titles")[i]
else:
sub_title = None
subplot_titles = kwargs.get("subplot_titles")
sub_title = subplot_titles[i] if subplot_titles is not None else None

# if list of colorbar labels provided, use them
if kwargs.get("cbar_labels") is not None:
cbar_label = kwargs.get("cbar_labels")[i]
else:
cbar_label = " "
cbar_labels = kwargs.get("cbar_labels")
cbar_label = cbar_labels[i] if cbar_labels is not None else " "

# if list of colorbar units provided, use them
if kwargs.get("cbar_units") is not None:
cbar_unit = kwargs.get("cbar_units")[i]
else:
cbar_unit = " "
cbar_units = kwargs.get("cbar_units")
cbar_unit = cbar_units[i] if cbar_units is not None else " "

# if list of cmaps limits provided, use them
if kwargs.get("cpt_limits") is not None:
cpt_lims = kwargs.get("cpt_limits")[i]
else:
cpt_lims = None
cpt_limits = kwargs.get("cpt_limits")
cpt_lims = cpt_limits[i] if cpt_limits is not None else None

# plot the grids
plot_grd(
Expand Down Expand Up @@ -2313,7 +2302,6 @@ def plot_3d(
num_grids = len(grids)

# if not provided as a list, make it a list the length of num_grids

if not isinstance(cbar_labels, list):
cbar_labels = [cbar_labels] * num_grids
if not isinstance(modis, list):
Expand All @@ -2338,9 +2326,7 @@ def plot_3d(
& (all(isinstance(x, float) for x in cpt_lims_list))
):
cpt_lims_list = [cpt_lims_list] * num_grids
if cmap_region is None:
cmap_region = [None] * num_grids
elif (
if (
isinstance(cmap_region, list)
& (len(cmap_region) == 4)
& (all(isinstance(x, float) for x in cmap_region))
Expand Down
6 changes: 3 additions & 3 deletions src/polartoolkit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ def grd_compare(
three xarray.DataArrays: (diff, resampled grid1, resampled grid2)
"""
shp_mask = kwargs.get("shp_mask")
region: tuple[float, float, float, float] = kwargs.get("region")
region = kwargs.get("region")
verbose = kwargs.get("verbose", "e")
if isinstance(da1, str):
da1 = xr.load_dataarray(da1)
Expand Down Expand Up @@ -1406,8 +1406,8 @@ def grd_compare(
a.set_aspect("equal")
if kwargs.get("points") is not None:
a.plot(kwargs.get("points").x, kwargs.get("points").y, "k+") # type: ignore[union-attr]
if kwargs.get("show_region") is not None:
show_region = kwargs.get("show_region")
show_region = kwargs.get("show_region")
if show_region is not None:
a.add_patch(
mpl.patches.Rectangle(
xy=(show_region[0], show_region[2]),
Expand Down

0 comments on commit e73a310

Please sign in to comment.