Skip to content

Commit

Permalink
Pin minimum versions of dependencies (#13)
Browse files Browse the repository at this point in the history
* Auto update pre-commit hooks

* Update .gitignore

* Pin lower versions of prod dependencies

* Add pre-commit fixes
  • Loading branch information
sidmitra authored Jan 26, 2023
1 parent c4fa4b1 commit 38ea18d
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 216 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ share/python-wheels/
*.egg
MANIFEST

#IDE
#IDE
.idea*

# PyInstaller
Expand Down
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
repos:

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.4.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-yaml

- repo: https://github.com/asottile/pyupgrade
rev: v3.0.0
rev: v3.3.1
hooks:
- id: pyupgrade
args: ["--py39-plus", "--keep-runtime-typing"]
Expand All @@ -29,24 +29,24 @@ repos:
- toml

- repo: https://github.com/ambv/black
rev: 22.10.0
rev: 22.12.0
hooks:
- id: black
args: [--line-length=88, --safe]

- repo: https://github.com/myint/autoflake
rev: v1.7.0
rev: v2.0.0
hooks:
- id: autoflake
args: [--in-place, --remove-all-unused-import]

- repo: https://github.com/pycqa/flake8
rev: 5.0.4
rev: 6.0.0
hooks:
- id: flake8

- repo: https://github.com/pycqa/pylint
rev: v2.15.3
rev: v2.16.0b1
hooks:
- id: pylint
args: [--extension-pkg-whitelist=confluent_kafka]
Expand Down
4 changes: 2 additions & 2 deletions eventbusk/brokers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from abc import ABC, abstractmethod
from contextlib import ContextDecorator
from types import TracebackType
from typing import Callable, Optional, Type, Union
from typing import Callable, Optional, Union

from confluent_kafka import cimpl # type: ignore

Expand Down Expand Up @@ -64,7 +64,7 @@ def __enter__(self) -> BaseConsumer:

def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_type: Optional[type[BaseException]],
exc_value: Optional[BaseException],
exc_traceback: Optional[TracebackType],
) -> None:
Expand Down
12 changes: 7 additions & 5 deletions eventbusk/brokers/kafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
from dataclasses import dataclass
from types import TracebackType
from typing import TYPE_CHECKING, Optional, Type, Union
from typing import TYPE_CHECKING, Optional, Union

from confluent_kafka import Consumer as CConsumer # type: ignore
from confluent_kafka import KafkaError
Expand Down Expand Up @@ -163,7 +163,7 @@ def __enter__(self) -> Consumer:

def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_type: Optional[type[BaseException]],
exc_value: Optional[BaseException],
exc_traceback: Optional[TracebackType],
) -> None:
Expand All @@ -173,9 +173,11 @@ def __exit__(
logger.warning(
"Kafka consumer error.",
exc_info=True,
extra=dict(
exc_type=exc_type, exc_value=exc_value, exc_traceback=exc_traceback
),
extra={
"exc_type": exc_type,
"exc_value": exc_value,
"exc_traceback": exc_traceback,
},
)

def poll(self, timeout: int) -> Optional[MessageT]:
Expand Down
35 changes: 22 additions & 13 deletions eventbusk/bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from abc import ABC
from dataclasses import asdict, dataclass, field
from functools import wraps
from typing import Callable, Type, Union
from typing import Callable, Union

from confluent_kafka import KafkaError # type: ignore

Expand All @@ -32,17 +32,22 @@ class MyEvent(Event):
foo: int
bar: str
"""

event_id: uuid.UUID = field(default_factory=uuid.uuid4, init=False)


class EventJsonEncoder(json.JSONEncoder):
"""
JSON encoder that additionally converts uuid to str.
"""

def default(self, o):
if isinstance(o, uuid.UUID):
return str(o)
return json.JSONEncoder.default(self, o)


EventT = Type[Event]
EventT = type[Event]
ReceiverT = Callable[[Event], None]
ReceiverWrappedT = Callable[[], None]
ReceivedOuterT = Callable[[ReceiverT], ReceiverWrappedT]
Expand Down Expand Up @@ -139,7 +144,11 @@ def send(
if fail_silently:
logger.warning(
"Error producing event.",
extra={"event": event_fqn, "topic": topic, "event_id": event.event_id},
extra={
"event": event_fqn,
"topic": topic,
"event_id": event.event_id,
},
exc_info=True,
)
else:
Expand All @@ -161,7 +170,7 @@ def receive(self, event_type: EventT, poll_timeout: int = 1) -> ReceivedOuterT:
bus.
"""
event_fqn = self.to_fqn(event_type)
if event_fqn not in self._event_to_topic.keys():
if event_fqn not in self._event_to_topic:
raise UnknownEvent(
"Register the event to a topic using "
f"`bus.register_event('foo_topic', {event_type})`"
Expand All @@ -172,9 +181,12 @@ def _outer(func: ReceiverT) -> ReceiverWrappedT:
group = self.to_fqn(func)
receiver_fqn = self.to_fqn(func)
topic = self._event_to_topic[event_fqn]
log_context = dict(
event=event_fqn, receiver=receiver_fqn, topic=topic, group=group
)
log_context = {
"event": event_fqn,
"receiver": receiver_fqn,
"topic": topic,
"group": group,
}

@wraps(func)
def wrapper() -> None:
Expand Down Expand Up @@ -226,23 +238,20 @@ def wrapper() -> None:

if "event_id" in event_data:
try:
event_id = uuid.UUID(event_data.pop('event_id'))
event_id = uuid.UUID(event_data.pop("event_id"))
except ValueError:
logger.exception(
(
"Error while converting str -> UUID "
),
("Error while converting str -> UUID "),
extra={**log_context, **{"data": event_data}},
exc_info=True,
)
pass
else:
event_id = None

# TODO: Fix following
# Too many arguments for "Event" [call-arg]
event = event_type(**event_data) # type: ignore
setattr(event, 'event_id', event_id)
setattr(event, "event_id", event_id)

try:
func(event)
Expand Down
3 changes: 2 additions & 1 deletion eventbusk/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import os
import sys
import threading
from collections.abc import Generator
from contextlib import contextmanager, suppress
from types import ModuleType
from typing import Callable, Generator, Optional
from typing import Callable, Optional

import click
import cotyledon # type: ignore
Expand Down
Loading

0 comments on commit 38ea18d

Please sign in to comment.