Skip to content

Commit

Permalink
New formatting to comply with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
burkeds committed Sep 19, 2024
1 parent 518073e commit 1e8a00a
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 121 deletions.
30 changes: 13 additions & 17 deletions src/ophyd_async/tango/base_devices/_base_device.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from __future__ import annotations

from typing import (
Dict,
Optional,
Tuple,
Type,
TypeVar,
Union,
get_args,
Expand All @@ -22,7 +18,7 @@
make_backend,
tango_signal_auto,
)
from tango import DeviceProxy as SyncDeviceProxy
from tango import DeviceProxy as DeviceProxy
from tango.asyncio import DeviceProxy as AsyncDeviceProxy

T = TypeVar("T")
Expand All @@ -44,15 +40,15 @@ class TangoDevice(Device):
"""

trl: str = ""
proxy: Optional[Union[AsyncDeviceProxy, SyncDeviceProxy]] = None
_polling: Tuple[bool, float, float, float] = (False, 0.1, None, 0.1)
_signal_polling: Dict[str, Tuple[bool, float, float, float]] = {}
proxy: DeviceProxy | None = None
_polling: tuple[bool, float, float, float] = (False, 0.1, None, 0.1)
_signal_polling: dict[str, tuple[bool, float, float, float]] = {}
_poll_only_annotated_signals: bool = True

def __init__(
self,
trl: Optional[str] = None,
device_proxy: Optional[Union[AsyncDeviceProxy, SyncDeviceProxy]] = None,
trl: str | None = None,
device_proxy: DeviceProxy | None = None,
name: str = "",
) -> None:
self.trl = trl if trl else ""
Expand Down Expand Up @@ -112,10 +108,10 @@ def register_signals(self):


def tango_polling(
polling: Optional[
Union[Tuple[float, float, float], Dict[str, Tuple[float, float, float]]]
] = None,
signal_polling: Optional[Dict[str, Tuple[float, float, float]]] = None,
polling: tuple[float, float, float]
| dict[str, tuple[float, float, float]]
| None = None,
signal_polling: dict[str, tuple[float, float, float]] | None = None,
):
"""
Class decorator to configure polling for Tango devices.
Expand Down Expand Up @@ -172,7 +168,7 @@ def decorator(cls):


def tango_create_children_from_annotations(
device: TangoDevice, included_optional_fields: Tuple[str, ...] = ()
device: TangoDevice, included_optional_fields: tuple[str, ...] = ()
):
"""Initialize blocks at __init__ of `device`."""
for name, device_type in get_type_hints(type(device)).items():
Expand Down Expand Up @@ -227,7 +223,7 @@ async def fill_proxy_entries(device: TangoDevice):
raise e


def _strip_union(field: Union[Union[T], T]) -> Tuple[T, bool]:
def _strip_union(field: T | T) -> tuple[T, bool]:
if get_origin(field) is Union:
args = get_args(field)
is_optional = type(None) in args
Expand All @@ -237,7 +233,7 @@ def _strip_union(field: Union[Union[T], T]) -> Tuple[T, bool]:
return field, False


def _strip_device_vector(field: Union[Type[Device]]) -> Tuple[bool, Type[Device]]:
def _strip_device_vector(field: type[Device]) -> tuple[bool, type[Device]]:
if get_origin(field) is DeviceVector:
return True, get_args(field)[0]
return False, field
9 changes: 3 additions & 6 deletions src/ophyd_async/tango/base_devices/_tango_readable.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from __future__ import annotations

from typing import Optional, Union

from ophyd_async.core import (
StandardReadable,
)
from ophyd_async.tango.base_devices._base_device import TangoDevice
from tango import DeviceProxy as SyncDeviceProxy
from tango.asyncio import DeviceProxy as AsyncDeviceProxy
from tango import DeviceProxy


class TangoReadable(TangoDevice, StandardReadable):
Expand All @@ -26,8 +23,8 @@ class TangoReadable(TangoDevice, StandardReadable):

def __init__(
self,
trl: Optional[str] = None,
device_proxy: Optional[Union[AsyncDeviceProxy, SyncDeviceProxy]] = None,
trl: str | None = None,
device_proxy: DeviceProxy | None = None,
name: str = "",
) -> None:
TangoDevice.__init__(self, trl, device_proxy=device_proxy, name=name)
4 changes: 1 addition & 3 deletions src/ophyd_async/tango/demo/_counter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

from ophyd_async.core import (
DEFAULT_TIMEOUT,
AsyncStatus,
Expand All @@ -23,7 +21,7 @@ class TangoCounter(TangoReadable):
start: SignalX
reset: SignalX

def __init__(self, trl: Optional[str] = "", name=""):
def __init__(self, trl: str | None = "", name=""):
super().__init__(trl, name=name)
self.add_readables([self.counts], HintedSignal)
self.add_readables([self.sample_time], ConfigSignal)
Expand Down
9 changes: 4 additions & 5 deletions src/ophyd_async/tango/demo/_mover.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import asyncio
from typing import Optional

from bluesky.protocols import Movable, Stoppable

from ophyd_async.core import (
CALCULATE_TIMEOUT,
DEFAULT_TIMEOUT,
AsyncStatus,
CalculatableTimeout,
CalculateTimeout,
ConfigSignal,
HintedSignal,
SignalRW,
Expand All @@ -30,19 +29,19 @@ class TangoMover(TangoReadable, Movable, Stoppable):
velocity: SignalRW[float]
_stop: SignalX

def __init__(self, trl: Optional[str] = "", name=""):
def __init__(self, trl: str | None = "", name=""):
super().__init__(trl, name=name)
self.add_readables([self.position], HintedSignal)
self.add_readables([self.velocity], ConfigSignal)
self._set_success = True

@WatchableAsyncStatus.wrap
async def set(self, value: float, timeout: CalculatableTimeout = CalculateTimeout):
async def set(self, value: float, timeout: CalculatableTimeout = CALCULATE_TIMEOUT):
self._set_success = True
(old_position, velocity) = await asyncio.gather(
self.position.get_value(), self.velocity.get_value()
)
if timeout is CalculateTimeout:
if timeout is CALCULATE_TIMEOUT:
assert velocity > 0, "Motor has zero velocity"
timeout = abs(value - old_position) / velocity + DEFAULT_TIMEOUT

Expand Down
41 changes: 20 additions & 21 deletions src/ophyd_async/tango/signal/_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

from enum import Enum, IntEnum
from typing import Optional, Type, Union

import numpy.typing as npt

Expand All @@ -12,24 +11,24 @@
TangoSignalBackend,
get_python_type,
)
from tango import AttrDataFormat, AttrWriteType, CmdArgType, DevState
from tango.asyncio import DeviceProxy
from tango import AttrDataFormat, AttrWriteType, CmdArgType, DeviceProxy, DevState
from tango.asyncio import DeviceProxy as AsyncDeviceProxy


def make_backend(
datatype: Optional[Type[T]],
read_trl: Optional[str] = "",
write_trl: Optional[str] = "",
device_proxy: Optional[DeviceProxy] = None,
datatype: type[T] | None,
read_trl: str | None = "",
write_trl: str | None = "",
device_proxy: DeviceProxy | None = None,
) -> TangoSignalBackend:
return TangoSignalBackend(datatype, read_trl, write_trl, device_proxy)


def tango_signal_rw(
datatype: Type[T],
datatype: type[T],
read_trl: str,
write_trl: Optional[str] = None,
device_proxy: Optional[DeviceProxy] = None,
write_trl: str | None = None,
device_proxy: DeviceProxy | None = None,
timeout: float = DEFAULT_TIMEOUT,
name: str = "",
) -> SignalRW[T]:
Expand All @@ -55,9 +54,9 @@ def tango_signal_rw(


def tango_signal_r(
datatype: Type[T],
datatype: type[T],
read_trl: str,
device_proxy: Optional[DeviceProxy] = None,
device_proxy: DeviceProxy | None = None,
timeout: float = DEFAULT_TIMEOUT,
name: str = "",
) -> SignalR[T]:
Expand All @@ -81,9 +80,9 @@ def tango_signal_r(


def tango_signal_w(
datatype: Type[T],
datatype: type[T],
write_trl: str,
device_proxy: Optional[DeviceProxy] = None,
device_proxy: DeviceProxy | None = None,
timeout: float = DEFAULT_TIMEOUT,
name: str = "",
) -> SignalW[T]:
Expand All @@ -108,7 +107,7 @@ def tango_signal_w(

def tango_signal_x(
write_trl: str,
device_proxy: Optional[DeviceProxy] = None,
device_proxy: DeviceProxy | None = None,
timeout: float = DEFAULT_TIMEOUT,
name: str = "",
) -> SignalX:
Expand All @@ -130,13 +129,13 @@ def tango_signal_x(


async def tango_signal_auto(
datatype: Optional[Type[T]] = None,
datatype: type[T] | None = None,
*,
trl: str,
device_proxy: Optional[DeviceProxy],
device_proxy: DeviceProxy | None,
timeout: float = DEFAULT_TIMEOUT,
name: str = "",
) -> Union[SignalW, SignalX, SignalR, SignalRW, None]:
) -> SignalW | SignalX | SignalR | SignalRW | None:
try:
signal_character = await infer_signal_character(trl, device_proxy)
except RuntimeError as e:
Expand All @@ -159,10 +158,10 @@ async def tango_signal_auto(
return SignalX(backend=backend, timeout=timeout, name=name)


async def infer_python_type(trl: str = "", proxy: DeviceProxy = None) -> Type[T]:
async def infer_python_type(trl: str = "", proxy: DeviceProxy = None) -> type[T]:
device_trl, tr_name = trl.rsplit("/", 1)
if proxy is None:
dev_proxy = await DeviceProxy(device_trl)
dev_proxy = await AsyncDeviceProxy(device_trl)
else:
dev_proxy = proxy

Expand All @@ -189,7 +188,7 @@ async def infer_python_type(trl: str = "", proxy: DeviceProxy = None) -> Type[T]
async def infer_signal_character(trl, proxy: DeviceProxy = None) -> str:
device_trl, tr_name = trl.rsplit("/", 1)
if proxy is None:
dev_proxy = await DeviceProxy(device_trl)
dev_proxy = await AsyncDeviceProxy(device_trl)
else:
dev_proxy = proxy

Expand Down
Loading

0 comments on commit 1e8a00a

Please sign in to comment.