Skip to content

Commit 19a2f71

Browse files
authored
Merge pull request #44 from ISISComputingGroup/Ticket8553_pyright_day_genie_advanced
pyright fixes for genie_advanced
2 parents af56258 + 42c2a43 commit 19a2f71

File tree

3 files changed

+72
-33
lines changed

3 files changed

+72
-33
lines changed

src/genie_python/genie_advanced.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
"""
66

77
import contextlib
8-
from datetime import datetime, timedelta
8+
from datetime import UTC, datetime, timedelta
99
from time import sleep
10-
from typing import Any, Callable, Iterator, TypedDict
10+
from typing import Any, Iterator, Protocol, TypedDict
1111

12+
import numpy as np
1213
import numpy.typing as npt
1314

1415
from genie_python.genie_api_setup import (
@@ -21,6 +22,11 @@
2122
from genie_python.utilities import check_break
2223

2324

25+
class PrePostCmd(Protocol):
26+
def __call__(self, **kwargs: Any) -> str | None:
27+
pass
28+
29+
2430
@usercommand
2531
@helparglist("")
2632
def get_manager_mode() -> bool:
@@ -52,7 +58,7 @@ def assert_in_manager_mode() -> None:
5258

5359

5460
@contextlib.contextmanager
55-
def motor_in_set_mode(pv_name: str) -> Iterator:
61+
def motor_in_set_mode(pv_name: str) -> Iterator[None]:
5662
"""
5763
Uses a context to place motor into set mode and ensure that it leaves
5864
set mode after context has ended. If it can not set the mode correctly
@@ -134,7 +140,7 @@ def get_pv_from_block(block: str) -> str:
134140
@usercommand
135141
@helparglist("pv str")
136142
@log_command_and_handle_exception
137-
def pv_exists(pv: str, is_local: bool = False) -> None:
143+
def pv_exists(pv: str, is_local: bool = False) -> bool:
138144
"""
139145
Check if PV exists.
140146
@@ -159,21 +165,21 @@ def wait_for_pv(
159165
value: The value to wait for
160166
maxwait (int, optional): The maximum time to wait for in seconds
161167
"""
162-
start_time = datetime.utcnow()
168+
start_time = datetime.now(UTC)
163169
while True:
164170
curr_value = __api.get_pv_value(pv)
165171
if curr_value == value:
166172
break
167173
if maxwait is not None:
168-
if timedelta(seconds=maxwait) < datetime.utcnow() - start_time:
174+
if timedelta(seconds=maxwait) < datetime.now(UTC) - start_time:
169175
break
170176
sleep(DELAY_IN_WAIT_FOR_SLEEP_LOOP)
171177
check_break(2)
172178

173179

174180
@usercommand
175181
@helparglist("")
176-
def set_begin_precmd(begin_precmd: Callable[[Any], str | None]) -> None:
182+
def set_begin_precmd(begin_precmd: PrePostCmd) -> None:
177183
"""
178184
Set the function to call before the begin command.
179185
@@ -186,7 +192,7 @@ def set_begin_precmd(begin_precmd: Callable[[Any], str | None]) -> None:
186192

187193
@usercommand
188194
@helparglist("")
189-
def set_begin_postcmd(begin_postcmd: Callable[[Any], str | None]) -> None:
195+
def set_begin_postcmd(begin_postcmd: PrePostCmd) -> None:
190196
"""
191197
Set the function to call after the begin command.
192198
@@ -198,7 +204,7 @@ def set_begin_postcmd(begin_postcmd: Callable[[Any], str | None]) -> None:
198204

199205
@usercommand
200206
@helparglist("")
201-
def set_abort_precmd(abort_precmd: Callable[[Any], str | None]) -> None:
207+
def set_abort_precmd(abort_precmd: PrePostCmd) -> None:
202208
"""
203209
Set the function to call before the abort command.
204210
@@ -210,7 +216,7 @@ def set_abort_precmd(abort_precmd: Callable[[Any], str | None]) -> None:
210216

211217
@usercommand
212218
@helparglist("")
213-
def set_abort_postcmd(abort_postcmd: Callable[[Any], str | None]) -> None:
219+
def set_abort_postcmd(abort_postcmd: PrePostCmd) -> None:
214220
"""
215221
Set the function to call after the abort command.
216222
@@ -222,7 +228,7 @@ def set_abort_postcmd(abort_postcmd: Callable[[Any], str | None]) -> None:
222228

223229
@usercommand
224230
@helparglist("")
225-
def set_end_precmd(end_precmd: Callable[[Any], str | None]) -> None:
231+
def set_end_precmd(end_precmd: PrePostCmd) -> None:
226232
"""
227233
Set the function to call before the end command.
228234
@@ -234,7 +240,7 @@ def set_end_precmd(end_precmd: Callable[[Any], str | None]) -> None:
234240

235241
@usercommand
236242
@helparglist("")
237-
def set_end_postcmd(end_postcmd: Callable[[Any], str | None]) -> None:
243+
def set_end_postcmd(end_postcmd: PrePostCmd) -> None:
238244
"""
239245
Set the function to call after the end command.
240246
@@ -246,7 +252,7 @@ def set_end_postcmd(end_postcmd: Callable[[Any], str | None]) -> None:
246252

247253
@usercommand
248254
@helparglist("")
249-
def set_pause_precmd(pause_precmd: Callable[[Any], str | None]) -> None:
255+
def set_pause_precmd(pause_precmd: PrePostCmd) -> None:
250256
"""
251257
Set the function to call before the pause command.
252258
@@ -258,7 +264,7 @@ def set_pause_precmd(pause_precmd: Callable[[Any], str | None]) -> None:
258264

259265
@usercommand
260266
@helparglist("")
261-
def set_pause_postcmd(pause_postcmd: Callable[[Any], str | None]) -> None:
267+
def set_pause_postcmd(pause_postcmd: PrePostCmd) -> None:
262268
"""
263269
Set the function to call after the pause command.
264270
@@ -270,7 +276,7 @@ def set_pause_postcmd(pause_postcmd: Callable[[Any], str | None]) -> None:
270276

271277
@usercommand
272278
@helparglist("")
273-
def set_resume_precmd(resume_precmd: Callable[[Any], str | None]) -> None:
279+
def set_resume_precmd(resume_precmd: PrePostCmd) -> None:
274280
"""
275281
Set the function to call before the resume command.
276282
@@ -282,7 +288,7 @@ def set_resume_precmd(resume_precmd: Callable[[Any], str | None]) -> None:
282288

283289
@usercommand
284290
@helparglist("")
285-
def set_resume_postcmd(resume_postcmd: Callable[[Any], str | None]) -> None:
291+
def set_resume_postcmd(resume_postcmd: PrePostCmd) -> None:
286292
"""
287293
Set the function to call after the resume command.
288294
@@ -400,7 +406,9 @@ def get_exp_data(
400406
@usercommand
401407
@helparglist("")
402408
@log_command_and_handle_exception
403-
def get_spectrum_data(with_spec_zero: bool = True, with_time_bin_zero: bool = False) -> npt.NDArray:
409+
def get_spectrum_data(
410+
with_spec_zero: bool = True, with_time_bin_zero: bool = False
411+
) -> npt.NDArray[np.float32]:
404412
"""
405413
Get the event mode spectrum data as ND array.
406414
Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,45 @@
11
from builtins import object
2+
from typing import Any
23

34

45
class PrePostCmdManager(object):
56
"""
6-
A class to manager the precmd and postcmd commands such as used in begin, end, abort, resume, pause.
7+
A class to manager the precmd and postcmd commands such as used in begin, end, abort, resume,
8+
pause.
79
"""
810

9-
def __init__(self):
10-
self.begin_precmd = lambda **pars: None
11-
self.begin_postcmd = lambda **pars: None
12-
self.abort_precmd = lambda **pars: None
13-
self.abort_postcmd = lambda **pars: None
14-
self.end_precmd = lambda **pars: None
15-
self.end_postcmd = lambda **pars: None
16-
self.pause_precmd = lambda **pars: None
17-
self.pause_postcmd = lambda **pars: None
18-
self.resume_precmd = lambda **pars: None
19-
self.resume_postcmd = lambda **pars: None
20-
self.cset_precmd = lambda **pars: True
21-
self.cset_postcmd = lambda **pars: None
11+
def begin_precmd(self, **kwargs: Any) -> str | None:
12+
return
13+
14+
def begin_postcmd(self, **kwargs: Any) -> str | None:
15+
return
16+
17+
def abort_precmd(self, **kwargs: Any) -> str | None:
18+
return
19+
20+
def abort_postcmd(self, **kwargs: Any) -> str | None:
21+
return
22+
23+
def end_precmd(self, **kwargs: Any) -> str | None:
24+
return
25+
26+
def end_postcmd(self, **kwargs: Any) -> str | None:
27+
return
28+
29+
def pause_precmd(self, **kwargs: Any) -> str | None:
30+
return
31+
32+
def pause_postcmd(self, **kwargs: Any) -> str | None:
33+
return
34+
35+
def resume_precmd(self, **kwargs: Any) -> str | None:
36+
return
37+
38+
def resume_postcmd(self, **kwargs: Any) -> str | None:
39+
return
40+
41+
def cset_precmd(self, **kwargs: Any) -> bool:
42+
return True
43+
44+
def cset_postcmd(self, **kwargs: Any) -> str | None:
45+
return

src/genie_python/genie_simulate_impl.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,12 +1117,16 @@ def __init__(
11171117
self.sample_pars = {}
11181118
self.strict_block = strict_block
11191119
self.logger = GenieLogger(sim_mode=True)
1120+
self.exp_data = None
11201121

11211122
def set_instrument(
11221123
self, pv_prefix: str, globs: dict | None, import_instrument_init: bool
11231124
) -> None:
11241125
self.inst_prefix = pv_prefix
11251126

1127+
def get_instrument(self) -> str | None:
1128+
return self.inst_prefix
1129+
11261130
def prefix_pv_name(self, name: str) -> str:
11271131
"""Adds the instrument prefix to the specified PV"""
11281132
if self.inst_prefix is not None and not name.startswith(self.inst_prefix):
@@ -1156,7 +1160,7 @@ def get_pv_value(
11561160
% (name, to_string, attempts, is_local, use_numpy)
11571161
)
11581162

1159-
def pv_exists(self, name: str) -> bool:
1163+
def pv_exists(self, name: str, is_local: bool = False) -> bool:
11601164
return True
11611165

11621166
def connected_pvs_in_list(self, pv_list: list[str], is_local: bool = False) -> list[str]:
@@ -1229,7 +1233,10 @@ def get_block_data(self, block: str, fail_fast: bool = False) -> "_CgetReturn":
12291233
ans["alarm"] = self.get_alarm_from_block(block)
12301234
return typing.cast("_CgetReturn", ans)
12311235

1232-
def set_multiple_blocks(self, names: list, values: list) -> None:
1236+
def get_pv_from_block(self, block_name: str) -> str:
1237+
return block_name
1238+
1239+
def set_multiple_blocks(self, names: list[str], values: list["PVValue"]) -> None:
12331240
temp = list(zip(names, values))
12341241
for name, value in temp:
12351242
if name in self.block_dict:

0 commit comments

Comments
 (0)