From 44eae9aa397368165e94804a5c576b11d07004f3 Mon Sep 17 00:00:00 2001 From: Dan Fuchs Date: Mon, 9 Dec 2024 21:33:55 -0600 Subject: [PATCH] Just call everything P and be done with it --- safir/src/safir/metrics/_event_manager.py | 28 ++++++----------------- safir/tests/metrics/event_manager_test.py | 4 +++- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/safir/src/safir/metrics/_event_manager.py b/safir/src/safir/metrics/_event_manager.py index 9366b3fc..44fdc2d9 100644 --- a/safir/src/safir/metrics/_event_manager.py +++ b/safir/src/safir/metrics/_event_manager.py @@ -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 @@ -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", @@ -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 @@ -153,7 +139,7 @@ def __init__( *, application: str, manager: KafkaEventManager, - event_class: type[EnrichedEvent], + event_class: type[P], publisher: AsyncAPIDefaultPublisher, schema_info: SchemaInfo, ) -> None: @@ -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) @@ -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) @@ -500,7 +486,7 @@ async def initialize(self) -> None: async def publish( self, - event: EnrichedEvent, + event: P, publisher: AsyncAPIDefaultPublisher, schema_info: SchemaInfo | None, ) -> None: diff --git a/safir/tests/metrics/event_manager_test.py b/safir/tests/metrics/event_manager_test.py index dad720ab..4eb730ec 100644 --- a/safir/tests/metrics/event_manager_test.py +++ b/safir/tests/metrics/event_manager_test.py @@ -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 @@ -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)