Skip to content

Commit

Permalink
fix: make ruff and mypy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
betaboon committed Jan 30, 2024
1 parent dd6e765 commit c1f22ce
Show file tree
Hide file tree
Showing 64 changed files with 472 additions and 430 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
python_import_name = "eventstoredb"


def linkcode_resolve(domain: str, info: dict[str, str]) -> str | None: # noqa: D103
def linkcode_resolve(domain: str, info: dict[str, str]) -> str | None:
if domain != "py":
return None

Expand Down
4 changes: 4 additions & 0 deletions eventstoredb/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
from eventstoredb.client.client import Client

__all__ = [
"Client",
]
12 changes: 8 additions & 4 deletions eventstoredb/client/append_to_stream/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from __future__ import annotations

from eventstoredb.client.append_to_stream.types import AppendExpectedRevision
from eventstoredb.client.exceptions import ClientException
from eventstoredb.types import StreamRevision
from typing import TYPE_CHECKING

from eventstoredb.client.exceptions import ClientError

class AppendToStreamError(ClientException):
if TYPE_CHECKING:
from eventstoredb.client.append_to_stream.types import AppendExpectedRevision
from eventstoredb.types import StreamRevision


class AppendToStreamError(ClientError):
pass


Expand Down
26 changes: 16 additions & 10 deletions eventstoredb/client/append_to_stream/grpc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import betterproto

from eventstoredb.client.append_to_stream.exceptions import (
Expand All @@ -12,7 +14,6 @@
AppendToStreamOptions,
)
from eventstoredb.client.exceptions import StreamNotFoundError
from eventstoredb.events import EventData
from eventstoredb.generated.event_store.client import Empty, StreamIdentifier, Uuid
from eventstoredb.generated.event_store.client.streams import (
AppendReq,
Expand All @@ -24,6 +25,9 @@
)
from eventstoredb.types import AllPosition, StreamRevision

if TYPE_CHECKING:
from eventstoredb.events import EventData


def create_append_header(stream_name: str, options: AppendToStreamOptions) -> AppendReq:
request_options = AppendReqOptions()
Expand Down Expand Up @@ -60,11 +64,12 @@ def convert_append_response(stream_name: str, message: AppendResp) -> AppendResu
stream_name=stream_name,
message=message.wrong_expected_version,
)
elif result_type == "success":

if result_type == "success":
return convert_append_response_success(message=message.success)
else:
# TODO raise a more specific exception
raise Exception("I shouldnt be here")

# TODO raise a more specific exception
raise Exception("I shouldnt be here") # noqa: TRY002,TRY003


def convert_append_response_success(message: AppendRespSuccess) -> AppendResult:
Expand Down Expand Up @@ -92,19 +97,20 @@ def convert_wrong_expected_version(

if expected_type == "expected_no_stream":
return StreamAlreadyExistsError(stream_name=stream_name)
elif expected_type == "expected_stream_exists":

if expected_type == "expected_stream_exists":
return StreamNotFoundError(stream_name=stream_name)
elif expected_type == "expected_revision" and current_type == "current_no_stream":
if expected_type == "expected_revision" and current_type == "current_no_stream":
return StreamNotFoundError(stream_name=stream_name)
elif expected_type == "expected_any" and current_type == "current_no_stream":
if expected_type == "expected_any" and current_type == "current_no_stream":
return StreamNotFoundError(stream_name=stream_name)
elif expected_type == "expected_revision" and current_type == "current_revision":
if expected_type == "expected_revision" and current_type == "current_revision":
return RevisionMismatchError(
stream_name=stream_name,
expected_revision=message.expected_revision,
current_revision=message.current_revision,
)
elif expected_type == "expected_any" and current_type == "current_revision":
if expected_type == "expected_any" and current_type == "current_revision":
return RevisionMismatchError(
stream_name=stream_name,
expected_revision=AppendExpectedRevision.ANY,
Expand Down
5 changes: 4 additions & 1 deletion eventstoredb/client/append_to_stream/mixin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import AsyncGenerator, Iterable
from typing import TYPE_CHECKING

from eventstoredb.client.append_to_stream.grpc import (
convert_append_response,
Expand All @@ -15,6 +15,9 @@
from eventstoredb.events import EventData
from eventstoredb.generated.event_store.client.streams import AppendReq, StreamsStub

if TYPE_CHECKING:
from collections.abc import AsyncGenerator, Iterable


class AppendToStreamMixin(ClientProtocol):
async def append_to_stream(
Expand Down
8 changes: 4 additions & 4 deletions eventstoredb/client/append_to_stream/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from dataclasses import dataclass
from enum import Enum, auto
from typing import TYPE_CHECKING

from eventstoredb.types import AllPosition, StreamRevision
if TYPE_CHECKING:
from eventstoredb.types import AllPosition, StreamRevision


class AppendExpectedRevision(Enum):
Expand All @@ -14,9 +16,7 @@ class AppendExpectedRevision(Enum):

@dataclass
class AppendToStreamOptions:
expected_revision: AppendExpectedRevision | StreamRevision = (
AppendExpectedRevision.ANY
)
expected_revision: AppendExpectedRevision | StreamRevision = AppendExpectedRevision.ANY


@dataclass
Expand Down
26 changes: 12 additions & 14 deletions eventstoredb/client/create_persistent_subscription_to_all/grpc.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import annotations

from eventstoredb.client.create_persistent_subscription_to_all.types import (
CreatePersistentSubscriptionToAllOptions,
)
from typing import TYPE_CHECKING

from eventstoredb.client.create_persistent_subscription_to_stream.grpc import (
create_persistent_subscription_request_settings,
)
Expand All @@ -24,8 +23,13 @@
)
from eventstoredb.types import AllPosition, StreamPosition

if TYPE_CHECKING:
from eventstoredb.client.create_persistent_subscription_to_all.types import (
CreatePersistentSubscriptionToAllOptions,
)

def create_create_persistent_subscription_to_all_request(

def create_create_persistent_subscription_to_all_request( # noqa: C901
group_name: str,
options: CreatePersistentSubscriptionToAllOptions,
) -> CreateReq:
Expand All @@ -42,12 +46,8 @@ def create_create_persistent_subscription_to_all_request(

if isinstance(options.from_position, AllPosition):
request_options.all.position = CreateReqPosition()
request_options.all.position.commit_position = (
options.from_position.commit_position
)
request_options.all.position.prepare_position = (
options.from_position.prepare_position
)
request_options.all.position.commit_position = options.from_position.commit_position
request_options.all.position.prepare_position = options.from_position.prepare_position
elif options.from_position == StreamPosition.START:
request_options.all.start = Empty()
elif options.from_position == StreamPosition.END:
Expand All @@ -67,7 +67,7 @@ def create_create_persistent_subscription_to_all_request(
if options.filter.prefix:
filter_expression.prefix = options.filter.prefix

if isinstance(options.filter, ExcludeSystemEventsFilter):
if isinstance(options.filter, ExcludeSystemEventsFilter): # noqa: SIM114
request_options.all.filter.event_type = filter_expression
elif isinstance(options.filter, EventTypeFilter):
request_options.all.filter.event_type = filter_expression
Expand All @@ -79,8 +79,6 @@ def create_create_persistent_subscription_to_all_request(
else:
request_options.all.filter.count = Empty()

request_options.all.filter.checkpoint_interval_multiplier = (
options.checkpoint_interval
)
request_options.all.filter.checkpoint_interval_multiplier = options.checkpoint_interval

return CreateReq(options=request_options)
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ async def create_persistent_subscription_to_all(
try:
await client.create(create_req=request)
except GRPCError as e:
raise convert_grpc_error_to_exception(e)
raise convert_grpc_error_to_exception(e) # noqa: B904,TRY200
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from __future__ import annotations

from dataclasses import dataclass, field
from typing import TYPE_CHECKING

from eventstoredb.client.create_persistent_subscription_to_stream.types import (
PersistentSubscriptionSettings,
)
from eventstoredb.filters import (
EventTypeFilter,
ExcludeSystemEventsFilter,
StreamNameFilter,
)
from eventstoredb.types import AllPosition, StreamPosition

if TYPE_CHECKING:
from eventstoredb.filters import (
EventTypeFilter,
ExcludeSystemEventsFilter,
StreamNameFilter,
)


@dataclass
class CreatePersistentSubscriptionToAllOptions:
settings: PersistentSubscriptionSettings = field(
default_factory=PersistentSubscriptionSettings
)
settings: PersistentSubscriptionSettings = field(default_factory=PersistentSubscriptionSettings)
from_position: AllPosition | StreamPosition = StreamPosition.START
filter: ExcludeSystemEventsFilter | EventTypeFilter | StreamNameFilter | None = None
max_search_window: int | None = None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from eventstoredb.client.exceptions import ClientException
from eventstoredb.client.exceptions import ClientError


class PersistentSubscriptionError(ClientException):
class PersistentSubscriptionError(ClientError):
pass


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from __future__ import annotations

from typing import Type, TypeVar
from typing import TYPE_CHECKING, TypeVar

from grpclib.const import Status as GRPCStatus
from grpclib.exceptions import GRPCError

from eventstoredb.client.create_persistent_subscription_to_stream.exceptions import (
PersistentSubscriptionAlreadyExistsError,
Expand All @@ -29,6 +28,9 @@
)
from eventstoredb.types import StreamPosition, StreamRevision

if TYPE_CHECKING:
from grpclib.exceptions import GRPCError

SettingsClass = TypeVar(
"SettingsClass",
CreateReqSettings,
Expand All @@ -43,8 +45,8 @@

def create_persistent_subscription_request_settings(
settings: PersistentSubscriptionSettings,
settings_class: Type[SettingsClass],
consumer_strategy_class: Type[ConsumerStrategyClass],
settings_class: type[SettingsClass],
consumer_strategy_class: type[ConsumerStrategyClass],
) -> SettingsClass:
request_settings = settings_class()
request_settings.resolve_links = settings.resolve_links
Expand Down Expand Up @@ -107,11 +109,10 @@ def convert_grpc_error_to_exception(
) -> PersistentSubscriptionError | GRPCError:
if error.status == GRPCStatus.CANCELLED:
return PersistentSubscriptionDroppedError(error.message)
elif error.status == GRPCStatus.NOT_FOUND:
if error.status == GRPCStatus.NOT_FOUND:
return PersistentSubscriptionNotFoundError(error.message)
elif error.status == GRPCStatus.ALREADY_EXISTS:
if error.status == GRPCStatus.ALREADY_EXISTS:
return PersistentSubscriptionAlreadyExistsError(error.message)
elif error.status == GRPCStatus.FAILED_PRECONDITION:
if error.status == GRPCStatus.FAILED_PRECONDITION:
return PersistentSubscriptionMaxSubscribersReachedError(error.message)
else:
return error
return error
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ async def create_persistent_subscription_to_stream(
try:
await client.create(create_req=request)
except GRPCError as e:
raise convert_grpc_error_to_exception(e)
raise convert_grpc_error_to_exception(e) # noqa: B904,TRY200
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ class PersistentSubscriptionSettings:
@dataclass
class CreatePersistentSubscriptionToStreamOptions:
settings: PersistentSubscriptionSettings = field(
default_factory=PersistentSubscriptionSettings
default_factory=PersistentSubscriptionSettings,
)
from_revision: StreamRevision | StreamPosition = StreamPosition.START
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from __future__ import annotations

from eventstoredb.client.delete_persistent_subscription_to_all.types import (
DeletePersistentSubscriptionToAllOptions,
)
from typing import TYPE_CHECKING

from eventstoredb.generated.event_store.client import Empty
from eventstoredb.generated.event_store.client.persistent_subscriptions import (
DeleteReq,
DeleteReqOptions,
)

if TYPE_CHECKING:
from eventstoredb.client.delete_persistent_subscription_to_all.types import (
DeletePersistentSubscriptionToAllOptions,
)


def create_delete_persistent_subscription_to_all_request(
group_name: str,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ async def delete_persistent_subscription_to_all(
try:
await client.delete(delete_req=request)
except GRPCError as e:
raise convert_grpc_error_to_exception(e)
raise convert_grpc_error_to_exception(e) # noqa: B904,TRY200
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@


@dataclass
class DeletePersistentSubscriptionToAllOptions:
...
class DeletePersistentSubscriptionToAllOptions: ...
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from __future__ import annotations

from eventstoredb.client.delete_persistent_subscription_to_stream.types import (
DeletePersistentSubscriptionToStreamOptions,
)
from typing import TYPE_CHECKING

from eventstoredb.generated.event_store.client import StreamIdentifier
from eventstoredb.generated.event_store.client.persistent_subscriptions import (
DeleteReq,
DeleteReqOptions,
)

if TYPE_CHECKING:
from eventstoredb.client.delete_persistent_subscription_to_stream.types import (
DeletePersistentSubscriptionToStreamOptions,
)


def create_delete_persistent_subscription_to_stream_request(
stream_name: str,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ async def delete_persistent_subscription_to_stream(
try:
await client.delete(delete_req=request)
except GRPCError as e:
raise convert_grpc_error_to_exception(e)
raise convert_grpc_error_to_exception(e) # noqa: B904,TRY200
5 changes: 2 additions & 3 deletions eventstoredb/client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ def __init__(self, connection_string: str) -> None:
)


class ClientException(Exception):
...
class ClientError(Exception): ...


class StreamNotFoundError(ClientException):
class StreamNotFoundError(ClientError):
def __init__(self, stream_name: str) -> None:
self.stream_name = stream_name
super().__init__(f"Stream '{stream_name}' not found")
Loading

0 comments on commit c1f22ce

Please sign in to comment.