Skip to content

Commit ca2a91d

Browse files
authored
Enable ruff's flake8-simplify (SIM) rules (#2829)
* Enable ruff's flake8-simplify (SIM) rules * Fix SIM errors * Ignore SIM117
1 parent 15a0642 commit ca2a91d

File tree

4 files changed

+14
-20
lines changed

4 files changed

+14
-20
lines changed

pygmt/clib/session.py

+10-15
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
55
Uses ctypes to wrap most of the core functions from the C API.
66
"""
7+
import contextlib
78
import ctypes as ctp
89
import pathlib
910
import sys
1011
import warnings
11-
from contextlib import contextmanager, nullcontext
1212

1313
import numpy as np
1414
import pandas as pd
@@ -190,12 +190,10 @@ def info(self):
190190
}
191191
# For GMT<6.4.0, API_IMAGE_LAYOUT is not defined if GMT is not
192192
# compiled with GDAL. Since GMT 6.4.0, GDAL is a required GMT
193-
# dependency. The try-except block can be refactored after we bump
193+
# dependency. The code block can be refactored after we bump
194194
# the minimum required GMT version to 6.4.0.
195-
try:
195+
with contextlib.suppress(GMTCLibError):
196196
self._info["image layout"] = self.get_default("API_IMAGE_LAYOUT")
197-
except GMTCLibError:
198-
pass
199197
# API_BIN_VERSION is new in GMT 6.4.0.
200198
if Version(self._info["version"]) >= Version("6.4.0"):
201199
self._info["binary version"] = self.get_default("API_BIN_VERSION")
@@ -732,10 +730,7 @@ def _parse_pad(self, family, kwargs):
732730
"""
733731
pad = kwargs.get("pad", None)
734732
if pad is None:
735-
if "MATRIX" in family:
736-
pad = 0
737-
else:
738-
pad = self["GMT_PAD_DEFAULT"]
733+
pad = 0 if "MATRIX" in family else self["GMT_PAD_DEFAULT"]
739734
return pad
740735

741736
def _parse_constant(self, constant, valid, valid_modifiers=None):
@@ -1089,7 +1084,7 @@ def write_data(self, family, geometry, mode, wesn, output, data):
10891084
if status != 0:
10901085
raise GMTCLibError(f"Failed to write dataset to '{output}'")
10911086

1092-
@contextmanager
1087+
@contextlib.contextmanager
10931088
def open_virtual_file(self, family, geometry, direction, data):
10941089
"""
10951090
Open a GMT virtual file to pass data to and from a module.
@@ -1196,7 +1191,7 @@ def open_virtual_file(self, family, geometry, direction, data):
11961191
if status != 0:
11971192
raise GMTCLibError(f"Failed to close virtual file '{vfname}'.")
11981193

1199-
@contextmanager
1194+
@contextlib.contextmanager
12001195
def virtualfile_from_vectors(self, *vectors):
12011196
"""
12021197
Store 1-D arrays as columns of a table inside a virtual file.
@@ -1298,7 +1293,7 @@ def virtualfile_from_vectors(self, *vectors):
12981293
) as vfile:
12991294
yield vfile
13001295

1301-
@contextmanager
1296+
@contextlib.contextmanager
13021297
def virtualfile_from_matrix(self, matrix):
13031298
"""
13041299
Store a 2-D array as a table inside a virtual file.
@@ -1379,7 +1374,7 @@ def virtualfile_from_matrix(self, matrix):
13791374
) as vfile:
13801375
yield vfile
13811376

1382-
@contextmanager
1377+
@contextlib.contextmanager
13831378
def virtualfile_from_grid(self, grid):
13841379
"""
13851380
Store a grid in a virtual file.
@@ -1550,8 +1545,8 @@ def virtualfile_from_data( # noqa: PLR0912
15501545

15511546
# Decide which virtualfile_from_ function to use
15521547
_virtualfile_from = {
1553-
"file": nullcontext,
1554-
"arg": nullcontext,
1548+
"file": contextlib.nullcontext,
1549+
"arg": contextlib.nullcontext,
15551550
"geojson": tempfile_from_geojson,
15561551
"grid": self.virtualfile_from_grid,
15571552
"image": tempfile_from_image,

pygmt/datasets/earth_magnetic_anomaly.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,7 @@ def load_earth_magnetic_anomaly(
144144
"Valid values are 'emag2', 'emag2_4km', and 'wdmam'."
145145
)
146146
dataset_prefix = magnetic_anomaly_sources[data_source]
147-
if data_source == "wdmam":
148-
dataset_name = "earth_wdmam"
149-
else:
150-
dataset_name = "earth_magnetic_anomaly"
147+
dataset_name = "earth_wdmam" if data_source == "wdmam" else "earth_magnetic_anomaly"
151148
grid = _load_remote_dataset(
152149
dataset_name=dataset_name,
153150
dataset_prefix=dataset_prefix,

pygmt/datasets/load_remote_dataset.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def _load_remote_dataset(
273273
dataset = datasets[dataset_name]
274274

275275
# check resolution
276-
if resolution not in dataset.resolutions.keys():
276+
if resolution not in dataset.resolutions:
277277
raise GMTInvalidInput(f"Invalid resolution '{resolution}'.")
278278

279279
# check registration

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,15 @@ select = [
102102
"NPY", # numpy
103103
"PD", # pandas-vet
104104
"PL", # pylint
105+
"SIM", # flake8-simplify
105106
"UP", # pyupgrade
106107
"W", # pycodestyle warnings
107108
]
108109
ignore = [
109110
"E501", # Avoid enforcing line-length violations
110111
"PD901", # Allow using the generic variable name `df` for DataFrames
111112
"PLR2004", # Allow any magic values
113+
"SIM117", # Allow nested `with` statements
112114
]
113115

114116
[tool.ruff.lint.isort]

0 commit comments

Comments
 (0)