Skip to content

Commit

Permalink
Just call everything P and be done with it
Browse files Browse the repository at this point in the history
  • Loading branch information
fajpunk committed Dec 10, 2024
1 parent dd2d301 commit 44eae9a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
28 changes: 7 additions & 21 deletions safir/src/safir/metrics/_event_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import time
from abc import ABCMeta, abstractmethod
from datetime import UTC, datetime
from typing import Generic, TypeAlias, TypeVar, cast
from typing import Generic, TypeVar, cast
from uuid import uuid4

import structlog
Expand All @@ -28,18 +28,6 @@
P = TypeVar("P", bound=EventPayload)
"""Generic event payload type."""

EnrichedEvent: TypeAlias = P | EventMetadata
"""Alias to refer to the dynamically created payload+metadata event type.
Since this type is dynamically created, there is no way to name it or make it a
TypeVar. We could declare it as ``P`` everywhere we need to declare it, but
that makes it difficult to see when we are using the payload vs the
payload+metadata types.
This lets us differentiate it from a payload class in the code, and it can be
narrowed further if we have to access either metadata or payload attributes.
"""

__all__ = [
"EventManager",
"EventPublisher",
Expand All @@ -66,13 +54,11 @@ class EventPublisher(Generic[P], metaclass=ABCMeta):
publication.
"""

def __init__(
self, application: str, event_class: type[EnrichedEvent]
) -> None:
def __init__(self, application: str, event_class: type[P]) -> None:
self._application = application
self._event_class = event_class

def construct_event(self, payload: P) -> EnrichedEvent:
def construct_event(self, payload: P) -> P:
"""Construct the full event as it will be published.
Parameters
Expand Down Expand Up @@ -153,7 +139,7 @@ def __init__(
*,
application: str,
manager: KafkaEventManager,
event_class: type[EnrichedEvent],
event_class: type[P],
publisher: AsyncAPIDefaultPublisher,
schema_info: SchemaInfo,
) -> None:
Expand All @@ -179,7 +165,7 @@ class NoopEventPublisher(EventPublisher, Generic[P]):
def __init__(
self,
application: str,
event_class: type[EnrichedEvent],
event_class: type[P],
logger: BoundLogger,
) -> None:
super().__init__(application, event_class)
Expand All @@ -204,7 +190,7 @@ class MockEventPublisher(NoopEventPublisher, Generic[P]):
def __init__(
self,
application: str,
event_class: type[EnrichedEvent],
event_class: type[P],
logger: BoundLogger,
) -> None:
super().__init__(application, event_class, logger)
Expand Down Expand Up @@ -500,7 +486,7 @@ async def initialize(self) -> None:

async def publish(
self,
event: EnrichedEvent,
event: P,
publisher: AsyncAPIDefaultPublisher,
schema_info: SchemaInfo | None,
) -> None:
Expand Down
4 changes: 3 additions & 1 deletion safir/tests/metrics/event_manager_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import math
from datetime import timedelta
from enum import Enum
from typing import cast
from uuid import UUID

import pytest
from aiokafka import AIOKafkaConsumer
from aiokafka.admin.client import AIOKafkaAdminClient, NewTopic
from dataclasses_avroschema.pydantic import AvroBaseModel
from faststream.kafka import KafkaBroker
from pydantic import Field
from schema_registry.client.client import AsyncSchemaRegistryClient
Expand Down Expand Up @@ -79,7 +81,7 @@ async def assert_from_kafka(
serializer = AsyncAvroMessageSerializer(schema_registry)
deserialized_dict = await serializer.decode_message(message.value)
assert deserialized_dict is not None
event_class = event._event_class
event_class = cast(type[AvroBaseModel], event._event_class)
deserialized = event_class(**deserialized_dict)

assert isinstance(deserialized, EventMetadata)
Expand Down

0 comments on commit 44eae9a

Please sign in to comment.