Skip to content

Commit

Permalink
fix: adapt code to be python 3.8 compatible for the typing (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngruelaneo authored Apr 18, 2024
2 parents 2ed7fd3 + cbf0e91 commit 7a7af54
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ tests = [
dev = [
'mypy',
'ruff',
'types-protobuf',
]

[tool.pytest.ini_options]
Expand Down
16 changes: 6 additions & 10 deletions packages/python/src/armonik/client/events.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, cast, List
from typing import Callable, cast, Iterable, List, Optional

from grpc import Channel

Expand Down Expand Up @@ -42,10 +42,10 @@ def __init__(self, grpc_channel: Channel):
def get_events(
self,
session_id: str,
event_types: List[EventTypes],
event_types: Iterable[EventTypes], # TODO: make EventTypes an enum when Python 3.8 support will be not supported
event_handlers: List[Callable[[str, EventTypes, Event], bool]],
task_filter: Filter | None = None,
result_filter: Filter | None = None,
task_filter: Optional[Filter] = None,
result_filter: Optional[Filter] = None,
) -> None:
"""Get events that represents updates of result and tasks data.
Expand All @@ -62,13 +62,9 @@ def get_events(
"""
request = EventSubscriptionRequest(session_id=session_id, returned_events=event_types)
if task_filter:
request.tasks_filters = (
cast(rawTaskFilters, task_filter.to_disjunction().to_message()),
)
request.tasks_filters = cast(rawTaskFilters, task_filter.to_disjunction().to_message())
if result_filter:
request.results_filters = (
cast(rawResultFilters, result_filter.to_disjunction().to_message()),
)
request.results_filters = cast(rawResultFilters, result_filter.to_disjunction().to_message())

streaming_call = self._client.GetEvents(request)
for message in streaming_call:
Expand Down
4 changes: 2 additions & 2 deletions packages/python/src/armonik/client/partitions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Tuple, cast
from typing import List, Tuple, cast, Optional

from grpc import Channel

Expand Down Expand Up @@ -49,7 +49,7 @@ def __init__(self, grpc_channel: Channel):

def list_partitions(
self,
partition_filter: Filter | None = None,
partition_filter: Optional[Filter] = None,
page: int = 0,
page_size: int = 1000,
sort_field: Filter = PartitionFieldFilter.PRIORITY,
Expand Down
6 changes: 3 additions & 3 deletions packages/python/src/armonik/client/results.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Dict, List, Tuple, cast
from typing import Dict, List, Tuple, cast, Optional, Union

from deprecation import deprecated
from grpc import Channel
Expand Down Expand Up @@ -92,7 +92,7 @@ def get_results_ids(self, session_id: str, names: List[str]) -> Dict[str, str]:

def list_results(
self,
result_filter: Filter | None = None,
result_filter: Optional[Filter] = None,
page: int = 0,
page_size: int = 1000,
sort_field: Filter = ResultFieldFilter.STATUS,
Expand Down Expand Up @@ -214,7 +214,7 @@ def create_results(
return results

def upload_result_data(
self, result_id: str, session_id: str, result_data: bytes | bytearray
self, result_id: str, session_id: str, result_data: Union[bytes, bytearray]
) -> None:
"""Upload data for an empty result already created.
Expand Down
2 changes: 1 addition & 1 deletion packages/python/src/armonik/client/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def get_session(self, session_id: str):

def list_sessions(
self,
session_filter: Filter | None = None,
session_filter: Optional[Filter] = None,
page: int = 0,
page_size: int = 1000,
sort_field: Filter = SessionFieldFilter.STATUS,
Expand Down
6 changes: 3 additions & 3 deletions packages/python/src/armonik/client/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def get_task(self, task_id: str) -> Task:

def list_tasks(
self,
task_filter: Filter | None = None,
task_filter: Optional[Filter] = None,
with_errors: bool = False,
page: int = 0,
page_size: int = 1000,
Expand Down Expand Up @@ -367,7 +367,7 @@ def get_result_ids(
tasks_result_ids[t.task_id] = list(t.result_ids)
return tasks_result_ids

def count_tasks_by_status(self, task_filter: Filter | None = None) -> Dict[TaskStatus, int]:
def count_tasks_by_status(self, task_filter: Optional[Filter] = None) -> Dict[TaskStatus, int]:
"""Get number of tasks by status.
Args:
Expand All @@ -393,7 +393,7 @@ def submit_tasks(
self,
session_id: str,
tasks: List[TaskDefinition],
default_task_options: Optional[TaskOptions | None] = None,
default_task_options: Optional[TaskOptions] = None,
chunk_size: Optional[int] = 100,
) -> List[Task]:
"""Submit tasks to ArmoniK.
Expand Down
3 changes: 1 addition & 2 deletions packages/python/src/armonik/common/events.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from abc import ABC
from typing import List

from dataclasses import dataclass

from .enumwrapper import TaskStatus, ResultStatus


class Event(ABC):
class Event:
@classmethod
def from_raw_event(cls, raw_event):
values = {}
Expand Down
4 changes: 2 additions & 2 deletions packages/python/src/armonik/common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def get_task_filter(
return task_filter


def datetime_to_timestamp(time_stamp: datetime | None) -> timestamp.Timestamp:
def datetime_to_timestamp(time_stamp: Optional[datetime]) -> timestamp.Timestamp:
"""Helper function to convert a Python Datetime to a gRPC Timestamp
Args:
Expand All @@ -68,7 +68,7 @@ def datetime_to_timestamp(time_stamp: datetime | None) -> timestamp.Timestamp:
return t


def timestamp_to_datetime(time_stamp: timestamp.Timestamp) -> datetime | None:
def timestamp_to_datetime(time_stamp: timestamp.Timestamp) -> Optional[datetime]:
"""Helper function to convert a gRPC Timestamp to a Python Datetime
Note that datetime has microseconds accuracy instead of nanosecond accuracy for gRPC Timestamp
Therefore, the conversion may not be lossless.
Expand Down
2 changes: 1 addition & 1 deletion packages/python/src/armonik/worker/seqlogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def log(
elif isinstance(exc_info, BaseException):
exc_info = (type(exc_info), exc_info, exc_info.__traceback__)
payload["@x"] = "\n".join(traceback.format_exception(*exc_info))
for k, v in kwargs:
for k, v in kwargs.items():
if k.startswith("@"):
k = "@" + k
payload[k] = str(v)
Expand Down
4 changes: 2 additions & 2 deletions packages/python/src/armonik/worker/taskhandler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations
import os
from deprecation import deprecated
from typing import Optional, Dict, List, Tuple
from typing import Optional, Dict, List, Tuple, Union

from ..common import TaskOptions, TaskDefinition, Task, Result
from ..protogen.common.agent_common_pb2 import (
Expand Down Expand Up @@ -144,7 +144,7 @@ def submit_tasks(

self._client.SubmitTasks(request)

def send_results(self, results_data: Dict[str, bytes | bytearray]) -> None:
def send_results(self, results_data: Dict[str, Union[bytes, bytearray]]) -> None:
"""Send results.
Args:
Expand Down

0 comments on commit 7a7af54

Please sign in to comment.