Skip to content

Commit 0f31b7a

Browse files
authored
Enable ruff's literal-membership (PLR6201) rule and fix violations (#3317)
* Enable ruff's literal-membership (PLR6201) rule Xref https://docs.astral.sh/ruff/rules/literal-membership. * Fix PLR6201 violations * Fix PT001 violation due to preview mode setting Xref https://docs.astral.sh/ruff/settings/#lint_flake8-pytest-style_fixture-parentheses and astral-sh/ruff#12106. * Check geodataframe column dtype's str name instead of class object
1 parent 41a0fe0 commit 0f31b7a

File tree

11 files changed

+22
-19
lines changed

11 files changed

+22
-19
lines changed

pygmt/accessors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def registration(self):
141141

142142
@registration.setter
143143
def registration(self, value):
144-
if value not in (0, 1):
144+
if value not in {0, 1}:
145145
raise GMTInvalidInput(
146146
f"Invalid grid registration value: {value}, should be either "
147147
"0 for Gridline registration or 1 for Pixel registration."
@@ -157,7 +157,7 @@ def gtype(self):
157157

158158
@gtype.setter
159159
def gtype(self, value):
160-
if value not in (0, 1):
160+
if value not in {0, 1}:
161161
raise GMTInvalidInput(
162162
f"Invalid coordinate system type: {value}, should be "
163163
"either 0 for Cartesian or 1 for Geographic."

pygmt/clib/session.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ def put_vector(self, dataset, column, vector):
944944
)
945945

946946
gmt_type = self._check_dtype_and_dim(vector, ndim=1)
947-
if gmt_type in (self["GMT_TEXT"], self["GMT_DATETIME"]):
947+
if gmt_type in {self["GMT_TEXT"], self["GMT_DATETIME"]}:
948948
if gmt_type == self["GMT_DATETIME"]:
949949
vector = np.datetime_as_string(array_to_datetime(vector))
950950
vector_pointer = strings_to_ctypes_array(vector)
@@ -1622,7 +1622,7 @@ def virtualfile_in( # noqa: PLR0912
16221622
}[kind]
16231623

16241624
# Ensure the data is an iterable (Python list or tuple)
1625-
if kind in ("geojson", "grid", "image", "file", "arg"):
1625+
if kind in {"geojson", "grid", "image", "file", "arg"}:
16261626
if kind == "image" and data.dtype != "uint8":
16271627
msg = (
16281628
f"Input image has dtype: {data.dtype} which is unsupported, "
@@ -1849,7 +1849,7 @@ def read_virtualfile(
18491849
# _GMT_DATASET).
18501850
if kind is None: # Return the ctypes void pointer
18511851
return pointer
1852-
if kind in ["image", "cube"]:
1852+
if kind in {"image", "cube"}:
18531853
raise NotImplementedError(f"kind={kind} is not supported yet.")
18541854
dtype = {"dataset": _GMT_DATASET, "grid": _GMT_GRID}[kind]
18551855
return ctp.cast(pointer, ctp.POINTER(dtype))

pygmt/datasets/load_remote_dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def _load_remote_dataset(
387387
if registration is None:
388388
# Use gridline registration unless only pixel registration is available
389389
registration = "gridline" if "gridline" in resinfo.registrations else "pixel"
390-
elif registration in ("pixel", "gridline"):
390+
elif registration in {"pixel", "gridline"}:
391391
if registration not in resinfo.registrations:
392392
raise GMTInvalidInput(
393393
f"{registration} registration is not available for the "

pygmt/figure.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def psconvert(self, **kwargs):
236236
kwargs["A"] = ""
237237

238238
prefix = kwargs.get("F")
239-
if prefix in ["", None, False, True]:
239+
if prefix in {"", None, False, True}:
240240
raise GMTInvalidInput(
241241
"The 'prefix' parameter must be specified with a valid value."
242242
)
@@ -363,7 +363,7 @@ def savefig( # noqa: PLR0912
363363
kwargs["Qg"] = 2
364364

365365
if worldfile:
366-
if ext in ["eps", "kml", "pdf", "tiff"]:
366+
if ext in {"eps", "kml", "pdf", "tiff"}:
367367
raise GMTInvalidInput(
368368
f"Saving a world file is not supported for '{ext}' format."
369369
)
@@ -444,7 +444,7 @@ def show(self, dpi=300, width=500, method=None, waiting=0.5, **kwargs):
444444
if method is None:
445445
method = SHOW_CONFIG["method"]
446446

447-
if method not in ["external", "notebook", "none"]:
447+
if method not in {"external", "notebook", "none"}:
448448
raise GMTInvalidInput(
449449
f"Invalid display method '{method}', "
450450
"should be either 'notebook', 'external', or 'none'."
@@ -583,7 +583,7 @@ def set_display(method=None):
583583
>>> pygmt.set_display(method=None)
584584
>>> fig.show() # again, will show a PNG image in the current notebook
585585
"""
586-
if method in ["notebook", "external", "none"]:
586+
if method in {"notebook", "external", "none"}:
587587
SHOW_CONFIG["method"] = method
588588
elif method is not None:
589589
raise GMTInvalidInput(

pygmt/helpers/tempfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,15 @@ def tempfile_from_geojson(geojson):
148148
geojson = geojson.reset_index(drop=False)
149149
schema = gpd.io.file.infer_schema(geojson)
150150
for col, dtype in schema["properties"].items():
151-
if dtype in ("int", "int64"):
151+
if dtype in {"int", "int64"}:
152152
overflow = geojson[col].abs().max() > 2**31 - 1
153153
schema["properties"][col] = "float" if overflow else "int32"
154154
ogrgmt_kwargs["schema"] = schema
155155
else: # GeoPandas v1.x.
156156
# The default engine "pyogrio" doesn't support the 'schema' parameter
157157
# but we can change the dtype directly.
158158
for col in geojson.columns:
159-
if geojson[col].dtype in ("int", "int64", "Int64"):
159+
if geojson[col].dtype.name in {"int", "int64", "Int64"}:
160160
overflow = geojson[col].abs().max() > 2**31 - 1
161161
dtype = "float" if overflow else "int32"
162162
geojson[col] = geojson[col].astype(dtype)

pygmt/helpers/validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def validate_output_table_type(
4949
... assert len(w) == 1
5050
'file'
5151
"""
52-
if output_type not in ["file", "numpy", "pandas"]:
52+
if output_type not in {"file", "numpy", "pandas"}:
5353
raise GMTInvalidInput(
5454
"Must specify 'output_type' either as 'file', 'numpy', or 'pandas'."
5555
)

pygmt/src/meca.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def convention_code(convention, component="full"):
9595
f"Invalid component '{component}' for convention '{convention}'."
9696
)
9797
return codes2[convention][component]
98-
if convention in ["a", "c", "m", "d", "z", "p", "x", "y", "t"]:
98+
if convention in {"a", "c", "m", "d", "z", "p", "x", "y", "t"}:
9999
return convention
100100
raise GMTInvalidInput(f"Invalid convention '{convention}'.")
101101

pygmt/src/tilemap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def tilemap(
146146

147147
# Only set region if no_clip is None or False, so that plot is clipped to exact
148148
# bounding box region
149-
if kwargs.get("N") in [None, False]:
149+
if kwargs.get("N") in {None, False}:
150150
kwargs["R"] = "/".join(str(coordinate) for coordinate in region)
151151

152152
with Session() as lib:

pygmt/tests/test_clib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ def test_get_default():
529529
Make sure get_default works without crashing and gives reasonable results.
530530
"""
531531
with clib.Session() as lib:
532-
assert lib.get_default("API_GRID_LAYOUT") in ["rows", "columns"]
532+
assert lib.get_default("API_GRID_LAYOUT") in {"rows", "columns"}
533533
assert int(lib.get_default("API_CORES")) >= 1
534534
assert Version(lib.get_default("API_VERSION")) >= Version("6.3.0")
535535
assert lib.get_default("PROJ_LENGTH_UNIT") == "cm"

pygmt/tests/test_clib_loading.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def _mock_ctypes_cdll_return(self, libname):
131131
# libname is a loaded GMT library
132132
return self.loaded_libgmt
133133

134-
@pytest.fixture()
134+
@pytest.fixture
135135
def _mock_ctypes(self, monkeypatch):
136136
"""
137137
Patch the ctypes.CDLL function.

0 commit comments

Comments
 (0)