Skip to content

Commit

Permalink
add ruff to pre-commit, upgrade code to 3.10 (use __future__.annotati…
Browse files Browse the repository at this point in the history
…ons)
  • Loading branch information
dimastbk committed Oct 9, 2024
1 parent cbe7617 commit d374a7c
Show file tree
Hide file tree
Showing 54 changed files with 363 additions and 254 deletions.
9 changes: 8 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ repos:
language: python
pass_filenames: false
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
rev: v3.17.0
hooks:
- id: pyupgrade
args: [--py39-plus]
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.6.9
hooks:
- id: ruff
args:
- --fix
exclude: ^(tests/.*|examples/.*|docs/.*)$
11 changes: 5 additions & 6 deletions examples/worker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
from typing import Dict

from pyzeebe import (
Job,
Expand Down Expand Up @@ -52,14 +51,14 @@ async def example_logging_task_decorator(job: Job) -> Job:

# Create a task like this:
@worker.task(task_type="test")
def example_task() -> Dict:
return {"output": f"Hello world, test!"}
def example_task() -> dict:
return {"output": "Hello world, test!"}


# Or like this:
@worker.task(task_type="test2")
async def second_example_task() -> Dict:
return {"output": f"Hello world, test2!"}
async def second_example_task() -> dict:
return {"output": "Hello world, test2!"}


# Create a task that will return a single value (not a dict) like this:
Expand Down Expand Up @@ -98,7 +97,7 @@ async def exception_task():
before=[example_logging_task_decorator],
after=[example_logging_task_decorator],
)
async def decorator_task() -> Dict:
async def decorator_task() -> dict:
return {"output": "Hello world, test!"}


Expand Down
19 changes: 19 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ profile = "black"
[tool.pytest.ini_options]
asyncio_mode = "auto"

[tool.ruff]
target-version = "py39"

[tool.ruff.lint]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"F", # pyflakes
"C", # flake8-comprehensions
"B", # flake8-bugbear
"TID", # flake8-tidy-imports
"T20", # flake8-print
"ASYNC", # flake8-async
"FA", # flake8-future-annotations
]
ignore = [
"E501", # line too long, handled by black
]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
8 changes: 7 additions & 1 deletion pyzeebe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from pyzeebe import errors
from pyzeebe.channel import *
from pyzeebe.channel import (
create_camunda_cloud_channel,
create_insecure_channel,
create_oauth2_client_credentials_channel,
create_secure_channel,
)
from pyzeebe.client.client import ZeebeClient
from pyzeebe.client.sync_client import SyncZeebeClient
from pyzeebe.credentials.base import CredentialsABC
Expand All @@ -18,6 +23,7 @@
"create_camunda_cloud_channel",
"create_insecure_channel",
"create_secure_channel",
"create_oauth2_client_credentials_channel",
"ZeebeClient",
"SyncZeebeClient",
"Job",
Expand Down
7 changes: 7 additions & 0 deletions pyzeebe/channel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@
create_oauth2_client_credentials_channel,
)
from pyzeebe.channel.secure_channel import create_secure_channel

__all__ = (
"create_insecure_channel",
"create_camunda_cloud_channel",
"create_oauth2_client_credentials_channel",
"create_secure_channel",
)
4 changes: 2 additions & 2 deletions pyzeebe/channel/camunda_cloud_channel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from __future__ import annotations

import grpc
from oauthlib import oauth2
Expand All @@ -24,7 +24,7 @@ def create_camunda_cloud_channel(
client_secret: str,
cluster_id: str,
region: str = "bru-2",
channel_options: Optional[ChannelArgumentType] = None,
channel_options: ChannelArgumentType | None = None,
) -> grpc.aio.Channel:
"""
Create channel connected to a Camunda Cloud cluster
Expand Down
4 changes: 2 additions & 2 deletions pyzeebe/channel/channel_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
https://docs.camunda.io/docs/product-manuals/zeebe/deployment-guide/operations/setting-up-a-cluster/#keep-alive-intervals
"""

from typing import Optional
from __future__ import annotations

from pyzeebe.types import ChannelArgumentType

GRPC_CHANNEL_OPTIONS_DEFAULT: ChannelArgumentType = (("grpc.keepalive_time_ms", 45_000),)


def get_channel_options(options: Optional[ChannelArgumentType] = None) -> ChannelArgumentType:
def get_channel_options(options: ChannelArgumentType | None = None) -> ChannelArgumentType:
"""
Get default channel options for creating the gRPC channel.
Expand Down
4 changes: 2 additions & 2 deletions pyzeebe/channel/insecure_channel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from __future__ import annotations

import grpc

Expand All @@ -8,7 +8,7 @@


def create_insecure_channel(
grpc_address: Optional[str] = None, channel_options: Optional[ChannelArgumentType] = None
grpc_address: str | None = None, channel_options: ChannelArgumentType | None = None
) -> grpc.aio.Channel:
"""
Create an insecure channel
Expand Down
24 changes: 12 additions & 12 deletions pyzeebe/channel/oauth_channel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from functools import partial
from typing import Optional

import grpc

Expand All @@ -13,12 +14,12 @@ def create_oauth2_client_credentials_channel(
client_id: str,
client_secret: str,
authorization_server: str,
scope: Optional[str] = None,
audience: Optional[str] = None,
channel_credentials: grpc.ChannelCredentials = grpc.ssl_channel_credentials(),
channel_options: Optional[ChannelArgumentType] = None,
scope: str | None = None,
audience: str | None = None,
channel_credentials: grpc.ChannelCredentials | None = None,
channel_options: ChannelArgumentType | None = None,
leeway: int = 60,
expire_in: Optional[int] = None,
expire_in: int | None = None,
) -> grpc.aio.Channel:
"""Create a gRPC channel for connecting to Camunda 8 (Self-Managed) with OAuth2ClientCredentials.
Expand Down Expand Up @@ -65,7 +66,7 @@ def create_oauth2_client_credentials_channel(

call_credentials: grpc.CallCredentials = grpc.metadata_call_credentials(oauth2_client_credentials)
composite_credentials: grpc.ChannelCredentials = grpc.composite_channel_credentials(
channel_credentials, call_credentials
channel_credentials or grpc.ssl_channel_credentials(), call_credentials
)

channel: grpc.aio.Channel = grpc.aio.secure_channel(
Expand All @@ -83,10 +84,10 @@ def create_camunda_cloud_channel(
scope: str = "Zeebe",
authorization_server: str = "https://login.cloud.camunda.io/oauth/token",
audience: str = "zeebe.camunda.io",
channel_credentials: grpc.ChannelCredentials = grpc.ssl_channel_credentials(),
channel_options: Optional[ChannelArgumentType] = None,
channel_credentials: grpc.ChannelCredentials | None = None,
channel_options: ChannelArgumentType | None = None,
leeway: int = 60,
expire_in: Optional[int] = None,
expire_in: int | None = None,
) -> grpc.aio.Channel:
"""Create a gRPC channel for connecting to Camunda 8 Cloud (SaaS).
Expand Down Expand Up @@ -139,9 +140,8 @@ def create_camunda_cloud_channel(
oauth2_client_credentials._func_retrieve_token = func

call_credentials: grpc.CallCredentials = grpc.metadata_call_credentials(oauth2_client_credentials)
# channel_credentials: grpc.ChannelCredentials = channel_credentials or grpc.ssl_channel_credentials()
composite_credentials: grpc.ChannelCredentials = grpc.composite_channel_credentials(
channel_credentials, call_credentials
channel_credentials or grpc.ssl_channel_credentials(), call_credentials
)

channel: grpc.aio.Channel = grpc.aio.secure_channel(
Expand Down
8 changes: 4 additions & 4 deletions pyzeebe/channel/secure_channel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from __future__ import annotations

import grpc

Expand All @@ -8,9 +8,9 @@


def create_secure_channel(
grpc_address: Optional[str] = None,
channel_options: Optional[ChannelArgumentType] = None,
channel_credentials: Optional[grpc.ChannelCredentials] = None,
grpc_address: str | None = None,
channel_options: ChannelArgumentType | None = None,
channel_credentials: grpc.ChannelCredentials | None = None,
) -> grpc.aio.Channel:
"""
Create a secure channel
Expand Down
5 changes: 3 additions & 2 deletions pyzeebe/channel/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations

import os
from typing import Optional

DEFAULT_ZEEBE_ADDRESS = "localhost:26500"


def create_address(
grpc_address: Optional[str] = None,
grpc_address: str | None = None,
) -> str:
if grpc_address:
return grpc_address
Expand Down
24 changes: 12 additions & 12 deletions pyzeebe/client/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Iterable, Optional
from __future__ import annotations

from collections.abc import Iterable

import grpc

Expand Down Expand Up @@ -28,9 +30,9 @@ def __init__(self, grpc_channel: grpc.aio.Channel, max_connection_retries: int =
async def run_process(
self,
bpmn_process_id: str,
variables: Optional[Variables] = None,
variables: Variables | None = None,
version: int = -1,
tenant_id: Optional[str] = None,
tenant_id: str | None = None,
) -> CreateProcessInstanceResponse:
"""
Run process
Expand Down Expand Up @@ -61,11 +63,11 @@ async def run_process(
async def run_process_with_result(
self,
bpmn_process_id: str,
variables: Optional[Variables] = None,
variables: Variables | None = None,
version: int = -1,
timeout: int = 0,
variables_to_fetch: Optional[Iterable[str]] = None,
tenant_id: Optional[str] = None,
variables_to_fetch: Iterable[str] | None = None,
tenant_id: str | None = None,
) -> CreateProcessInstanceWithResultResponse:
"""
Run process and wait for the result.
Expand Down Expand Up @@ -121,9 +123,7 @@ async def cancel_process_instance(self, process_instance_key: int) -> CancelProc
"""
return await self.zeebe_adapter.cancel_process_instance(process_instance_key=process_instance_key)

async def deploy_resource(
self, *resource_file_path: str, tenant_id: Optional[str] = None
) -> DeployResourceResponse:
async def deploy_resource(self, *resource_file_path: str, tenant_id: str | None = None) -> DeployResourceResponse:
"""
Deploy one or more processes
Expand All @@ -150,10 +150,10 @@ async def publish_message(
self,
name: str,
correlation_key: str,
variables: Optional[Variables] = None,
variables: Variables | None = None,
time_to_live_in_milliseconds: int = 60000,
message_id: Optional[str] = None,
tenant_id: Optional[str] = None,
message_id: str | None = None,
tenant_id: str | None = None,
) -> PublishMessageResponse:
"""
Publish a message
Expand Down
21 changes: 11 additions & 10 deletions pyzeebe/client/sync_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import asyncio
from typing import List, Optional

import grpc

Expand All @@ -22,20 +23,20 @@ def __init__(self, grpc_channel: grpc.aio.Channel, max_connection_retries: int =
def run_process(
self,
bpmn_process_id: str,
variables: Optional[Variables] = None,
variables: Variables | None = None,
version: int = -1,
tenant_id: Optional[str] = None,
tenant_id: str | None = None,
) -> CreateProcessInstanceResponse:
return self.loop.run_until_complete(self.client.run_process(bpmn_process_id, variables, version, tenant_id))

def run_process_with_result(
self,
bpmn_process_id: str,
variables: Optional[Variables] = None,
variables: Variables | None = None,
version: int = -1,
timeout: int = 0,
variables_to_fetch: Optional[List[str]] = None,
tenant_id: Optional[str] = None,
variables_to_fetch: list[str] | None = None,
tenant_id: str | None = None,
) -> CreateProcessInstanceWithResultResponse:
return self.loop.run_until_complete(
self.client.run_process_with_result(
Expand All @@ -46,17 +47,17 @@ def run_process_with_result(
def cancel_process_instance(self, process_instance_key: int) -> CancelProcessInstanceResponse:
return self.loop.run_until_complete(self.client.cancel_process_instance(process_instance_key))

def deploy_resource(self, *resource_file_path: str, tenant_id: Optional[str] = None) -> DeployResourceResponse:
def deploy_resource(self, *resource_file_path: str, tenant_id: str | None = None) -> DeployResourceResponse:
return self.loop.run_until_complete(self.client.deploy_resource(*resource_file_path, tenant_id=tenant_id))

def publish_message(
self,
name: str,
correlation_key: str,
variables: Optional[Variables] = None,
variables: Variables | None = None,
time_to_live_in_milliseconds: int = 60000,
message_id: Optional[str] = None,
tenant_id: Optional[str] = None,
message_id: str | None = None,
tenant_id: str | None = None,
) -> PublishMessageResponse:
return self.loop.run_until_complete(
self.client.publish_message(
Expand Down
8 changes: 8 additions & 0 deletions pyzeebe/credentials/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
from .camunda_identity import CamundaIdentityCredentials
from .oauth import Oauth2ClientCredentialsMetadataPlugin, OAuth2MetadataPlugin
from .plugins import AuthMetadataPlugin

__all__ = (
"CredentialsABC",
"CamundaIdentityCredentials",
"Oauth2ClientCredentialsMetadataPlugin",
"OAuth2MetadataPlugin",
"AuthMetadataPlugin",
)
Loading

0 comments on commit d374a7c

Please sign in to comment.