Skip to content

Commit

Permalink
Enable ruff's flake8-simplify (SIM) rules (#2829)
Browse files Browse the repository at this point in the history
* Enable ruff's flake8-simplify (SIM) rules
* Fix SIM errors
* Ignore SIM117
  • Loading branch information
seisman authored Nov 27, 2023
1 parent 15a0642 commit ca2a91d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
25 changes: 10 additions & 15 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
Uses ctypes to wrap most of the core functions from the C API.
"""
import contextlib
import ctypes as ctp
import pathlib
import sys
import warnings
from contextlib import contextmanager, nullcontext

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -190,12 +190,10 @@ def info(self):
}
# For GMT<6.4.0, API_IMAGE_LAYOUT is not defined if GMT is not
# compiled with GDAL. Since GMT 6.4.0, GDAL is a required GMT
# dependency. The try-except block can be refactored after we bump
# dependency. The code block can be refactored after we bump
# the minimum required GMT version to 6.4.0.
try:
with contextlib.suppress(GMTCLibError):
self._info["image layout"] = self.get_default("API_IMAGE_LAYOUT")
except GMTCLibError:
pass
# API_BIN_VERSION is new in GMT 6.4.0.
if Version(self._info["version"]) >= Version("6.4.0"):
self._info["binary version"] = self.get_default("API_BIN_VERSION")
Expand Down Expand Up @@ -732,10 +730,7 @@ def _parse_pad(self, family, kwargs):
"""
pad = kwargs.get("pad", None)
if pad is None:
if "MATRIX" in family:
pad = 0
else:
pad = self["GMT_PAD_DEFAULT"]
pad = 0 if "MATRIX" in family else self["GMT_PAD_DEFAULT"]
return pad

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

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

@contextmanager
@contextlib.contextmanager
def virtualfile_from_vectors(self, *vectors):
"""
Store 1-D arrays as columns of a table inside a virtual file.
Expand Down Expand Up @@ -1298,7 +1293,7 @@ def virtualfile_from_vectors(self, *vectors):
) as vfile:
yield vfile

@contextmanager
@contextlib.contextmanager
def virtualfile_from_matrix(self, matrix):
"""
Store a 2-D array as a table inside a virtual file.
Expand Down Expand Up @@ -1379,7 +1374,7 @@ def virtualfile_from_matrix(self, matrix):
) as vfile:
yield vfile

@contextmanager
@contextlib.contextmanager
def virtualfile_from_grid(self, grid):
"""
Store a grid in a virtual file.
Expand Down Expand Up @@ -1550,8 +1545,8 @@ def virtualfile_from_data( # noqa: PLR0912

# Decide which virtualfile_from_ function to use
_virtualfile_from = {
"file": nullcontext,
"arg": nullcontext,
"file": contextlib.nullcontext,
"arg": contextlib.nullcontext,
"geojson": tempfile_from_geojson,
"grid": self.virtualfile_from_grid,
"image": tempfile_from_image,
Expand Down
5 changes: 1 addition & 4 deletions pygmt/datasets/earth_magnetic_anomaly.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,7 @@ def load_earth_magnetic_anomaly(
"Valid values are 'emag2', 'emag2_4km', and 'wdmam'."
)
dataset_prefix = magnetic_anomaly_sources[data_source]
if data_source == "wdmam":
dataset_name = "earth_wdmam"
else:
dataset_name = "earth_magnetic_anomaly"
dataset_name = "earth_wdmam" if data_source == "wdmam" else "earth_magnetic_anomaly"
grid = _load_remote_dataset(
dataset_name=dataset_name,
dataset_prefix=dataset_prefix,
Expand Down
2 changes: 1 addition & 1 deletion pygmt/datasets/load_remote_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def _load_remote_dataset(
dataset = datasets[dataset_name]

# check resolution
if resolution not in dataset.resolutions.keys():
if resolution not in dataset.resolutions:
raise GMTInvalidInput(f"Invalid resolution '{resolution}'.")

# check registration
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,15 @@ select = [
"NPY", # numpy
"PD", # pandas-vet
"PL", # pylint
"SIM", # flake8-simplify
"UP", # pyupgrade
"W", # pycodestyle warnings
]
ignore = [
"E501", # Avoid enforcing line-length violations
"PD901", # Allow using the generic variable name `df` for DataFrames
"PLR2004", # Allow any magic values
"SIM117", # Allow nested `with` statements
]

[tool.ruff.lint.isort]
Expand Down

0 comments on commit ca2a91d

Please sign in to comment.