Skip to content

Commit d628c88

Browse files
committed
Fix ruff check complaints
1 parent b4c2389 commit d628c88

File tree

11 files changed

+64
-65
lines changed

11 files changed

+64
-65
lines changed

src/pytac/__init__.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@
1818
# Default argument flag.
1919
DEFAULT = "default"
2020

21-
from . import (
22-
data_source,
23-
device,
24-
element,
25-
exceptions,
26-
lattice,
27-
load_csv,
28-
units,
29-
utils,
30-
)
3121

3222
__all__ = [
3323
"__version__",

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def pytest_sessionstart():
3131
(caget and caput).
3232
"""
3333

34-
class ca_nothing(Exception):
34+
class ca_nothing(Exception): # noqa: N801, N818
3535
"""A minimal mock of the cothread ca_nothing exception class."""
3636

3737
def __init__(self, name, errorcode=True):

tests/test_cothread_cs.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_set_multiple_calls_caput_correctly(cs):
5555
)
5656

5757

58-
def test_get_multiple_raises_ControlSystemException(cs):
58+
def test_get_multiple_raises_control_system_exception(cs):
5959
"""Here we check that errors are thrown, suppressed and logged correctly."""
6060
caget.return_value = [12, ca_nothing("pv", False)]
6161
with pytest.raises(pytac.exceptions.ControlSystemException):
@@ -65,7 +65,7 @@ def test_get_multiple_raises_ControlSystemException(cs):
6565
log.check(("root", "WARNING", "Cannot connect to pv."))
6666

6767

68-
def test_set_multiple_raises_ControlSystemException(cs):
68+
def test_set_multiple_raises_control_system_exception(cs):
6969
"""Here we check that errors are thrown, suppressed and logged correctly."""
7070
caput.return_value = [ca_nothing("pv1", True), ca_nothing("pv2", False)]
7171
with pytest.raises(pytac.exceptions.ControlSystemException):
@@ -75,7 +75,7 @@ def test_set_multiple_raises_ControlSystemException(cs):
7575
log.check(("root", "WARNING", "Cannot connect to pv2."))
7676

7777

78-
def test_get_single_raises_ControlSystemException(cs):
78+
def test_get_single_raises_control_system_exception(cs):
7979
"""Here we check that errors are thrown, suppressed and logged correctly."""
8080
caget.side_effect = ca_nothing("pv", False)
8181
with LogCapture() as log:
@@ -85,7 +85,7 @@ def test_get_single_raises_ControlSystemException(cs):
8585
log.check(("root", "WARNING", "Cannot connect to prefix:rb."))
8686

8787

88-
def test_set_single_raises_ControlSystemException(cs):
88+
def test_set_single_raises_control_system_exception(cs):
8989
"""Here we check that errors are thrown, suppressed and logged correctly."""
9090
caput.side_effect = ca_nothing("pv", False)
9191
with LogCapture() as log:
@@ -95,7 +95,7 @@ def test_set_single_raises_ControlSystemException(cs):
9595
log.check(("root", "WARNING", "Cannot connect to prefix:sp."))
9696

9797

98-
def test_set_multiple_raises_ValueError_on_input_length_mismatch(cs):
98+
def test_set_multiple_raises_value_error_on_input_length_mismatch(cs):
9999
with pytest.raises(ValueError):
100100
cs.set_multiple([SP_PV], [42, 6])
101101
with pytest.raises(ValueError):

tests/test_device.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_get_simple_device_value_with_handle():
6161
assert device.get_value(handle=pytac.RB) == 1.0
6262

6363

64-
def test_simple_device_raises_DataSourceException_if_readonly_and_set_value_called():
64+
def test_simple_device_raises_data_source_exception_if_readonly_and_set_value_called():
6565
device = SimpleDevice(10, readonly=True)
6666
with pytest.raises(DataSourceException):
6767
device.set_value(4)
@@ -79,7 +79,7 @@ def test_device_is_enabled_by_default(device_creation_function):
7979
@pytest.mark.parametrize(
8080
"device_creation_function", [create_epics_device, create_simple_device]
8181
)
82-
def test_device_is_disabled_if_False_enabler(device_creation_function):
82+
def test_device_is_disabled_if_false_enabler(device_creation_function):
8383
device = device_creation_function(enabled=False)
8484
assert not device.is_enabled()
8585

@@ -93,7 +93,7 @@ def test_device_is_enabled_returns_bool_value(device_creation_function):
9393

9494

9595
# PvEnabler test.
96-
def test_PvEnabler(mock_cs):
96+
def test_pv_enabler(mock_cs):
9797
pve = PvEnabler("enable-pv", 40, mock_cs)
9898
assert pve
9999
mock_cs.get_single.return_value = 50

tests/test_element.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_create_element():
1818
assert e._lattice == lat
1919

2020

21-
def test_element_properties_are_None_without_lattice():
21+
def test_element_properties_are_none_without_lattice():
2222
e = Element(1.2, "SEXT")
2323
assert e.index is None
2424
assert e.s is None
@@ -53,7 +53,7 @@ def test_add_element_to_family_and_case_insensitive_retrieval():
5353
assert e.is_in_family("FAM")
5454

5555

56-
def test_device_methods_raise_DataSourceException_if_no_live_data_source(
56+
def test_device_methods_raise_data_source_exception_if_no_live_data_source(
5757
simple_element,
5858
):
5959
basic_element = simple_element
@@ -66,7 +66,7 @@ def test_device_methods_raise_DataSourceException_if_no_live_data_source(
6666
basic_element.get_device("x")
6767

6868

69-
def test_get_device_raises_KeyError_if_device_not_present(simple_element):
69+
def test_get_device_raises_key_error_if_device_not_present(simple_element):
7070
with pytest.raises(pytac.exceptions.FieldException):
7171
simple_element.get_device("not-a-device")
7272

@@ -76,15 +76,15 @@ def test_get_unitconv_returns_unitconv_object(simple_element, unit_uc, double_uc
7676
assert simple_element.get_unitconv("y") == double_uc
7777

7878

79-
def test_set_unit_conv(simple_element):
79+
def test_set_unitconv(simple_element):
8080
with pytest.raises(KeyError):
8181
simple_element._data_source_manager._uc["field1"]
8282
uc = mock.Mock()
8383
simple_element.set_unitconv("field1", uc)
8484
assert simple_element._data_source_manager._uc["field1"] == uc
8585

8686

87-
def test_get_unitconv_raises_FieldException_if_device_not_present(simple_element):
87+
def test_get_unitconv_raises_field_exception_if_device_not_present(simple_element):
8888
with pytest.raises(pytac.exceptions.FieldException):
8989
simple_element.get_unitconv("not-a-device")
9090

@@ -144,7 +144,7 @@ def test_identity_conversion(simple_element):
144144

145145

146146
def test_get_fields(simple_element):
147-
assert set(simple_element.get_fields()[pytac.LIVE]) == set(["y", "x"])
147+
assert set(simple_element.get_fields()[pytac.LIVE]) == {"y", "x"}
148148

149149

150150
def test_element_representation():

tests/test_epics.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ def test_get_value_uses_cs_if_data_source_live(simple_epics_element, mock_cs):
9393
mock_cs.get_single.assert_called_with(RB_PV, True)
9494

9595

96-
def test_get_value_raises_HandleExceptions(simple_epics_element):
96+
def test_get_value_raises_handle_exceptions(simple_epics_element):
9797
with pytest.raises(pytac.exceptions.HandleException):
9898
simple_epics_element.get_value("y", "unknown_handle")
9999

100100

101-
def test_lattice_get_pv_name_raises_DataSourceException(simple_epics_lattice):
101+
def test_lattice_get_pv_name_raises_data_source_exception(simple_epics_lattice):
102102
basic_epics_lattice = simple_epics_lattice
103103
with pytest.raises(pytac.exceptions.DataSourceException):
104104
basic_epics_lattice.get_pv_name("basic", pytac.RB)
@@ -107,7 +107,7 @@ def test_lattice_get_pv_name_raises_DataSourceException(simple_epics_lattice):
107107
basic_epics_lattice.get_pv_name("x", pytac.RB)
108108

109109

110-
def test_set_element_values_length_mismatch_raises_IndexError(simple_epics_lattice):
110+
def test_set_element_values_length_mismatch_raises_index_error(simple_epics_lattice):
111111
with pytest.raises(IndexError):
112112
simple_epics_lattice.set_element_values("family", "x", [1, 2])
113113
with pytest.raises(IndexError):
@@ -125,6 +125,6 @@ def test_element_get_pv_name_raises_exceptions(simple_epics_element):
125125
basic_epics_element.get_pv_name("x", pytac.RB)
126126

127127

128-
def test_create_EpicsDevice_raises_DataSourceException_if_no_PVs_are_given():
128+
def test_create_epics_device_raises_data_source_exception_if_no_PVs_are_given(): # noqa: N802
129129
with pytest.raises(pytac.exceptions.DataSourceException):
130130
pytac.device.EpicsDevice("device_1", "a_control_system")

tests/test_invalid_classes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pytac import cs, data_source, device
44

55

6-
def test_ControlSystem_throws_NotImplementedError():
6+
def test_control_system_throws_not_implemented_error():
77
test_cs = cs.ControlSystem()
88
with pytest.raises(NotImplementedError):
99
test_cs.get_single("dummy", "throw")
@@ -15,7 +15,7 @@ def test_ControlSystem_throws_NotImplementedError():
1515
test_cs.set_multiple(["dummy_1", "dummy_2"], [1, 2], "throw")
1616

1717

18-
def test_DataSource_throws_NotImplementedError():
18+
def test_data_source_throws_not_implemented_error():
1919
test_ds = data_source.DataSource()
2020
with pytest.raises(NotImplementedError):
2121
test_ds.get_fields()
@@ -25,7 +25,7 @@ def test_DataSource_throws_NotImplementedError():
2525
test_ds.set_value("field", 0.0, "throw")
2626

2727

28-
def test_Device_throws_NotImplementedError():
28+
def test_device_throws_not_implemented_error():
2929
test_d = device.Device()
3030
with pytest.raises(NotImplementedError):
3131
test_d.is_enabled()

tests/test_lattice.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_lattice_without_symmetry():
3939

4040
def test_lattice_cell_properties():
4141
lat = Lattice("", 2)
42-
for i in range(5):
42+
for _ in range(5):
4343
lat.add_element(Element(0.5, "DRIFT"))
4444
assert lat.cell_length == 1.25
4545
assert lat.cell_bounds == [1, 4, 5]
@@ -51,7 +51,7 @@ def test_get_element_devices(simple_lattice):
5151
assert devices[0].name == "x_device"
5252

5353

54-
def test_device_methods_raise_DataSourceException_if_no_live_data_source(
54+
def test_device_methods_raise_data_source_exception_if_no_live_data_source(
5555
simple_lattice,
5656
):
5757
basic_lattice = simple_lattice
@@ -64,7 +64,7 @@ def test_device_methods_raise_DataSourceException_if_no_live_data_source(
6464
basic_lattice.get_device("x")
6565

6666

67-
def test_get_unitconv_raises_FieldException_if_no_uc_for_field(simple_lattice):
67+
def test_get_unitconv_raises_field_exception_if_no_uc_for_field(simple_lattice):
6868
with pytest.raises(pytac.exceptions.FieldException):
6969
simple_lattice.get_unitconv("not_a_field")
7070

@@ -93,7 +93,7 @@ def test_set_value_raises_exceptions_correctly(simple_lattice):
9393
simple_lattice.set_value("not_a_field", 0)
9494

9595

96-
def test_get_element_devices_raises_ValueError_for_mismatched_family(simple_lattice):
96+
def test_get_element_devices_raises_value_error_for_mismatched_family(simple_lattice):
9797
with pytest.raises(ValueError):
9898
devices = simple_lattice.get_element_devices("not-a-family", "x")
9999
basic_element = simple_lattice.get_elements("family")[0]
@@ -104,7 +104,9 @@ def test_get_element_devices_raises_ValueError_for_mismatched_family(simple_latt
104104
assert devices == []
105105

106106

107-
def test_get_element_devices_raises_FieldException_if_field_not_matched(simple_lattice):
107+
def test_get_element_devices_raises_field_exception_if_field_not_matched(
108+
simple_lattice,
109+
):
108110
with pytest.raises(pytac.exceptions.FieldException):
109111
simple_lattice.get_element_devices("family", "not-a-field")
110112

@@ -176,7 +178,7 @@ def test_set_element_values(simple_lattice):
176178
)
177179

178180

179-
def test_set_element_values_raises_Exceptions_correctly(simple_lattice):
181+
def test_set_element_values_raises_exceptions_correctly(simple_lattice):
180182
with pytest.raises(IndexError):
181183
simple_lattice.set_element_values("family", "x", [1, 2])
182184
with pytest.raises(IndexError):
@@ -240,7 +242,7 @@ def test_convert_family_values(simple_lattice):
240242
assert post_values == [12]
241243

242244

243-
def test_convert_family_values_length_mismatch_raises_IndexError(simple_lattice):
245+
def test_convert_family_values_length_mismatch_raises_index_error(simple_lattice):
244246
with pytest.raises(IndexError):
245247
simple_lattice.convert_family_values(
246248
"family", "x", [1, 2], pytac.ENG, pytac.PHYS

tests/test_load.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
@pytest.fixture
13-
def mock_cs_raises_ImportError():
13+
def mock_cs_raises_import_error():
1414
"""We create a mock control system to replace CothreadControlSystem, so
1515
that we can check that when it raises an ImportError load_csv.load
1616
catches it and raises a ControlSystemException instead.
@@ -21,8 +21,9 @@ def mock_cs_raises_ImportError():
2121
ImportError when the code is compiled.
2222
"""
2323

24-
def CothreadControlSystem():
25-
raise ImportError
24+
class CothreadControlSystem:
25+
def __init__(self):
26+
raise ImportError
2627

2728
return CothreadControlSystem
2829

@@ -37,13 +38,13 @@ def test_default_control_system_import():
3738
assert isinstance(load(TESTING_MODE)._cs, pytac.cothread_cs.CothreadControlSystem)
3839

3940

40-
def test_import_fail_raises_ControlSystemException(mock_cs_raises_ImportError):
41+
def test_import_fail_raises_control_system_exception(mock_cs_raises_import_error):
4142
"""In this test we:
4243
- check that load corectly fails if cothread cannot be imported
4344
- check that when the import of the CothreadControlSystem fails the
4445
ImportError raised is replaced with a ControlSystemException
4546
"""
46-
with patch("pytac.cothread_cs.CothreadControlSystem", mock_cs_raises_ImportError):
47+
with patch("pytac.cothread_cs.CothreadControlSystem", mock_cs_raises_import_error):
4748
with pytest.raises(pytac.exceptions.ControlSystemException):
4849
load(TESTING_MODE)
4950

@@ -69,10 +70,16 @@ def test_devices_loaded(lattice):
6970

7071

7172
def test_families_loaded(lattice):
72-
assert lattice.get_all_families() == set(
73-
["drift", "sext", "quad", "ds", "qf", "qs", "sd"]
74-
)
75-
assert lattice.get_elements("quad")[0].families == set(["quad", "qf", "qs"])
73+
assert lattice.get_all_families() == {
74+
"drift",
75+
"sext",
76+
"quad",
77+
"ds",
78+
"qf",
79+
"qs",
80+
"sd",
81+
}
82+
assert lattice.get_elements("quad")[0].families == {"quad", "qf", "qs"}
7683

7784

7885
def test_load_unitconv_warns_if_pchip_or_poly_data_file_not_found(
@@ -94,7 +101,7 @@ def test_load_unitconv_warns_if_pchip_or_poly_data_file_not_found(
94101
)
95102

96103

97-
def test_resolve_unitconv_raises_UnitsException_if_pchip_or_poly_data_file_not_found(
104+
def test_resolve_unitconv_raises_units_exception_if_pchip_or_poly_data_file_not_found(
98105
polyconv_file, pchipconv_file
99106
):
100107
uc_params = {
@@ -119,7 +126,7 @@ def test_resolve_unitconv_raises_UnitsException_if_pchip_or_poly_data_file_not_f
119126
resolve_unitconv(uc_params, {}, polyconv_file, pchipconv_file)
120127

121128

122-
def test_resolve_unitconv_raises_UnitsException_if_unrecognised_UnitConv_type(
129+
def test_resolve_unitconv_raises_units_exception_if_unrecognised_unitconv_type(
123130
polyconv_file, pchipconv_file
124131
):
125132
uc_params = {

tests/test_machine.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def test_load_quadrupoles(lattice, n_quads, request):
9999
quads = lattice.get_elements("Quadrupole")
100100
assert len(quads) == n_quads
101101
for quad in quads:
102-
assert set(quad.get_fields()[pytac.LIVE]) == set(["b1"])
102+
assert set(quad.get_fields()[pytac.LIVE]) == {"b1"}
103103
device = quad.get_device("b1")
104104
assert re.match("SR.*Q.*:I", device.rb_pv)
105105
assert re.match("SR.*Q.*:SETI", device.sp_pv)
@@ -197,11 +197,11 @@ def test_quad_unitconv_raise_exception():
197197

198198

199199
def test_quad_unitconv_known_failing_test():
200-
LAT_ENERGY = 3000
200+
lat_energy = 3000
201201

202202
uc = pytac.units.PchipUnitConv([50.0, 100.0, 180.0], [-4.95, -9.85, -17.56])
203-
uc._post_eng_to_phys = pytac.utils.get_div_rigidity(LAT_ENERGY)
204-
uc._pre_phys_to_eng = pytac.utils.get_mult_rigidity(LAT_ENERGY)
203+
uc._post_eng_to_phys = pytac.utils.get_div_rigidity(lat_energy)
204+
uc._pre_phys_to_eng = pytac.utils.get_mult_rigidity(lat_energy)
205205
numpy.testing.assert_allclose(uc.eng_to_phys(70), -0.69133465)
206206
numpy.testing.assert_allclose(uc.phys_to_eng(-0.7), 70.8834284954)
207207

0 commit comments

Comments
 (0)