From 11f4539514d70957743cfb84049cdc0a46aa2cd3 Mon Sep 17 00:00:00 2001 From: mdtanker Date: Mon, 18 Nov 2024 14:16:27 -0500 Subject: [PATCH] chore: remove ruff commands and use pre-commit --- Makefile | 8 +------- docs/contributing.md | 10 +++------- pyproject.toml | 1 - src/polartoolkit/maps.py | 42 +++++++++++++-------------------------- src/polartoolkit/utils.py | 6 +++--- 5 files changed, 21 insertions(+), 46 deletions(-) diff --git a/Makefile b/Makefile index ecd5b69d..74f98f43 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/docs/contributing.md b/docs/contributing.md index d778c1e9..4e2edbf8 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -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: @@ -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 ``` diff --git a/pyproject.toml b/pyproject.toml index 150dee02..7ae6d8b8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -86,7 +86,6 @@ docs = [ ] dev = [ "polartoolkit[interactive,viz,test,docs]", - "ruff", "nox", "pre-commit", "pylint>=3.2", diff --git a/src/polartoolkit/maps.py b/src/polartoolkit/maps.py index 3d8e8f45..f85d98e0 100644 --- a/src/polartoolkit/maps.py +++ b/src/polartoolkit/maps.py @@ -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 @@ -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 @@ -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 @@ -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( @@ -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): @@ -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)) diff --git a/src/polartoolkit/utils.py b/src/polartoolkit/utils.py index 6cc8246b..72639c29 100644 --- a/src/polartoolkit/utils.py +++ b/src/polartoolkit/utils.py @@ -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) @@ -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]),