Skip to content

Commit fbcfba9

Browse files
committed
add type annotations
Signed-off-by: Tudor Plugaru <[email protected]>
1 parent a893fb3 commit fbcfba9

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

src/cloudevents/core/formats/base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919

2020

2121
class Format(Protocol):
22-
def read(self, event_factory: Callable[[dict, Optional[Union[dict, str, bytes]]], BaseCloudEvent], data: Union[str, bytes]) -> BaseCloudEvent: ...
22+
def read(
23+
self,
24+
event_factory: Callable[
25+
[dict, Optional[Union[dict, str, bytes]]], BaseCloudEvent
26+
],
27+
data: Union[str, bytes],
28+
) -> BaseCloudEvent: ...
2329

2430
def write(self, event: BaseCloudEvent) -> bytes: ...

src/cloudevents/core/formats/json.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,23 @@ class JSONFormat(Format):
4848
r"^(application|text)/([a-zA-Z0-9\-\.]+\+)?json(;.*)?$"
4949
)
5050

51-
def read(self, event_factory: Callable[[dict, Optional[Union[dict, str, bytes]]], BaseCloudEvent], data: Union[str, bytes]) -> BaseCloudEvent:
51+
def read(
52+
self,
53+
event_factory: Callable[
54+
[dict, Optional[Union[dict, str, bytes]]], BaseCloudEvent
55+
],
56+
data: Union[str, bytes],
57+
) -> BaseCloudEvent:
5258
"""
5359
Read a CloudEvent from a JSON formatted byte string.
5460
5561
:param event_factory: A factory function to create CloudEvent instances.
5662
:param data: The JSON formatted byte array.
5763
:return: The CloudEvent instance.
5864
"""
65+
decoded_data: str
5966
if isinstance(data, bytes):
60-
decoded_data: str = data.decode("utf-8")
67+
decoded_data = data.decode("utf-8")
6168
else:
6269
decoded_data = data
6370

tests/test_core/test_format/test_json.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,9 @@ def test_write_cloud_event_with_extension_attributes() -> None:
233233

234234

235235
def test_read_cloud_event_with_extension_attributes() -> None:
236-
data = '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "customext1": "value1", "customext2": 123}'.encode("utf-8")
236+
data = '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "customext1": "value1", "customext2": 123}'.encode(
237+
"utf-8"
238+
)
237239
formatter = JSONFormat()
238240
result = formatter.read(CloudEvent, data)
239241

@@ -247,7 +249,7 @@ def test_write_cloud_event_with_different_json_content_types() -> None:
247249
("text/json", {"key": "value"}),
248250
("application/json; charset=utf-8", {"key": "value"}),
249251
]
250-
252+
251253
for content_type, data in test_cases:
252254
attributes = {
253255
"id": "123",
@@ -259,12 +261,14 @@ def test_write_cloud_event_with_different_json_content_types() -> None:
259261
event = CloudEvent(attributes=attributes, data=data)
260262
formatter = JSONFormat()
261263
result = formatter.write(event)
262-
264+
263265
assert b'"data": {"key": "value"}' in result
264266

265267

266268
def test_read_cloud_event_with_string_data() -> None:
267-
data = '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "data": "plain string data"}'.encode("utf-8")
269+
data = '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "data": "plain string data"}'.encode(
270+
"utf-8"
271+
)
268272
formatter = JSONFormat()
269273
result = formatter.read(CloudEvent, data)
270274

@@ -296,14 +300,16 @@ def test_write_cloud_event_with_unicode_data() -> None:
296300
event = CloudEvent(attributes=attributes, data="Hello 世界 🌍")
297301
formatter = JSONFormat()
298302
result = formatter.write(event)
299-
303+
300304
decoded = result.decode("utf-8")
301305
assert '"data": "Hello' in decoded
302306
assert "Hello" in decoded
303307

304308

305309
def test_read_cloud_event_with_unicode_data() -> None:
306-
data = '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "data": "Hello 世界 🌍"}'.encode("utf-8")
310+
data = '{"id": "123", "source": "source", "type": "type", "specversion": "1.0", "data": "Hello 世界 🌍"}'.encode(
311+
"utf-8"
312+
)
307313
formatter = JSONFormat()
308314
result = formatter.read(CloudEvent, data)
309315

0 commit comments

Comments
 (0)