Skip to content

Commit b8ac198

Browse files
committed
chore: define type alias for EventFactory and use it everywhere
Signed-off-by: Tudor Plugaru <[email protected]>
1 parent a25029e commit b8ac198

File tree

6 files changed

+35
-45
lines changed

6 files changed

+35
-45
lines changed

src/cloudevents/core/base.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@
1313
# under the License.
1414

1515
from datetime import datetime
16-
from typing import Any, Protocol
16+
from typing import Any, Callable, Protocol
17+
18+
EventFactory = Callable[
19+
[dict[str, Any], dict[str, Any] | str | bytes | None], "BaseCloudEvent"
20+
]
21+
"""
22+
Type alias for a callable that creates a BaseCloudEvent from attributes and data.
23+
24+
Args:
25+
attributes: The CloudEvent attributes (required fields like id, source, type, etc.)
26+
data: The CloudEvent data payload (optional)
27+
28+
Returns:
29+
A BaseCloudEvent instance
30+
"""
1731

1832

1933
class BaseCloudEvent(Protocol):

src/cloudevents/core/bindings/amqp.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
from dataclasses import dataclass
1616
from datetime import datetime, timezone
17-
from typing import Any, Callable, Final
17+
from typing import Any, Final
1818

1919
from dateutil.parser import isoparse
2020

21-
from cloudevents.core.base import BaseCloudEvent
21+
from cloudevents.core.base import BaseCloudEvent, EventFactory
2222
from cloudevents.core.formats.base import Format
2323

2424
# AMQP CloudEvents spec allows both cloudEvents_ and cloudEvents: prefixes
@@ -149,9 +149,7 @@ def to_binary(event: BaseCloudEvent, event_format: Format) -> AMQPMessage:
149149
def from_binary(
150150
message: AMQPMessage,
151151
event_format: Format,
152-
event_factory: Callable[
153-
[dict[str, Any], dict[str, Any] | str | bytes | None], BaseCloudEvent
154-
],
152+
event_factory: EventFactory,
155153
) -> BaseCloudEvent:
156154
"""
157155
Parse an AMQP binary content mode message to a CloudEvent.
@@ -250,9 +248,7 @@ def to_structured(event: BaseCloudEvent, event_format: Format) -> AMQPMessage:
250248
def from_structured(
251249
message: AMQPMessage,
252250
event_format: Format,
253-
event_factory: Callable[
254-
[dict[str, Any], dict[str, Any] | str | bytes | None], BaseCloudEvent
255-
],
251+
event_factory: EventFactory,
256252
) -> BaseCloudEvent:
257253
"""
258254
Parse an AMQP structured content mode message to a CloudEvent.
@@ -283,9 +279,7 @@ def from_structured(
283279
def from_amqp(
284280
message: AMQPMessage,
285281
event_format: Format,
286-
event_factory: Callable[
287-
[dict[str, Any], dict[str, Any] | str | bytes | None], BaseCloudEvent
288-
],
282+
event_factory: EventFactory,
289283
) -> BaseCloudEvent:
290284
"""
291285
Parse an AMQP message to a CloudEvent with automatic mode detection.

src/cloudevents/core/bindings/http.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
# under the License.
1414

1515
from dataclasses import dataclass
16-
from typing import Any, Callable, Final
16+
from typing import Any, Final
1717

18-
from cloudevents.core.base import BaseCloudEvent
18+
from cloudevents.core.base import BaseCloudEvent, EventFactory
1919
from cloudevents.core.bindings.common import (
2020
CONTENT_TYPE_HEADER,
2121
DATACONTENTTYPE_ATTR,
@@ -92,9 +92,7 @@ def to_binary(event: BaseCloudEvent, event_format: Format) -> HTTPMessage:
9292
def from_binary(
9393
message: HTTPMessage,
9494
event_format: Format,
95-
event_factory: Callable[
96-
[dict[str, Any], dict[str, Any] | str | bytes | None], BaseCloudEvent
97-
],
95+
event_factory: EventFactory,
9896
) -> BaseCloudEvent:
9997
"""
10098
Parse an HTTP binary content mode message to a CloudEvent.
@@ -172,9 +170,7 @@ def to_structured(event: BaseCloudEvent, event_format: Format) -> HTTPMessage:
172170
def from_structured(
173171
message: HTTPMessage,
174172
event_format: Format,
175-
event_factory: Callable[
176-
[dict[str, Any], dict[str, Any] | str | bytes | None], BaseCloudEvent
177-
],
173+
event_factory: EventFactory,
178174
) -> BaseCloudEvent:
179175
"""
180176
Parse an HTTP structured content mode message to a CloudEvent.
@@ -203,9 +199,7 @@ def from_structured(
203199
def from_http(
204200
message: HTTPMessage,
205201
event_format: Format,
206-
event_factory: Callable[
207-
[dict[str, Any], dict[str, Any] | str | bytes | None], BaseCloudEvent
208-
],
202+
event_factory: EventFactory,
209203
) -> BaseCloudEvent:
210204
"""
211205
Parse an HTTP message to a CloudEvent with automatic mode detection.

src/cloudevents/core/bindings/kafka.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from dataclasses import dataclass
1616
from typing import Any, Callable, Final
1717

18-
from cloudevents.core.base import BaseCloudEvent
18+
from cloudevents.core.base import BaseCloudEvent, EventFactory
1919
from cloudevents.core.bindings.common import (
2020
CONTENT_TYPE_HEADER,
2121
DATACONTENTTYPE_ATTR,
@@ -125,9 +125,7 @@ def to_binary(
125125
def from_binary(
126126
message: KafkaMessage,
127127
event_format: Format,
128-
event_factory: Callable[
129-
[dict[str, Any], dict[str, Any] | str | bytes | None], BaseCloudEvent
130-
],
128+
event_factory: EventFactory,
131129
) -> BaseCloudEvent:
132130
"""
133131
Parse a Kafka binary content mode message to a CloudEvent.
@@ -228,9 +226,7 @@ def to_structured(
228226
def from_structured(
229227
message: KafkaMessage,
230228
event_format: Format,
231-
event_factory: Callable[
232-
[dict[str, Any], dict[str, Any] | str | bytes | None], BaseCloudEvent
233-
],
229+
event_factory: EventFactory,
234230
) -> BaseCloudEvent:
235231
"""
236232
Parse a Kafka structured content mode message to a CloudEvent.
@@ -276,9 +272,7 @@ def from_structured(
276272
def from_kafka(
277273
message: KafkaMessage,
278274
event_format: Format,
279-
event_factory: Callable[
280-
[dict[str, Any], dict[str, Any] | str | bytes | None], BaseCloudEvent
281-
],
275+
event_factory: EventFactory,
282276
) -> BaseCloudEvent:
283277
"""
284278
Parse a Kafka message to a CloudEvent with automatic mode detection.

src/cloudevents/core/formats/base.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from typing import Any, Callable, Protocol
15+
from typing import Any, Protocol
1616

17-
from cloudevents.core.base import BaseCloudEvent
17+
from cloudevents.core.base import BaseCloudEvent, EventFactory
1818

1919

2020
class Format(Protocol):
@@ -29,10 +29,7 @@ class Format(Protocol):
2929

3030
def read(
3131
self,
32-
event_factory: Callable[
33-
[dict[str, Any], dict[str, Any] | str | bytes | None],
34-
BaseCloudEvent,
35-
],
32+
event_factory: EventFactory,
3633
data: str | bytes,
3734
) -> BaseCloudEvent:
3835
"""

src/cloudevents/core/formats/json.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
import re
1717
from datetime import datetime
1818
from json import JSONEncoder, dumps, loads
19-
from typing import Any, Callable, Final, Pattern
19+
from typing import Any, Final, Pattern
2020

2121
from dateutil.parser import isoparse
2222

23-
from cloudevents.core.base import BaseCloudEvent
23+
from cloudevents.core.base import BaseCloudEvent, EventFactory
2424
from cloudevents.core.formats.base import Format
2525

2626

@@ -49,10 +49,7 @@ class JSONFormat(Format):
4949

5050
def read(
5151
self,
52-
event_factory: Callable[
53-
[dict[str, Any], dict[str, Any] | str | bytes | None],
54-
BaseCloudEvent,
55-
],
52+
event_factory: EventFactory,
5653
data: str | bytes,
5754
) -> BaseCloudEvent:
5855
"""

0 commit comments

Comments
 (0)