Skip to content
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

added ruff lint and format #656

Closed
wants to merge 5 commits into from
Closed
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
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,25 @@ pip3 install -r dev-requirements.txt
```bash
tox -e flake8
```
5. Run linter and fix

5. Run unit-test
```bash
tox -e ruff
```

6. Run unit-test

```bash
tox -e py311
```

6. Run type check
7. Run type check

```bash
tox -e type
```

7. Run examples
8. Run examples

```bash
tox -e examples
Expand Down
3 changes: 3 additions & 0 deletions dapr/actor/actor_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ async def do_actor_method1(self, param):
async def do_actor_method2(self, param):
...
"""

...


Expand All @@ -51,8 +52,10 @@ async def do_actor_call(self, param):
Args:
name (str, optional): the name of actor method.
"""

def wrapper(funcobj):
funcobj.__actormethod__ = name
funcobj.__isabstractmethod__ = True
return funcobj

return wrapper
57 changes: 35 additions & 22 deletions dapr/actor/client/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
class ActorFactoryBase(ABC):
@abstractmethod
def create(
self, actor_type: str, actor_id: ActorId,
actor_interface: Optional[Type[ActorInterface]] = None) -> 'ActorProxy':
self,
actor_type: str,
actor_id: ActorId,
actor_interface: Optional[Type[ActorInterface]] = None,
) -> 'ActorProxy':
...


Expand All @@ -44,25 +47,29 @@ class ActorProxyFactory(ActorFactoryBase):
"""

def __init__(
self,
message_serializer=DefaultJSONSerializer(),
http_timeout_seconds: int = settings.DAPR_HTTP_TIMEOUT_SECONDS):
self,
message_serializer=DefaultJSONSerializer(),
http_timeout_seconds: int = settings.DAPR_HTTP_TIMEOUT_SECONDS,
):
# TODO: support serializer for state store later
self._dapr_client = DaprActorHttpClient(message_serializer, timeout=http_timeout_seconds)
self._message_serializer = message_serializer

def create(
self, actor_type: str, actor_id: ActorId,
actor_interface: Optional[Type[ActorInterface]] = None) -> 'ActorProxy':
self,
actor_type: str,
actor_id: ActorId,
actor_interface: Optional[Type[ActorInterface]] = None,
) -> 'ActorProxy':
return ActorProxy(
self._dapr_client, actor_type, actor_id,
actor_interface, self._message_serializer)
self._dapr_client, actor_type, actor_id, actor_interface, self._message_serializer
)


class CallableProxy:
def __init__(
self, proxy: 'ActorProxy', attr_call_type: Dict[str, Any],
message_serializer: Serializer):
self, proxy: 'ActorProxy', attr_call_type: Dict[str, Any], message_serializer: Serializer
):
self._proxy = proxy
self._attr_call_type = attr_call_type
self._message_serializer = message_serializer
Expand Down Expand Up @@ -94,11 +101,13 @@ class ActorProxy:
_default_proxy_factory = ActorProxyFactory()

def __init__(
self, client: DaprActorClientBase,
actor_type: str,
actor_id: ActorId,
actor_interface: Optional[Type[ActorInterface]],
message_serializer: Serializer):
self,
client: DaprActorClientBase,
actor_type: str,
actor_id: ActorId,
actor_interface: Optional[Type[ActorInterface]],
message_serializer: Serializer,
):
self._dapr_client = client
self._actor_id = actor_id
self._actor_type = actor_type
Expand All @@ -120,10 +129,12 @@ def actor_type(self) -> str:

@classmethod
def create(
cls,
actor_type: str, actor_id: ActorId,
actor_interface: Optional[Type[ActorInterface]] = None,
actor_proxy_factory: Optional[ActorFactoryBase] = None) -> 'ActorProxy':
cls,
actor_type: str,
actor_id: ActorId,
actor_interface: Optional[Type[ActorInterface]] = None,
actor_proxy_factory: Optional[ActorFactoryBase] = None,
) -> 'ActorProxy':
"""Creates ActorProxy client to call actor.

Args:
Expand Down Expand Up @@ -160,7 +171,8 @@ async def invoke_method(self, method: str, raw_body: Optional[bytes] = None) ->
raise ValueError(f'raw_body {type(raw_body)} is not bytes type')

return await self._dapr_client.invoke_method(
self._actor_type, str(self._actor_id), method, raw_body)
self._actor_type, str(self._actor_id), method, raw_body
)

def __getattr__(self, name: str) -> CallableProxy:
"""Enables RPC style actor method invocation.
Expand Down Expand Up @@ -188,6 +200,7 @@ def __getattr__(self, name: str) -> CallableProxy:

if name not in self._callable_proxies:
self._callable_proxies[name] = CallableProxy(
self, attr_call_type, self._message_serializer)
self, attr_call_type, self._message_serializer
)

return self._callable_proxies[name]
2 changes: 1 addition & 1 deletion dapr/actor/id.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ActorId:

def __init__(self, actor_id: str):
if not isinstance(actor_id, str):
raise TypeError(f"Argument actor_id must be of type str, not {type(actor_id)}")
raise TypeError(f'Argument actor_id must be of type str, not {type(actor_id)}')
self._id = actor_id

@classmethod
Expand Down
1 change: 1 addition & 0 deletions dapr/actor/runtime/_call_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ActorCallType(Enum):
:class:`ActorMethodContext` includes :class:`ActorCallType` passing to
:meth:`Actor._on_pre_actor_method` and :meth:`Actor._on_post_actor_method`
"""

# Specifies that the method invoked is an actor interface method for a given client request.
actor_interface_method = 0
# Specifies that the method invoked is a timer callback method.
Expand Down
16 changes: 11 additions & 5 deletions dapr/actor/runtime/_reminder_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ class ActorReminderData:
"""

def __init__(
self, reminder_name: str, state: Optional[bytes],
due_time: timedelta, period: timedelta, ttl: Optional[timedelta] = None):
self,
reminder_name: str,
state: Optional[bytes],
due_time: timedelta,
period: timedelta,
ttl: Optional[timedelta] = None,
):
"""Creates new :class:`ActorReminderData` instance.

Args:
Expand Down Expand Up @@ -90,7 +95,7 @@ def as_dict(self) -> Dict[str, Any]:
'reminderName': self._reminder_name,
'dueTime': self._due_time,
'period': self._period,
'data': encoded_state.decode("utf-8")
'data': encoded_state.decode('utf-8'),
}

if self._ttl is not None:
Expand All @@ -106,7 +111,8 @@ def from_dict(cls, reminder_name: str, obj: Dict[str, Any]) -> 'ActorReminderDat
if b64encoded_state is not None and len(b64encoded_state) > 0:
state_bytes = base64.b64decode(b64encoded_state)
if 'ttl' in obj:
return ActorReminderData(reminder_name, state_bytes, obj['dueTime'], obj['period'],
obj['ttl'])
return ActorReminderData(
reminder_name, state_bytes, obj['dueTime'], obj['period'], obj['ttl']
)
else:
return ActorReminderData(reminder_name, state_bytes, obj['dueTime'], obj['period'])
16 changes: 9 additions & 7 deletions dapr/actor/runtime/_state_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ class StateProvider:

This provides the decorator methods to load and save states and check the existence of states.
"""

def __init__(
self,
actor_client: DaprActorClientBase,
state_serializer: Serializer = DefaultJSONSerializer()):
self,
actor_client: DaprActorClientBase,
state_serializer: Serializer = DefaultJSONSerializer(),
):
self._state_client = actor_client
self._state_serializer = state_serializer

async def try_load_state(
self, actor_type: str, actor_id: str,
state_name: str, state_type: Type[Any] = object) -> Tuple[bool, Any]:
self, actor_type: str, actor_id: str, state_name: str, state_type: Type[Any] = object
) -> Tuple[bool, Any]:
raw_state_value = await self._state_client.get_state(actor_type, actor_id, state_name)
if (not raw_state_value) or len(raw_state_value) == 0:
return (False, None)
Expand All @@ -55,8 +57,8 @@ async def contains_state(self, actor_type: str, actor_id: str, state_name: str)
return (raw_state_value is not None) and len(raw_state_value) > 0

async def save_state(
self, actor_type: str, actor_id: str,
state_changes: List[ActorStateChange]) -> None:
self, actor_type: str, actor_id: str, state_changes: List[ActorStateChange]
) -> None:
"""
Transactional state update request body:
[
Expand Down
14 changes: 9 additions & 5 deletions dapr/actor/runtime/_timer_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ class ActorTimerData:
"""

def __init__(
self, timer_name: str,
callback: TIMER_CALLBACK, state: Any,
due_time: timedelta, period: timedelta,
ttl: Optional[timedelta] = None):
self,
timer_name: str,
callback: TIMER_CALLBACK,
state: Any,
due_time: timedelta,
period: timedelta,
ttl: Optional[timedelta] = None,
):
"""Create new :class:`ActorTimerData` instance.

Args:
Expand Down Expand Up @@ -96,7 +100,7 @@ def as_dict(self) -> Dict[str, Any]:
'callback': self._callback,
'data': self._state,
'dueTime': self._due_time,
'period': self._period
'period': self._period,
}

if self._ttl:
Expand Down
9 changes: 7 additions & 2 deletions dapr/actor/runtime/_type_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from dapr.actor.runtime._type_utils import is_dapr_actor, get_actor_interfaces

from typing import List, Type, TYPE_CHECKING

if TYPE_CHECKING:
from dapr.actor.actor_interface import ActorInterface # noqa: F401
from dapr.actor.runtime.actor import Actor # noqa: F401
Expand All @@ -27,8 +28,12 @@ class ActorTypeInformation:
implementing an actor.
"""

def __init__(self, name: str, implementation_class: Type['Actor'],
actor_bases: List[Type['ActorInterface']]):
def __init__(
self,
name: str,
implementation_class: Type['Actor'],
actor_bases: List[Type['ActorInterface']],
):
self._name = name
self._impl_type = implementation_class
self._actor_bases = actor_bases
Expand Down
8 changes: 4 additions & 4 deletions dapr/actor/runtime/_type_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


def get_class_method_args(func: Any) -> List[str]:
args = func.__code__.co_varnames[:func.__code__.co_argcount]
args = func.__code__.co_varnames[: func.__code__.co_argcount]

# Exclude self, cls arguments
if args[0] == 'self' or args[0] == 'cls':
Expand All @@ -46,8 +46,8 @@ def get_method_return_types(func: Any) -> Type:


def get_dispatchable_attrs_from_interface(
actor_interface: Type[ActorInterface],
dispatch_map: Dict[str, Any]) -> None:
actor_interface: Type[ActorInterface], dispatch_map: Dict[str, Any]
) -> None:
for attr, v in actor_interface.__dict__.items():
if attr.startswith('_') or not callable(v):
continue
Expand All @@ -58,7 +58,7 @@ def get_dispatchable_attrs_from_interface(
'method_name': attr,
'arg_names': get_class_method_args(v),
'arg_types': get_method_arg_types(v),
'return_types': get_method_return_types(v)
'return_types': get_method_return_types(v),
}


Expand Down
Loading
Loading