Skip to content

Commit 0ca0d0d

Browse files
committed
Fix test and run ruff and pyright on all affected files
1 parent 1a5d0bd commit 0ca0d0d

File tree

5 files changed

+109
-62
lines changed

5 files changed

+109
-62
lines changed

BlockServer/config/block.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# along with this program; if not, you can obtain a copy from
1414
# https://www.eclipse.org/org/documents/epl-v10.php or
1515
# http://opensource.org/licenses/eclipse-1.0.php
16-
from typing import Dict, Union
16+
from typing import Dict, TypedDict, Union
1717

1818
from server_common.helpers import PVPREFIX_MACRO
1919

@@ -41,17 +41,17 @@ def __init__(
4141
pv: str,
4242
local: bool = True,
4343
visible: bool = True,
44-
component: str = None,
44+
component: str | None = None,
4545
runcontrol: bool = False,
46-
lowlimit: float = None,
47-
highlimit: float = None,
46+
lowlimit: float | None = None,
47+
highlimit: float | None = None,
4848
suspend_on_invalid: bool = False,
4949
log_periodic: bool = False,
5050
log_rate: float = 5,
5151
log_deadband: float = 0,
5252
set_block: bool = False,
53-
set_block_val: str = None,
54-
):
53+
set_block_val: str | None = None,
54+
) -> None:
5555
"""Constructor.
5656
5757
Args:
@@ -92,24 +92,26 @@ def _get_pv(self) -> str:
9292
pv_name = PVPREFIX_MACRO + self.pv
9393
return pv_name
9494

95-
def set_visibility(self, visible: bool):
95+
def set_visibility(self, visible: bool) -> None:
9696
"""Toggle the visibility of the block.
9797
9898
Args:
9999
visible: Whether the block is visible or not
100100
"""
101101
self.visible = visible
102102

103-
def __str__(self):
103+
def __str__(self) -> str:
104104
set_block_str = ""
105105
if self.set_block:
106106
set_block_str = f", SetBlockVal: {self.set_block_val}"
107107
return (
108-
f"Name: {self.name}, PV: {self.pv}, Local: {self.local}, Visible: {self.visible}, Component: {self.component}"
109-
f", RCEnabled: {self.rc_enabled}, RCLow: {self.rc_lowlimit}, RCHigh: {self.rc_highlimit}{set_block_str}"
108+
f"Name: {self.name}, PV: {self.pv}, Local: {self.local}, "
109+
f"Visible: {self.visible}, Component: {self.component}"
110+
f", RCEnabled: {self.rc_enabled}, RCLow: {self.rc_lowlimit}, "
111+
f"RCHigh: {self.rc_highlimit}{set_block_str}"
110112
)
111113

112-
def to_dict(self) -> Dict[str, Union[str, float, bool]]:
114+
def to_dict(self) -> Dict[str, Union[str, float, bool, None]]:
113115
"""Puts the block's details into a dictionary.
114116
115117
Returns:
@@ -131,3 +133,17 @@ def to_dict(self) -> Dict[str, Union[str, float, bool]]:
131133
"set_block": self.set_block,
132134
"set_block_val": self.set_block_val,
133135
}
136+
137+
138+
class BlockKwargs(TypedDict, total=False):
139+
visible: bool
140+
component: str | None
141+
runcontrol: bool
142+
lowlimit: float | None
143+
highlimit: float | None
144+
suspend_on_invalid: bool
145+
log_periodic: bool
146+
log_rate: float
147+
log_deadband: float
148+
set_block: bool
149+
set_block_val: str | None

BlockServer/config/configuration.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
"""Contains all the code for defining a configuration or component"""
1818

1919
from collections import OrderedDict
20-
from typing import Dict
20+
from typing import Dict, Unpack
2121

22-
from BlockServer.config.block import Block
22+
from server_common.helpers import PVPREFIX_MACRO
23+
from server_common.utilities import print_and_log
24+
25+
from BlockServer.config.block import Block, BlockKwargs
2326
from BlockServer.config.group import Group
2427
from BlockServer.config.ioc import IOC
2528
from BlockServer.config.metadata import MetaData
2629
from BlockServer.core.constants import GRP_NONE
27-
from server_common.helpers import PVPREFIX_MACRO
28-
from server_common.utilities import print_and_log
2930

3031

3132
class Configuration:
@@ -41,7 +42,7 @@ class Configuration:
4142
is_component (bool): Whether it is actually a component
4243
"""
4344

44-
def __init__(self, macros: Dict):
45+
def __init__(self, macros: Dict) -> None:
4546
"""Constructor.
4647
4748
Args:
@@ -56,7 +57,14 @@ def __init__(self, macros: Dict):
5657
self.components = OrderedDict()
5758
self.is_component = False
5859

59-
def add_block(self, name: str, pv: str, group: str = GRP_NONE, local: bool = True, **kwargs):
60+
def add_block(
61+
self,
62+
name: str,
63+
pv: str,
64+
group: str = GRP_NONE,
65+
local: bool = True,
66+
**kwargs: Unpack[BlockKwargs],
67+
) -> None:
6068
"""Add a block to the configuration.
6169
6270
Args:
@@ -85,15 +93,15 @@ def add_block(self, name: str, pv: str, group: str = GRP_NONE, local: bool = Tru
8593
def add_ioc(
8694
self,
8795
name: str,
88-
component: str = None,
89-
autostart: bool = None,
90-
restart: bool = None,
91-
macros: Dict = None,
92-
pvs: Dict = None,
93-
pvsets: Dict = None,
94-
simlevel: str = None,
95-
remotePvPrefix: str = None,
96-
):
96+
component: str | None = None,
97+
autostart: bool | None = None,
98+
restart: bool | None = None,
99+
macros: Dict | None = None,
100+
pvs: Dict | None = None,
101+
pvsets: Dict | None = None,
102+
simlevel: str | None = None,
103+
remote_pv_prefix: str | None = None,
104+
) -> None:
97105
"""Add an IOC to the configuration.
98106
99107
Args:
@@ -115,7 +123,15 @@ def add_ioc(
115123
)
116124
else:
117125
self.iocs[name.upper()] = IOC(
118-
name, autostart, restart, component, macros, pvs, pvsets, simlevel, remotePvPrefix
126+
name,
127+
autostart if autostart is not None else True,
128+
restart if restart is not None else True,
129+
component,
130+
macros,
131+
pvs,
132+
pvsets,
133+
simlevel,
134+
remote_pv_prefix,
119135
)
120136

121137
def get_name(self) -> str:
@@ -128,7 +144,7 @@ def get_name(self) -> str:
128144
self.meta.name.decode("utf-8") if isinstance(self.meta.name, bytes) else self.meta.name
129145
)
130146

131-
def set_name(self, name: str):
147+
def set_name(self, name: str) -> None:
132148
"""Sets the configuration's name.
133149
134150
Args:

BlockServer/config/ioc.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ def __init__(
3939
name: str,
4040
autostart: bool = True,
4141
restart: bool = True,
42-
component: str = None,
43-
macros: Dict = None,
44-
pvs: Dict = None,
45-
pvsets: Dict = None,
46-
simlevel: str = None,
47-
remote_pv_prefix: str = None,
42+
component: str | None = None,
43+
macros: Dict | None = None,
44+
pvs: Dict | None = None,
45+
pvsets: Dict | None = None,
46+
simlevel: str | None = None,
47+
remotePvPrefix: str | None = None, # noqa: N803
4848
) -> None:
4949
"""Constructor.
5050
@@ -60,13 +60,14 @@ def __init__(
6060
pvs: The IOC's PVs
6161
pvsets: The IOC's PV sets
6262
simlevel: The level of simulation
63-
remote_pv_prefix: The remote pv prefix
63+
remotePvPrefix: The remote pv prefix,
64+
has to be formatted like this to be read in properly.
6465
"""
6566
self.name = name
6667
self.autostart = autostart
6768
self.restart = restart
6869
self.component = component
69-
self.remote_pv_prefix = remote_pv_prefix
70+
self.remote_pv_prefix = remotePvPrefix
7071

7172
if simlevel is None:
7273
self.simlevel = "none"
@@ -75,13 +76,18 @@ def __init__(
7576

7677
self.macros = {}
7778
if macros is not None:
78-
# Remove macros that are set to use default, they can be gotten from config.xml
79-
# so there is no need for them to be stored in the config.
80-
for name, data in macros.items():
81-
if not ("useDefault" in data and data["useDefault"]):
82-
self.macros.update({name: data})
83-
self.macros[name].pop("useDefault")
84-
79+
if isinstance(macros, dict):
80+
# Remove macros that are set to use default, they can be gotten from config.xml
81+
# so there is no need for them to be stored in the config.
82+
print(macros)
83+
for name, data in macros.items():
84+
if "useDefault" in data and not data["useDefault"]:
85+
self.macros.update({name: data})
86+
self.macros[name].pop("useDefault")
87+
elif "useDefault" not in data:
88+
self.macros.update({name: data})
89+
else:
90+
self.macros = macros
8591
if pvs is None:
8692
self.pvs = {}
8793
else:

BlockServer/core/config_holder.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __init__(
4343
macros: Dict,
4444
file_manager: ConfigurationFileManager,
4545
is_component: bool = False,
46-
test_config: Configuration = None,
46+
test_config: Configuration | None = None,
4747
) -> None:
4848
"""Constructor.
4949
@@ -127,7 +127,7 @@ def get_blocknames(self) -> List[str]:
127127
names.append(block.name)
128128
return names
129129

130-
def get_block_details(self):
130+
def get_block_details(self) -> Dict:
131131
"""Get the configuration details for all the blocks including any in components.
132132
133133
Returns:
@@ -183,7 +183,7 @@ def get_group_details(self) -> Dict[str, Group]:
183183

184184
return groups
185185

186-
def _set_group_details(self, redefinition) -> None:
186+
def _set_group_details(self, redefinition: List[Dict]) -> None:
187187
# Any redefinition only affects the main configuration
188188
homeless_blocks = self.get_blocknames()
189189
for grp in redefinition:
@@ -245,15 +245,15 @@ def get_ioc_names(self, include_base: bool = False) -> List[str]:
245245
iocs.extend(cv.iocs)
246246
return iocs
247247

248-
def get_ioc_details(self):
248+
def get_ioc_details(self) -> Dict:
249249
"""Get the details of the IOCs in the configuration.
250250
251251
Returns:
252252
A copy of all the configuration IOC details
253253
"""
254254
return copy.deepcopy(self._config.iocs)
255255

256-
def get_component_ioc_details(self):
256+
def get_component_ioc_details(self) -> Dict:
257257
"""Get the details of the IOCs in any components.
258258
259259
Returns:
@@ -266,7 +266,7 @@ def get_component_ioc_details(self):
266266
iocs[ioc_name] = ioc
267267
return iocs
268268

269-
def get_all_ioc_details(self):
269+
def get_all_ioc_details(self) -> Dict:
270270
"""Get the details of the IOCs in the configuration and any components.
271271
272272
Returns:
@@ -364,22 +364,22 @@ def is_dynamic(self) -> bool:
364364
"""
365365
return self._config.meta.isDynamic
366366

367-
def configures_block_gateway_and_archiver(self):
367+
def configures_block_gateway_and_archiver(self) -> bool:
368368
"""
369369
Returns:
370370
(bool): Whether this config has a gwblock.pvlist and block_config.xml to configure the
371371
block gateway and archiver with.
372372
"""
373373
return self._config.meta.configuresBlockGWAndArchiver
374374

375-
def _comps_to_list(self):
375+
def _comps_to_list(self) -> List:
376376
comps = []
377377
for component_name, component_value in self._components.items():
378378
if component_name.lower() != DEFAULT_COMPONENT.lower():
379379
comps.append({"name": component_value.get_name()})
380380
return comps
381381

382-
def _blocks_to_list(self, expand_macro: bool = False):
382+
def _blocks_to_list(self, expand_macro: bool = False) -> List:
383383
blocks = self.get_block_details()
384384
blks = []
385385
if blocks is not None:
@@ -391,7 +391,7 @@ def _blocks_to_list(self, expand_macro: bool = False):
391391
blks.append(b)
392392
return blks
393393

394-
def _groups_to_list(self):
394+
def _groups_to_list(self) -> List:
395395
groups = self.get_group_details()
396396
grps = []
397397
if groups is not None:
@@ -404,18 +404,18 @@ def _groups_to_list(self):
404404
grps.append(groups[GRP_NONE.lower()].to_dict())
405405
return grps
406406

407-
def _iocs_to_list(self):
407+
def _iocs_to_list(self) -> List[Dict]:
408408
return [ioc.to_dict() for ioc in self._config.iocs.values()]
409409

410-
def _iocs_to_list_with_components(self):
410+
def _iocs_to_list_with_components(self) -> List[Dict]:
411411
ioc_list = self._iocs_to_list()
412412

413413
for component in self._components.values():
414414
for ioc in component.iocs.values():
415415
ioc_list.append(ioc.to_dict())
416416
return ioc_list
417417

418-
def _to_dict(self, data_list):
418+
def _to_dict(self, data_list: List) -> None | Dict:
419419
return None if data_list is None else {item["name"]: item for item in data_list}
420420

421421
def set_config(self, config: Configuration, is_component: bool = False) -> None:

0 commit comments

Comments
 (0)