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

chore: add ruff to pre-commit, upgrade annotations to PEP585/604/696 #508

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
python-version: '3.11'
- name: Init python poetry action
uses: abatilo/[email protected]
with:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,4 @@ cython_debug/
.pytest_cache

# pycharm
.idea
.idea
16 changes: 15 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-toml
- id: check-added-large-files
- repo: local
hooks:
- id: black
Expand All @@ -21,7 +28,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/.*)$
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Contributing

When contributing to this repository, please first discuss the change you wish to make via issue,
email, or any other method with the owners of this repository before making a change.
email, or any other method with the owners of this repository before making a change.

Please note we have a code of conduct, please follow it in all your interactions with the project.

## Pull Request Process

1. Ensure any install or build dependencies are removed before the end of the layer when doing a
1. Ensure any install or build dependencies are removed before the end of the layer when doing a
build.
2. Update the README.md with details of changes to the interface, this includes new environment
2. Update the README.md with details of changes to the interface, this includes new environment
variables, useful file locations etc.
3. Increase the version numbers in any examples files and the README.md to the new version that this
Pull Request would represent. The versioning scheme we use is [SemVer](http://semver.org/).
4. You may merge the Pull Request in once you have the sign-off of two other developers, or if you
4. You may merge the Pull Request in once you have the sign-off of two other developers, or if you
do not have permission to do that, you may request the second reviewer to merge it for you.

## Code of Conduct
Expand Down
10 changes: 5 additions & 5 deletions docs/channels.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ In order to instantiate a ZeebeWorker or ZeebeClient you will need to provide an
This Channel can be configured with the parameters `channel_credentials` and `channel_options`.

.. seealso::

`Python Channel Options <https://grpc.github.io/grpc/python/glossary.html#term-channel_arguments>`_
Documentation of the available Python `grpc.aio.Channel` `options` (channel_arguments).


.. note::

By default, channel_options is defined so that the grpc.keepalive_time_ms option is always set to 45_000 (45 seconds).
Reference Camunda Docs `keep alive intervals <https://docs.camunda.io/docs/self-managed/zeebe-deployment/operations/setting-up-a-cluster/#keep-alive-intervals>`_.

You can override the default `channel_options` by passing
e.g. `channel_options = (("grpc.keepalive_time_ms", 60_000),)` - for a keepalive time of 60 seconds.


Pyzeebe provides a couple standard ways to achieve this:

Expand Down Expand Up @@ -81,7 +81,7 @@ Oauth2 Client Credentials Channel

.. autofunction:: pyzeebe.channel.oauth_channel.create_oauth2_client_credentials_channel

.. warning::
.. warning::
Some arguments are Optional and are highly dependent on your Authentication Server configuration,
`scope` is usually required and is often optional `audience` .

Expand Down Expand Up @@ -181,5 +181,5 @@ Example:

from pyzeebe.channel.camunda_cloud_channel import create_camunda_cloud_channel


channel = create_camunda_cloud_channel("client_id", "client_secret", "cluster_id")
4 changes: 2 additions & 2 deletions docs/credentials.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Oauth2 Client Credentials Plugin
:members:
:special-members:
:private-members:

Example:

.. code-block:: python
Expand Down Expand Up @@ -54,4 +54,4 @@ Internal (Deprecated)

.. autoclass:: pyzeebe.CamundaIdentityCredentials
:members:
:undoc-members:
:undoc-members:
5 changes: 2 additions & 3 deletions docs/worker_quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ Run using event loop
.. warning::

Calling ``worker.work`` directly using ``asyncio.run`` will not work. When you create an async grpc channel a new event loop will automatically be created, which causes problems when running the worker (see: https://github.com/camunda-community-hub/pyzeebe/issues/198).

An easy workaround:

.. code-block:: python

async def main():
channel = create_insecure_channel()
worker = ZeebeWorker(channel)
Expand Down Expand Up @@ -86,4 +86,3 @@ To stop a running worker:
# Trigger this on some event (SIGTERM for example)
async def shutdown():
await worker.stop()

4 changes: 2 additions & 2 deletions docs/worker_tasks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Tasks can be regular or async functions. If given a regular function, pyzeebe wi
.. note::

Make sure not to call any blocking function in an async task. This would slow the entire worker down.

Do:

.. code-block:: python
Expand Down Expand Up @@ -87,7 +87,7 @@ This tells Zeebe that the job failed. The job will then be retried (if configure

.. note::
The exception handler can also be set via :py:class:`pyzeebe.ZeebeWorker` or :py:class:`pyzeebe.ZeebeTaskRouter`.
Pyzeebe will try to find the exception handler in the following order:
Pyzeebe will try to find the exception handler in the following order:
``Worker`` -> ``Router`` -> ``Task`` -> :py:func:`pyzeebe.default_exception_handler`


Expand Down
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
25 changes: 22 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ strict = true

[[tool.mypy.overrides]]
module = [
"grpc",
"zeebe_grpc.gateway_pb2",
"zeebe_grpc.gateway_pb2_grpc",
"grpc",
"zeebe_grpc.gateway_pb2",
"zeebe_grpc.gateway_pb2_grpc",
]
ignore_missing_imports = true

Expand All @@ -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
Loading