Skip to content

Fix generic cardinality discriminator type alias #15

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

Merged
merged 2 commits into from
Apr 16, 2025
Merged
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
19 changes: 13 additions & 6 deletions streamdeck/models/events/common.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations

from abc import ABC
from typing import Annotated, Any, Generic, Literal, NamedTuple, Optional, Union
from typing import Annotated, Generic, Literal, NamedTuple, Optional, Union

from pydantic import Field
from pydantic import Field, JsonValue
from typing_extensions import TypedDict, TypeVar

from streamdeck.models.events.base import ConfiguredBaseModel
Expand All @@ -28,8 +28,11 @@ class DeviceSpecificEventMixin:
## Payload models and metadata used by multiple event models.


PluginDefinedData = dict[str, Any]
"""Data of arbitrary structure that is defined in and relevant to the plugin."""
PluginDefinedData = dict[str, JsonValue]
"""Data of arbitrary structure that is defined in and relevant to the plugin.

The root of the data structure will always be a dict of string keys, while the values can be any JSON-compatible type.
"""


EncoderControllerType = Literal["Encoder"]
Expand Down Expand Up @@ -116,10 +119,14 @@ class MultiActionPayloadMixin:
"""Indicates that this event is part of a multi-action."""


# These need to be covariant, as the Mixin classes are never meant to be instantiated themselves, only inherited from.
SingleActionPayload_co = TypeVar("SingleActionPayload_co", bound=SingleActionPayloadMixin, covariant=True)
MultiActionPayload_co = TypeVar("MultiActionPayload_co", bound=MultiActionPayloadMixin, covariant=True)

CardinalityDiscriminated = Annotated[
Union[ # noqa: UP007
TypeVar("S", bound=SingleActionPayloadMixin),
TypeVar("M", bound=MultiActionPayloadMixin),
SingleActionPayload_co,
MultiActionPayload_co,
],
Field(discriminator="is_in_multi_action"),
]
Expand Down