Add flake8-simplicity rules

This commit is contained in:
Julian Stirling 2025-09-18 15:56:10 +01:00
parent d7d1f42b2c
commit 6b1e40f689
7 changed files with 38 additions and 49 deletions

View file

@ -119,7 +119,7 @@ select = [
"PT", # pytest linting
"RET", # Consistent clear return statements
"RSE", # Raise parentheses
# "SIM", # Simplifications detected
"SIM", # Simplifications detected
"ARG", # unused arguments
"C90", # McCabe complexity!
"NPY", # Numpy linting
@ -142,6 +142,8 @@ ignore = [
# magic (such as 255 when doing uint8 maths)
"PT011", # Ifnore pytest.raises being used on too gereneral expectations without a
# match. We may need to revisit this.
"SIM105", # Not enforcing use of `contextlib.suppress(NotConnectedToServerError)`
# instead of `try`-`except`-`pass`
]
[tool.ruff.lint.per-file-ignores]

View file

@ -210,8 +210,7 @@ class ScanDirectoryManager:
not exist.
"""
file_path = os.path.join(self.path_for(scan_name), filename)
if check_exists:
if not os.path.exists(file_path):
if check_exists and not os.path.exists(file_path):
return None
return file_path
@ -225,8 +224,7 @@ class ScanDirectoryManager:
then the path is returned anyway
"""
file_path = os.path.join(self.img_dir_for(scan_name), filename)
if check_exists:
if not os.path.exists(file_path):
if check_exists and not os.path.exists(file_path):
return None
return file_path
@ -528,7 +526,8 @@ class ScanDirectory:
"""
zip_fname = os.path.join(self.dir_path, "images.zip")
if os.path.isfile(zip_fname):
# Use noqa as converting this into a 1 liner is not more readable.
if os.path.isfile(zip_fname): # noqa: SIM108
# get a list of files in the existing zip
zip_files = get_files_in_zip(zip_fname)
else:

View file

@ -163,10 +163,7 @@ class ScanPlanner:
# If focussed locations exist return closest location, favouring most recent
closest_pos = self.closest_focus_site(next_location)
if closest_pos is None:
z = None
else:
z = closest_pos[2]
z = None if closest_pos is None else closest_pos[2]
return next_location, z
@ -363,10 +360,7 @@ class SmartSpiral(ScanPlanner):
# If focused locations exist, return the neighbour with the lowest z position
closest_pos = self.select_nearby_focus_site(next_location)
if closest_pos is None:
z = None
else:
z = closest_pos[2]
z = None if closest_pos is None else closest_pos[2]
return next_location, z

View file

@ -165,9 +165,7 @@ class PreviewStitcher(BaseStitcher):
with self._popen_lock:
if self._popen_obj is None:
return False
if self._popen_obj.poll() is None:
return True
return False
return self._popen_obj.poll() is None
def wait(self, cancel: lt.deps.CancelHook) -> None:
"""Wait for this preview stitch to return.

View file

@ -334,8 +334,7 @@ class SmartScanThing(lt.Thing):
"""Manage the stitching threads, starting them if needed and not already running."""
# Assume 4 images means at least one offset in x and y, making the stitching
# well constrained.
if self._scan_data.image_count > 3:
if not self._preview_stitcher.running:
if self._scan_data.image_count > 3 and not self._preview_stitcher.running:
self._preview_stitcher.start()
@_scan_running

View file

@ -22,8 +22,7 @@ def test_no_warnings_if_correct_permissions(caplog):
"""
# Reset handler at start of test
ofm_logging.OFM_HANDLER = ofm_logging.OFMHandler()
with caplog.at_level(logging.WARNING):
with tempfile.TemporaryDirectory() as tmpdir:
with caplog.at_level(logging.WARNING), tempfile.TemporaryDirectory() as tmpdir:
ofm_logging.configure_logging(tmpdir)
assert len(caplog.records) == 0
with open(ofm_logging.OFM_LOG_FILE, "r", encoding="utf-8") as log_file:
@ -41,8 +40,7 @@ def test_permission_error_raises_warning(mocker, caplog):
)
# Reset handler at start of test
ofm_logging.OFM_HANDLER = ofm_logging.OFMHandler()
with caplog.at_level(logging.WARNING):
with tempfile.TemporaryDirectory() as tmpdir:
with caplog.at_level(logging.WARNING), tempfile.TemporaryDirectory() as tmpdir:
ofm_logging.configure_logging(tmpdir)
assert len(caplog.records) == 1
# Check OFM logger is added even if the file logger couldn't be.
@ -54,8 +52,7 @@ def test_making_log_dir(caplog):
"""Check that configure_logging will make a dir if needed."""
# Reset handler at start of test
ofm_logging.OFM_HANDLER = ofm_logging.OFMHandler()
with caplog.at_level(logging.WARNING):
with tempfile.TemporaryDirectory() as tmpdir:
with caplog.at_level(logging.WARNING), tempfile.TemporaryDirectory() as tmpdir:
log_dir = os.path.join(tmpdir, "new_dir")
assert not os.path.isdir(log_dir)
ofm_logging.configure_logging(log_dir)

View file

@ -23,8 +23,8 @@ def test_enforce_xy_tuple():
with pytest.raises(TypeError):
scan_planners.enforce_xy_tuple(value)
assert (1, 6) == scan_planners.enforce_xy_tuple((1, 6))
assert (1, 6) == scan_planners.enforce_xy_tuple([1, 6])
assert scan_planners.enforce_xy_tuple((1, 6)) == (1, 6)
assert scan_planners.enforce_xy_tuple([1, 6]) == (1, 6)
def test_enforce_xyz_tuple():
@ -39,8 +39,8 @@ def test_enforce_xyz_tuple():
with pytest.raises(TypeError):
scan_planners.enforce_xyz_tuple(value)
assert (1, 6, 2) == scan_planners.enforce_xyz_tuple((1, 6, 2))
assert (1, 6, 6) == scan_planners.enforce_xyz_tuple([1, 6, 6])
assert scan_planners.enforce_xyz_tuple((1, 6, 2)) == (1, 6, 2)
assert scan_planners.enforce_xyz_tuple([1, 6, 6]) == (1, 6, 6)
def test_base_class_not_implemented():