Skip to content

Commit 31688c4

Browse files
authored
ruff: Fix Python 3.10 violations (#3040)
* Fix B905 [*] `zip()` without an explicit `strict=` parameter Xref https://docs.astral.sh/ruff/rules/zip-without-explicit-strict * Fix UP035 [*] Import from `collections.abc` instead: `Callable` Xref https://docs.astral.sh/ruff/rules/deprecated-import * Fix UP038 Use `X | Y` in `isinstance` call instead of `(X, Y)` Xref https://docs.astral.sh/ruff/rules/non-pep604-isinstance
1 parent 72131ae commit 31688c4

File tree

8 files changed

+24
-11
lines changed

8 files changed

+24
-11
lines changed

doc/conf.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@
214214
"doc_path": "doc",
215215
"galleries": sphinx_gallery_conf["gallery_dirs"],
216216
"gallery_dir": dict(
217-
zip(sphinx_gallery_conf["gallery_dirs"], sphinx_gallery_conf["examples_dirs"])
217+
zip(
218+
sphinx_gallery_conf["gallery_dirs"],
219+
sphinx_gallery_conf["examples_dirs"],
220+
strict=True,
221+
)
218222
),
219223
"github_repo": repository,
220224
"github_version": "main",

pygmt/clib/session.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,9 @@ def virtualfile_from_vectors(self, *vectors):
12971297
if len(string_arrays) == 1:
12981298
strings = string_arrays[0]
12991299
elif len(string_arrays) > 1:
1300-
strings = np.array([" ".join(vals) for vals in zip(*string_arrays)])
1300+
strings = np.array(
1301+
[" ".join(vals) for vals in zip(*string_arrays, strict=True)]
1302+
)
13011303
strings = np.asanyarray(a=strings, dtype=str)
13021304
self.put_strings(
13031305
dataset, family="GMT_IS_VECTOR|GMT_IS_DUPLICATE", strings=strings

pygmt/datasets/samples.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""
22
Functions to load sample data.
33
"""
4-
from typing import Callable, Literal, NamedTuple
4+
from collections.abc import Callable
5+
from typing import Literal, NamedTuple
56

67
import pandas as pd
78
from pygmt.exceptions import GMTInvalidInput

pygmt/helpers/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ def data_kind(data=None, x=None, y=None, z=None, required_z=False, required_data
172172
'image'
173173
"""
174174
# determine the data kind
175-
if isinstance(data, (str, pathlib.PurePath)):
175+
if isinstance(data, str | pathlib.PurePath):
176176
kind = "file"
177-
elif isinstance(data, (bool, int, float)) or (data is None and not required_data):
177+
elif isinstance(data, bool | int | float) or (data is None and not required_data):
178178
kind = "arg"
179179
elif isinstance(data, xr.DataArray):
180180
kind = "image" if len(data.dims) == 3 else "grid"
@@ -257,6 +257,7 @@ def non_ascii_to_octal(argstr):
257257
"◊〈®©™∑" # \34x-35x
258258
"〉∫⌠⌡", # \36x-37x. \360 and \377 are undefined
259259
[*range(32, 127), *range(160, 240), *range(241, 255)],
260+
strict=True,
260261
)
261262
}
262263
)
@@ -282,6 +283,7 @@ def non_ascii_to_octal(argstr):
282283
"➠➡➢➣➤➥➦➧➨➩➪➫➬➭➮➯" # \34x-\35x
283284
"➱➲➳➴➵➶➷➸➹➺➻➼➽➾", # \36x-\37x. \360 and \377 are undefined
284285
[*range(32, 127), *range(161, 240), *range(241, 255)],
286+
strict=True,
285287
)
286288
}
287289
)
@@ -300,6 +302,7 @@ def non_ascii_to_octal(argstr):
300302
"Œ†‡Ł⁄‹Š›œŸŽł‰„“”" # \20x-\21x
301303
"ı`´ˆ˜¯˘˙¨‚˚¸'˝˛ˇ", # \22x-\23x
302304
[*range(25, 32), *range(127, 160)],
305+
strict=True,
303306
)
304307
}
305308
)

pygmt/src/meca.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def meca( # noqa: PLR0912, PLR0913, PLR0915
399399
kwargs = self._preprocess(**kwargs)
400400

401401
# Convert spec to pandas.DataFrame unless it's a file
402-
if isinstance(spec, (dict, pd.DataFrame)): # spec is a dict or pd.DataFrame
402+
if isinstance(spec, dict | pd.DataFrame): # spec is a dict or pd.DataFrame
403403
# determine convention from dict keys or pd.DataFrame column names
404404
for conv in ["aki", "gcmt", "mt", "partial", "principal_axis"]:
405405
if set(convention_params(conv)).issubset(set(spec.keys())):

pygmt/src/text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def text_( # noqa: PLR0912
217217
extra_arrays.append(np.atleast_1d(arg))
218218
else: # font or justify is str type
219219
extra_arrays.append(np.atleast_1d(arg).astype(str))
220-
elif isinstance(arg, (int, float, str)):
220+
elif isinstance(arg, int | float | str):
221221
kwargs["F"] += f"{flag}{arg}"
222222

223223
if isinstance(position, str):

pygmt/tests/test_clib_virtualfiles.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ def test_virtualfile_from_vectors_one_string_or_object_column(dtype):
239239
with GMTTempFile() as outfile:
240240
lib.call_module("convert", f"{vfile} ->{outfile.name}")
241241
output = outfile.read(keep_tabs=True)
242-
expected = "".join(f"{i}\t{j}\t{k}\n" for i, j, k in zip(x, y, strings))
242+
expected = "".join(
243+
f"{i}\t{j}\t{k}\n" for i, j, k in zip(x, y, strings, strict=True)
244+
)
243245
assert output == expected
244246

245247

@@ -260,7 +262,8 @@ def test_virtualfile_from_vectors_two_string_or_object_columns(dtype):
260262
lib.call_module("convert", f"{vfile} ->{outfile.name}")
261263
output = outfile.read(keep_tabs=True)
262264
expected = "".join(
263-
f"{h}\t{i}\t{j} {k}\n" for h, i, j, k in zip(x, y, strings1, strings2)
265+
f"{h}\t{i}\t{j} {k}\n"
266+
for h, i, j, k in zip(x, y, strings1, strings2, strict=True)
264267
)
265268
assert output == expected
266269

pygmt/tests/test_text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def test_text_transparency():
353353
"""
354354
x = np.arange(1, 10)
355355
y = np.arange(11, 20)
356-
text = [f"TEXT-{i}-{j}" for i, j in zip(x, y)]
356+
text = [f"TEXT-{i}-{j}" for i, j in zip(x, y, strict=True)]
357357

358358
fig = Figure()
359359
fig.basemap(region=[0, 10, 10, 20], projection="X10c", frame=True)
@@ -368,7 +368,7 @@ def test_text_varying_transparency():
368368
"""
369369
x = np.arange(1, 10)
370370
y = np.arange(11, 20)
371-
text = [f"TEXT-{i}-{j}" for i, j in zip(x, y)]
371+
text = [f"TEXT-{i}-{j}" for i, j in zip(x, y, strict=True)]
372372
transparency = np.arange(10, 100, 10)
373373

374374
fig = Figure()

0 commit comments

Comments
 (0)