Skip to content

Commit 2b9ce94

Browse files
committed
Fixed ruff check issues
Fixed issue where exceptions raised from within exceptions where not passed the context. Added defaults for stacklevel when using warn(). Removed redundant comparison for void test function. Disabled warning for use of private functions in tests. Disabled warning for unneeded list comprehension which I believe is required. Fixed line too long
1 parent 5a58f70 commit 2b9ce94

File tree

6 files changed

+24
-18
lines changed

6 files changed

+24
-18
lines changed

atip/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""ATIP: Accelerator Toolbox Interface for Pytac.
22
See README.rst & INSTALL.rst for more information.
33
"""
4+
45
from . import load_sim, sim_data_sources, simulator, utils
56

67
__all__ = ["load_sim", "sim_data_sources", "simulator", "utils"]

src/atip/simulator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def _recalculate_phys_data(self, callback):
197197
self._at_lat, self._rp, self._disable_emittance
198198
)
199199
except Exception as e:
200-
warn(at.AtWarning(e))
200+
warn(at.AtWarning(e), stacklevel=1)
201201
# Signal the up to date flag since the physics data is now up to date.
202202
# We do this before the callback is executed in case the callback
203203
# checks the flag.

src/atip/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def preload_at(at_lat):
7373
class elems:
7474
pass
7575

76-
elems.all = [elem for elem in at_lat]
76+
elems.all = [elem for elem in at_lat] # noqa: C416
7777
elems_dict = {
7878
type_: []
7979
for type_ in [
@@ -142,7 +142,7 @@ def get_atsim(target):
142142
if isinstance(target, atip.simulator.ATSimulator):
143143
return target
144144
else: # Pytac lattice
145-
return target._data_source_manager._data_sources[pytac.SIM]._atsim
145+
return target._data_source_manager._data_sources[pytac.SIM]._atsim # noqa: SLF001
146146

147147

148148
def get_sim_lattice(target):

src/virtac/atip_ioc_entry.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,22 @@ def main():
7878
if 5064 in ports_list or 5065 in ports_list:
7979
warn(
8080
f"At least one of {epics_env_vars} is set to 5064 or 5065"
81-
+ conflict_warning
81+
+ conflict_warning,
82+
stacklevel=1,
8283
)
8384
elif all(port == 0 for port in ports_list):
8485
warn(
8586
"No EPICS port set, default base port (5064) will be used"
86-
+ conflict_warning
87+
+ conflict_warning,
88+
stacklevel=1,
8789
)
8890
# Avoid PV conflict between multiple IP interfaces on the same machine.
8991
primary_ip = socket.gethostbyname(socket.getfqdn())
9092
if "EPICS_CAS_INTF_ADDR_LIST" in os.environ.keys():
91-
warn("Pre-existing 'EPICS_CAS_INTF_ADDR_LIST' value" + conflict_warning)
93+
warn(
94+
"Pre-existing 'EPICS_CAS_INTF_ADDR_LIST' value" + conflict_warning,
95+
stacklevel=1,
96+
)
9297
else:
9398
os.environ["EPICS_CAS_INTF_ADDR_LIST"] = primary_ip
9499
os.environ["EPICS_CAS_BEACON_ADDR_LIST"] = primary_ip

src/virtac/atip_server.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,10 @@ def _create_feedback_records(self, feedback_csv, disable_emittance):
309309
try:
310310
readonly = ast.literal_eval(line["read-only"])
311311
assert isinstance(readonly, bool)
312-
except (ValueError, AssertionError):
312+
except (ValueError, AssertionError) as exc:
313313
raise ValueError(
314314
f"Unable to evaluate {line['read-only']} as a boolean."
315-
)
315+
) from exc
316316
prefix, suffix = line["pv"].split(":", 1)
317317
builder.SetDeviceName(prefix)
318318
if readonly:
@@ -462,7 +462,7 @@ def monitor_mirrored_pvs(self):
462462
try:
463463
self._monitored_pvs[pv] = camonitor(pv, mask.callback)
464464
except Exception as e:
465-
warn(e)
465+
warn(e, stacklevel=1)
466466

467467
def refresh_record(self, pv_name):
468468
"""For a given PV refresh the time-stamp of the associated record,
@@ -473,10 +473,10 @@ def refresh_record(self, pv_name):
473473
"""
474474
try:
475475
record = self.all_record_names[pv_name]
476-
except KeyError:
476+
except KeyError as exc:
477477
raise ValueError(
478478
f"{pv_name} is not the name of a record created by this server."
479-
)
479+
) from exc
480480
else:
481481
record.set(record.get())
482482

@@ -513,7 +513,7 @@ def setup_tune_feedback(self, tune_csv=None):
513513
line["delta"], mask.callback
514514
)
515515
except Exception as e:
516-
warn(e)
516+
warn(e, stacklevel=1)
517517

518518
def stop_all_monitoring(self):
519519
"""Stop monitoring mirrored records and tune feedback offsets."""
@@ -546,12 +546,13 @@ def set_feedback_record(self, index, field, value):
546546
"""
547547
try:
548548
self._feedback_records[(index, field)].set(value)
549-
except KeyError:
549+
except KeyError as exc:
550550
if index == 0:
551551
raise FieldException(
552552
f"Simulated lattice {self.lattice} does not have field {field}."
553-
)
553+
) from exc
554554
else:
555555
raise FieldException(
556-
f"Simulated element {self.lattice[index]} does not have field {field}."
557-
)
556+
f"Simulated element {self.lattice[index]} does not have "
557+
f"field {field}."
558+
) from exc

tests/test_at_simulator_object.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ def test_toggle_calculations_and_wait_for_calculations(atsim, initial_phys_data)
199199
atsim._at_lat[5].PolynomB[1] = 2.5
200200
atsim.queue_set(mock.Mock(), "f", 0)
201201
assert atsim.wait_for_calculations(2) is False
202-
_check_initial_phys_data(atsim, initial_phys_data) is True
203-
atsim.toggle_calculations()
202+
_check_initial_phys_data(atsim, initial_phys_data)
204203
atsim.queue_set(mock.Mock(), "f", 0)
205204
assert atsim.wait_for_calculations() is True
206205
# Physics data has changed.

0 commit comments

Comments
 (0)