Skip to content

Commit

Permalink
fix typing according to new mypy rules
Browse files Browse the repository at this point in the history
  • Loading branch information
OdoctorG committed Jul 5, 2024
2 parents 7d6f1e2 + e1e333e commit 06830ef
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 39 deletions.
2 changes: 1 addition & 1 deletion python/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pylint:

.PHONY: mypy
mypy:
mypy . --config-file pyproject.toml --no-namespace-packages
mypy . --config-file pyproject.toml
@echo ""

.PHONY: ruff-format
Expand Down
6 changes: 3 additions & 3 deletions python/cloud-demo/lib/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import binascii
import queue
from threading import Thread
from typing import Any, Callable
from typing import Any, Callable, Sequence

import remotivelabs.broker.sync as br
from typing_extensions import Self
Expand Down Expand Up @@ -49,7 +49,7 @@ def list_signal_names(self) -> list[str]:
signal_names.append(sinfo.id.name)
return signal_names

def subscribe(self, signals: list[br.network_api_pb2.Signal], on_frame: Callable[..., None], changed_values_only: bool = True) -> Any:
def subscribe(self, signals: list[str], on_frame: Callable[..., None], changed_values_only: bool = True) -> Any:
client_id = br.common_pb2.ClientId(id="cloud_demo")

signals_to_subscribe_on = map(lambda signal: self.signal_creator.signal(signal, "custom_can"), signals)
Expand All @@ -73,7 +73,7 @@ def subscribe(self, signals: list[br.network_api_pb2.Signal], on_frame: Callable
def connect(cls, url: str, api_key: str | None = None, access_token: str | None = None) -> Self:
return Broker(url, api_key, access_token) # type: ignore

def __each_signal(self, signals: br.network_api_pb2.Signals, callback: Callable[..., Any]) -> None:
def __each_signal(self, signals: Sequence[br.network_api_pb2.Signal], callback: Callable[..., Any]) -> None:
callback(map(lambda s: {"timestamp_nanos": s.timestamp, "name": s.id.name, "value": self.__get_value(s)}, signals))

@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions python/playback-record/playback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import signal as sig
import time
from threading import Event
from typing import Any, Optional
from typing import Any, Optional, Sequence

import grpc
import remotivelabs.broker.sync as br
Expand Down Expand Up @@ -105,7 +105,7 @@ def ecu_b_subscribe_(stub: br.network_api_pb2_grpc.NetworkServiceStub) -> None:
print(err)


def read_on_timer(stub: br.network_api_pb2_grpc.NetworkServiceStub, signals: br.network_api_pb2.Signals, pause: int) -> None:
def read_on_timer(stub: br.network_api_pb2_grpc.NetworkServiceStub, signals: Sequence[br.common_pb2.SignalId], pause: int) -> None:
"""Simple reading with timer, logs on purpose tabbed with double space
Parameters
Expand Down
1 change: 1 addition & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ ignore=[
]
recursive=true
load-plugins = ["pylint_protobuf"]
enable = ["useless-suppression"]

[tool.pylint.format]
max-line-length=140
Expand Down
4 changes: 2 additions & 2 deletions python/pytest/test_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import pytest

# Server address:
_SERVER_URL = "https://personal-5z42sn9ui4-demo-uo7acw3qiq-ez.a.run.app"
_SERVER_APIKEY = "7925BA43-9FB03240-FF17B0DE-BA0CC228"
_SERVER_URL = "http://127.0.0.1:50051"
_SERVER_APIKEY = None


class Broker: # pylint: disable=R0903
Expand Down
8 changes: 4 additions & 4 deletions python/reflector-ecu/reflector.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def ecu_a(stub: br.network_api_pb2_grpc.NetworkServiceStub, signal_creator: br.S
increasing_counter = (increasing_counter + 1) % 4


def read_on_timer(stub: br.network_api_pb2_grpc.NetworkServiceStub, signals: br.network_api_pb2.Signals, pause: int) -> None:
def read_on_timer(stub: br.network_api_pb2_grpc.NetworkServiceStub, signals: Sequence[br.common_pb2.SignalId], pause: int) -> None:
"""Simple reading with timer
Parameters
Expand Down Expand Up @@ -130,7 +130,7 @@ def get_value_pair(signal: br.network_api_pb2.Signal) -> Tuple[str, Any]:
def act_on_signal(
client_id: br.common_pb2.ClientId,
stub: br.network_api_pb2_grpc.NetworkServiceStub,
sub_signals: br.common_pb2.SignalId,
sub_signals: Sequence[br.common_pb2.SignalId],
on_change: bool,
fun: Callable[[Any], None],
on_subcribed: Callable[[Any], None] | None = None,
Expand Down Expand Up @@ -196,7 +196,7 @@ def double_and_publish(
network_stub: br.network_api_pb2_grpc.NetworkServiceStub,
client_id: br.common_pb2.ClientId,
trigger: Any,
signals: br.network_api_pb2.Signals,
signals: Sequence[br.network_api_pb2.Signal],
signal_creator: br.SignalCreator,
) -> None:
if signal_creator is None:
Expand Down Expand Up @@ -262,7 +262,7 @@ def modify_signals_publish_frame(
network_stub: br.network_api_pb2_grpc.NetworkServiceStub,
client_id: br.common_pb2.ClientId,
destination_namespace_name: str,
signals: br.network_api_pb2.Signals,
signals: Sequence[br.network_api_pb2.Signal],
) -> None:
"""Modifiy recieved signals and publish them."""

Expand Down
7 changes: 4 additions & 3 deletions python/restbus/restbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import time
from typing import Any, Dict, Generator, Iterable, List, Optional, Tuple

import google.protobuf.internal.containers # type: ignore
import google.protobuf.internal.containers
import remotivelabs.broker.sync as br
from grpc import Channel
from typing_extensions import TypeAlias
Expand Down Expand Up @@ -192,6 +192,7 @@ def get_frame_selection(
"""Get the frame selection and E2eCounterStates for a chosen run"""

system_stub = br.system_api_pb2_grpc.SystemServiceStub(intercept_channel)
_exclude = exclude

if configure:
print(f"Configuring broker with {configure}")
Expand All @@ -206,13 +207,13 @@ def get_frame_selection(
# Exit if no frames selected
print(f"No frames specified, selecting all frames in namespace {run_info.namespace_name}")
run_info.frames = []
exclude = True
_exclude = True

# Generate a list of values ready for publish
sc = br.SignalCreator(system_stub)

e2e_counters: E2eCounterStates = dict([(signal_name, 0) for signal_name in select_e2e_counters(signals.frame)]) # pylint: disable=R1717
frame_selection: list[SchedulingTuple] = list(select_rest_bus_frames(sc, manual_sets, signals.frame, run_info.frames, exclude))
frame_selection: list[SchedulingTuple] = list(select_rest_bus_frames(sc, manual_sets, signals.frame, run_info.frames, _exclude))
# Return both the frame selection and counters to use for running the restbus
return frame_selection, e2e_counters

Expand Down
33 changes: 17 additions & 16 deletions python/simple-ecu/ecu.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
import time
from threading import Thread
from typing import Any, Callable, Optional, Sequence
from typing import Any, Callable, Optional, Sequence, Tuple

import grpc
import remotivelabs.broker.sync as br
Expand Down Expand Up @@ -82,7 +82,7 @@ def ecu_a(stub: br.network_api_pb2_grpc.NetworkServiceStub, signal_creator: br.S
increasing_counter = counter_start_value + (increasing_counter + 1) % 4


def read_on_timer(stub: br.network_api_pb2_grpc.NetworkServiceStub, signals: br.network_api_pb2.Signals, pause: int) -> None:
def read_on_timer(stub: br.network_api_pb2_grpc.NetworkServiceStub, signals: Sequence[br.common_pb2.SignalId], pause: int) -> None:
"""Simple reading with timer
Parameters
Expand Down Expand Up @@ -157,7 +157,7 @@ def double_and_publish(
network_stub: br.network_api_pb2_grpc.NetworkServiceStub,
client_id: br.common_pb2.ClientId,
trigger: Any,
signals: br.network_api_pb2.Signals,
signals: Sequence[br.network_api_pb2.Signal],
signal_creator: br.SignalCreator,
) -> None:
if signal_creator is None:
Expand All @@ -184,28 +184,29 @@ def subscribe(
broker: Any,
client_id: br.common_pb2.ClientId,
network_stub: br.network_api_pb2_grpc.NetworkServiceStub,
signals: br.network_api_pb2.Signals,
script: list[br.common_pb2.SignalId],
on_subscribe: Callable[[Sequence[br.network_api_pb2.Signal]], None],
on_change: bool = False,
) -> grpc.RpcContext:
sync = queue.Queue()
Thread(
target=broker.act_on_signal,
) -> Tuple[Any, Thread]:
sync: queue.Queue[Any] = queue.Queue()
thread: Thread = Thread(
target=broker.act_on_scripted_signal,
args=(
client_id,
network_stub,
signals,
script,
on_change, # True: only report when signal changes
on_subscribe,
lambda subscription: (sync.put(subscription)),
sync.put,
),
).start()
)
thread.start()
# wait for subscription to settle
subscription = sync.get()
return subscription
return subscription, thread


def run(url: str, configuration: str, x_api_key: Optional[str] = None, access_token: Optional[str] = None) -> None:
def run(url: str, configuration_folder: str, x_api_key: Optional[str] = None, access_token: Optional[str] = None) -> None:
"""Main function, checking arguments passed to script, setting up stubs, configuration and starting Threads."""
# Setting up stubs and configuration
intercept_channel = br.create_channel(url, x_api_key, access_token)
Expand All @@ -214,8 +215,8 @@ def run(url: str, configuration: str, x_api_key: Optional[str] = None, access_to
system_stub = br.system_api_pb2_grpc.SystemServiceStub(intercept_channel)
br.check_license(system_stub)

print(f"Using configuration {configuration}")
br.upload_folder(system_stub, configuration)
print(f"Using configuration {configuration_folder}")
br.upload_folder(system_stub, configuration_folder)
br.reload_configuration(system_stub)

signal_creator = br.SignalCreator(system_stub)
Expand Down Expand Up @@ -250,7 +251,7 @@ def run(url: str, configuration: str, x_api_key: Optional[str] = None, access_to
# ecu a, this is where we publish, and
Thread(
target=ecu_a,
args=(network_stub,signal_creator),
args=(network_stub, signal_creator),
).start()

# ecu b, bonus, periodically, read using timer.
Expand Down
7 changes: 3 additions & 4 deletions python/subscribe-to-scripted-signal/subscribe_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import queue
import time
from threading import Thread
from typing import Any, Callable, Generator, Optional, Tuple
from typing import Any, Callable, Generator, Optional, Sequence, Tuple

import remotivelabs.broker.sync as br

Expand All @@ -15,7 +15,7 @@ def subscribe(
client_id: br.common_pb2.ClientId,
network_stub: br.network_api_pb2_grpc.NetworkServiceStub,
script: bytes,
on_subscribe: Callable[[br.network_api_pb2.Signals], None],
on_subscribe: Callable[[Sequence[br.network_api_pb2.Signal]], None],
on_change: bool = False,
) -> Tuple[Any, Thread]:
# pylint: disable=R0913
Expand Down Expand Up @@ -54,7 +54,7 @@ def _get_value_str(signal: br.network_api_pb2.Signal) -> str:
return "empty"


def printer(signals: br.network_api_pb2.Signals) -> None:
def printer(signals: Sequence[br.network_api_pb2.Signal]) -> None:
for signal in signals:
print(f"{signal.id.name} {signal.id.namespace.name} {_get_value_str(signal)}")

Expand Down Expand Up @@ -101,7 +101,6 @@ def run(


class ScriptPathArgument(argparse.Action):
# pylint: disable=R0903
# pylint: disable=W0222
def __call__(self, _parser: Any, namespace: Any, value: Any, _option: Any) -> None: # type: ignore
print("Script path in use:", value)
Expand Down
7 changes: 3 additions & 4 deletions python/subscribe-to-scripted-signal/subscribe_standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
import time
from threading import Thread
from typing import Any, Callable, Generator, Optional, Tuple
from typing import Any, Callable, Generator, Optional, Sequence, Tuple

import remotivelabs.broker.sync as br

Expand All @@ -16,7 +16,7 @@ def subscribe(
client_id: br.common_pb2.ClientId,
network_stub: br.network_api_pb2_grpc.NetworkServiceStub,
script: bytes,
on_subscribe: Callable[[br.network_api_pb2.Signals], None],
on_subscribe: Callable[[Sequence[br.network_api_pb2.Signal]], None],
on_change: bool = False,
) -> Tuple[Any, Thread]:
# pylint: disable=R0913
Expand Down Expand Up @@ -55,7 +55,7 @@ def _get_value_str(signal: br.network_api_pb2.Signal) -> str:
return "empty"


def printer(signals: br.network_api_pb2.Signals) -> None:
def printer(signals: Sequence[br.network_api_pb2.Signal]) -> None:
for signal in signals:
print(f"{signal.id.name} {signal.id.namespace.name} {_get_value_str(signal)}")

Expand Down Expand Up @@ -152,7 +152,6 @@ def run(


class ScriptPathArgument(argparse.Action):
# pylint: disable=R0903
# pylint: disable=W0222
def __call__(self, _parser: Any, namespace: argparse.Namespace, value: Any, _option: Any) -> None: # type: ignore
print("Script path in use:", value)
Expand Down

0 comments on commit 06830ef

Please sign in to comment.