Skip to content

Ruff and pyright ig #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions src/genie_python/channel_access_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
"""
Useful and slightly more explit exceptions that can be thrown. In general catch the super class of these.
Useful and slightly more explicit exceptions that can be thrown.
In general catch the super class of these.
"""


class UnableToConnectToPVException(IOError):
class UnableToConnectToPVException(IOError): # noqa N818 Historic name
"""
The system is unable to connect to a PV for some reason.
"""

def __init__(self, pv_name, err):
def __init__(self, pv_name: str, err: str) -> None:
super(UnableToConnectToPVException, self).__init__(
"Unable to connect to PV {0}: {1}".format(pv_name, err)
f"Unable to connect to PV {pv_name}: {err}"
)


class InvalidEnumStringException(KeyError):
class InvalidEnumStringException(KeyError): # noqa N818 Historic name
"""
The enum string that is trying to be set is not listed in the pv.
"""

def __init__(self, pv_name, valid_states):
def __init__(self, pv_name: str, valid_states: str) -> None:
super(InvalidEnumStringException, self).__init__(
"Invalid string value entered for {}. Valid strings are {}".format(
pv_name, valid_states
)
f"Invalid string value entered for {pv_name}. Valid strings are {valid_states}"
)


class ReadAccessException(IOError):
class ReadAccessException(IOError): # noqa N818 Historic name
"""
PV exists but its value is unavailable to read.
"""

def __init__(self, pv_name):
super(ReadAccessException, self).__init__("Read access denied for PV {}".format(pv_name))
def __init__(self, pv_name: str) -> None:
super(ReadAccessException, self).__init__(f"Read access denied for PV {pv_name}")


class WriteAccessException(IOError):
class WriteAccessException(IOError): # noqa N818 Historic name
"""
PV was written to but does not allow writes.
"""

def __init__(self, pv_name):
super(WriteAccessException, self).__init__("Write access denied for PV {}".format(pv_name))
def __init__(self, pv_name: str) -> None:
super(WriteAccessException, self).__init__(f"Write access denied for PV {pv_name}")
39 changes: 24 additions & 15 deletions src/genie_python/genie_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
This module is used for setting alerts on blocks.
"""

from __future__ import absolute_import, print_function

from genie_python.genie_api_setup import (
__api,
helparglist,
Expand All @@ -25,9 +23,16 @@


@usercommand
@helparglist("block, lowlimit, highlimit, [delay_in, delay_out]")
@helparglist("block, lowlimit, highlimit, [set_enable, delay_in, delay_out]")
@log_command_and_handle_exception
def set_range(block, lowlimit, highlimit, set_enable=True, delay_in=None, delay_out=None):
def set_range(
block: str,
lowlimit: float,
highlimit: float,
set_enable: bool = True,
delay_in: float | None = None,
delay_out: float | None = None,
) -> None:
"""
Sets alert range on block.

Expand All @@ -36,8 +41,10 @@ def set_range(block, lowlimit, highlimit, set_enable=True, delay_in=None, delay_
lowlimit (float): low limit
highlimit (float): high limit
set_enable (bool): (optional setting True will enable alerts on the block. Defaults to True.
delay_in (float): (optional) delay /s before triggering in range. If not specified the delay remains unchanged.
delay_out (float): (optional) delay /s before triggering out of range. If not specified the delay remains unchanged.
delay_in (float): (optional) delay /s before triggering in range. If not specified the delay
remains unchanged.
delay_out (float): (optional) delay /s before triggering out of range.
If not specified the delay remains unchanged.

"""
if not __api.block_exists(block):
Expand All @@ -56,7 +63,7 @@ def set_range(block, lowlimit, highlimit, set_enable=True, delay_in=None, delay_
@usercommand
@helparglist("block [, is_enabled]")
@log_command_and_handle_exception
def enable(block, set_enabled=True):
def enable(block: str, set_enabled: bool = True) -> None:
"""
Enable alerts on a block.

Expand All @@ -73,7 +80,7 @@ def enable(block, set_enabled=True):
@usercommand
@helparglist("message")
@log_command_and_handle_exception
def send(message):
def send(message: str) -> None:
"""
Send a message to all alert recipients.

Expand All @@ -87,7 +94,7 @@ def send(message):
## no log decorator so mobile numbers not sent to log file
@usercommand
@helparglist("numbers")
def set_sms(numbers):
def set_sms(numbers: list[str] | str) -> None:
"""
Set SMS text numbers for alerts on blocks.

Expand All @@ -107,7 +114,7 @@ def set_sms(numbers):
## no log decorator so email addresses not sent to log file
@usercommand
@helparglist("emails")
def set_email(emails):
def set_email(emails: list[str] | str) -> None:
"""
Set email addresses for alerts on blocks.

Expand All @@ -124,7 +131,7 @@ def set_email(emails):
print("Unable to set alert email addresses: {}".format(e))


def _print_block(block, only_if_enabled=False):
def _print_block(block: str, only_if_enabled: bool = False) -> None:
enabled = (
__api.get_pv_value(_ALERT_ENABLE.format(block), to_string=True, is_local=True) == "YES"
)
Expand Down Expand Up @@ -157,7 +164,7 @@ def _print_block(block, only_if_enabled=False):
@usercommand
@helparglist("[block, all]")
@log_command_and_handle_exception
def status(block=None, all=False):
def status(block: str | None = None, all: bool = False) -> None:
"""
Prints the emails and mobiles used for alerts and the current status of specified block.
Args:
Expand All @@ -177,12 +184,14 @@ def status(block=None, all=False):


# used as part of tests, returns a dictionary of details
def _dump(block):
def _dump(block: str) -> dict[str, str]:
if not __api.block_exists(block):
raise Exception('No block with the name "{}" exists'.format(block))
res = {}
res["emails"] = __api.get_pv_value(_ALERT_EMAILS, to_string=True, is_local=True).split(";")
res["mobiles"] = __api.get_pv_value(_ALERT_MOBILES, to_string=True, is_local=True).split(";")
res["emails"] = str(__api.get_pv_value(_ALERT_EMAILS, to_string=True, is_local=True)).split(";")
res["mobiles"] = str(__api.get_pv_value(_ALERT_MOBILES, to_string=True, is_local=True)).split(
";"
)
res["enabled"] = __api.get_pv_value(_ALERT_ENABLE.format(block), to_string=False, is_local=True)
res["lowlimit"] = __api.get_pv_value(_ALERT_LOW.format(block), to_string=False, is_local=True)
res["highlimit"] = __api.get_pv_value(_ALERT_HIGH.format(block), to_string=False, is_local=True)
Expand Down