From c88986b569829fc6a2486d7950e29e58ca3fcdcf Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Tue, 6 Feb 2024 12:21:03 +1300 Subject: [PATCH 1/3] Fix B905 [*] `zip()` without an explicit `strict=` parameter Xref https://docs.astral.sh/ruff/rules/zip-without-explicit-strict --- doc/conf.py | 6 +++++- pygmt/clib/session.py | 4 +++- pygmt/helpers/utils.py | 3 +++ pygmt/tests/test_clib_virtualfiles.py | 7 +++++-- pygmt/tests/test_text.py | 4 ++-- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/doc/conf.py b/doc/conf.py index 715ac0c9034..b6321af4569 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -214,7 +214,11 @@ "doc_path": "doc", "galleries": sphinx_gallery_conf["gallery_dirs"], "gallery_dir": dict( - zip(sphinx_gallery_conf["gallery_dirs"], sphinx_gallery_conf["examples_dirs"]) + zip( + sphinx_gallery_conf["gallery_dirs"], + sphinx_gallery_conf["examples_dirs"], + strict=True, + ) ), "github_repo": repository, "github_version": "main", diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index 1be4e2922a0..7e39c960ba9 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -1297,7 +1297,9 @@ def virtualfile_from_vectors(self, *vectors): if len(string_arrays) == 1: strings = string_arrays[0] elif len(string_arrays) > 1: - strings = np.array([" ".join(vals) for vals in zip(*string_arrays)]) + strings = np.array( + [" ".join(vals) for vals in zip(*string_arrays, strict=True)] + ) strings = np.asanyarray(a=strings, dtype=str) self.put_strings( dataset, family="GMT_IS_VECTOR|GMT_IS_DUPLICATE", strings=strings diff --git a/pygmt/helpers/utils.py b/pygmt/helpers/utils.py index 0e7bb93f71d..3f8836f6835 100644 --- a/pygmt/helpers/utils.py +++ b/pygmt/helpers/utils.py @@ -257,6 +257,7 @@ def non_ascii_to_octal(argstr): "◊〈®©™∑" # \34x-35x "〉∫⌠⌡", # \36x-37x. \360 and \377 are undefined [*range(32, 127), *range(160, 240), *range(241, 255)], + strict=True, ) } ) @@ -282,6 +283,7 @@ def non_ascii_to_octal(argstr): "➠➡➢➣➤➥➦➧➨➩➪➫➬➭➮➯" # \34x-\35x "➱➲➳➴➵➶➷➸➹➺➻➼➽➾", # \36x-\37x. \360 and \377 are undefined [*range(32, 127), *range(161, 240), *range(241, 255)], + strict=True, ) } ) @@ -300,6 +302,7 @@ def non_ascii_to_octal(argstr): "Œ†‡Ł⁄‹Š›œŸŽł‰„“”" # \20x-\21x "ı`´ˆ˜¯˘˙¨‚˚¸'˝˛ˇ", # \22x-\23x [*range(25, 32), *range(127, 160)], + strict=True, ) } ) diff --git a/pygmt/tests/test_clib_virtualfiles.py b/pygmt/tests/test_clib_virtualfiles.py index 374d608272d..73e05d0b8f8 100644 --- a/pygmt/tests/test_clib_virtualfiles.py +++ b/pygmt/tests/test_clib_virtualfiles.py @@ -239,7 +239,9 @@ def test_virtualfile_from_vectors_one_string_or_object_column(dtype): with GMTTempFile() as outfile: lib.call_module("convert", f"{vfile} ->{outfile.name}") output = outfile.read(keep_tabs=True) - expected = "".join(f"{i}\t{j}\t{k}\n" for i, j, k in zip(x, y, strings)) + expected = "".join( + f"{i}\t{j}\t{k}\n" for i, j, k in zip(x, y, strings, strict=True) + ) assert output == expected @@ -260,7 +262,8 @@ def test_virtualfile_from_vectors_two_string_or_object_columns(dtype): lib.call_module("convert", f"{vfile} ->{outfile.name}") output = outfile.read(keep_tabs=True) expected = "".join( - f"{h}\t{i}\t{j} {k}\n" for h, i, j, k in zip(x, y, strings1, strings2) + f"{h}\t{i}\t{j} {k}\n" + for h, i, j, k in zip(x, y, strings1, strings2, strict=True) ) assert output == expected diff --git a/pygmt/tests/test_text.py b/pygmt/tests/test_text.py index 5a286348946..31ab7f3c83b 100644 --- a/pygmt/tests/test_text.py +++ b/pygmt/tests/test_text.py @@ -353,7 +353,7 @@ def test_text_transparency(): """ x = np.arange(1, 10) y = np.arange(11, 20) - text = [f"TEXT-{i}-{j}" for i, j in zip(x, y)] + text = [f"TEXT-{i}-{j}" for i, j in zip(x, y, strict=True)] fig = Figure() fig.basemap(region=[0, 10, 10, 20], projection="X10c", frame=True) @@ -368,7 +368,7 @@ def test_text_varying_transparency(): """ x = np.arange(1, 10) y = np.arange(11, 20) - text = [f"TEXT-{i}-{j}" for i, j in zip(x, y)] + text = [f"TEXT-{i}-{j}" for i, j in zip(x, y, strict=True)] transparency = np.arange(10, 100, 10) fig = Figure() From e2252698a83fd5572d3c34a889f22fc6bde6fdee Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Tue, 6 Feb 2024 12:21:58 +1300 Subject: [PATCH 2/3] Fix UP035 [*] Import from `collections.abc` instead: `Callable` Xref https://docs.astral.sh/ruff/rules/deprecated-import --- pygmt/datasets/samples.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pygmt/datasets/samples.py b/pygmt/datasets/samples.py index 5a47e3abeca..0c12444bd81 100644 --- a/pygmt/datasets/samples.py +++ b/pygmt/datasets/samples.py @@ -1,7 +1,8 @@ """ Functions to load sample data. """ -from typing import Callable, Literal, NamedTuple +from collections.abc import Callable +from typing import Literal, NamedTuple import pandas as pd from pygmt.exceptions import GMTInvalidInput From 8872394062962f5ea45640c3451ed8a1d93b6fb5 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Tue, 6 Feb 2024 15:34:13 +1300 Subject: [PATCH 3/3] Fix UP038 Use `X | Y` in `isinstance` call instead of `(X, Y)` Xref https://docs.astral.sh/ruff/rules/non-pep604-isinstance --- pygmt/helpers/utils.py | 4 ++-- pygmt/src/meca.py | 2 +- pygmt/src/text.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pygmt/helpers/utils.py b/pygmt/helpers/utils.py index 3f8836f6835..f1649a68a74 100644 --- a/pygmt/helpers/utils.py +++ b/pygmt/helpers/utils.py @@ -172,9 +172,9 @@ def data_kind(data=None, x=None, y=None, z=None, required_z=False, required_data 'image' """ # determine the data kind - if isinstance(data, (str, pathlib.PurePath)): + if isinstance(data, str | pathlib.PurePath): kind = "file" - elif isinstance(data, (bool, int, float)) or (data is None and not required_data): + elif isinstance(data, bool | int | float) or (data is None and not required_data): kind = "arg" elif isinstance(data, xr.DataArray): kind = "image" if len(data.dims) == 3 else "grid" diff --git a/pygmt/src/meca.py b/pygmt/src/meca.py index 98c8172717c..acaeb3957a3 100644 --- a/pygmt/src/meca.py +++ b/pygmt/src/meca.py @@ -399,7 +399,7 @@ def meca( # noqa: PLR0912, PLR0913, PLR0915 kwargs = self._preprocess(**kwargs) # Convert spec to pandas.DataFrame unless it's a file - if isinstance(spec, (dict, pd.DataFrame)): # spec is a dict or pd.DataFrame + if isinstance(spec, dict | pd.DataFrame): # spec is a dict or pd.DataFrame # determine convention from dict keys or pd.DataFrame column names for conv in ["aki", "gcmt", "mt", "partial", "principal_axis"]: if set(convention_params(conv)).issubset(set(spec.keys())): diff --git a/pygmt/src/text.py b/pygmt/src/text.py index 6c30fbe1f37..cd221b34672 100644 --- a/pygmt/src/text.py +++ b/pygmt/src/text.py @@ -217,7 +217,7 @@ def text_( # noqa: PLR0912 extra_arrays.append(np.atleast_1d(arg)) else: # font or justify is str type extra_arrays.append(np.atleast_1d(arg).astype(str)) - elif isinstance(arg, (int, float, str)): + elif isinstance(arg, int | float | str): kwargs["F"] += f"{flag}{arg}" if isinstance(position, str):