Skip to content

Commit f7b0518

Browse files
committed
chore: switch type hints to use | rather than Union
Signed-off-by: Tudor Plugaru <[email protected]>
1 parent fe79bf4 commit f7b0518

File tree

7 files changed

+39
-66
lines changed

7 files changed

+39
-66
lines changed

src/cloudevents/core/base.py

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

15-
1615
from datetime import datetime
17-
from typing import Any, Optional, Protocol, Union
16+
from typing import Any, Protocol
1817

1918

2019
class BaseCloudEvent(Protocol):
@@ -29,7 +28,7 @@ class BaseCloudEvent(Protocol):
2928
def __init__(
3029
self,
3130
attributes: dict[str, Any],
32-
data: Optional[Union[dict[str, Any], str, bytes]] = None,
31+
data: dict[str, Any] | str | bytes | None = None,
3332
) -> None:
3433
"""
3534
Create a new CloudEvent instance.
@@ -74,31 +73,31 @@ def get_specversion(self) -> str:
7473
"""
7574
...
7675

77-
def get_datacontenttype(self) -> Optional[str]:
76+
def get_datacontenttype(self) -> str | None:
7877
"""
7978
Retrieve the datacontenttype of the event.
8079
8180
:return: The datacontenttype of the event.
8281
"""
8382
...
8483

85-
def get_dataschema(self) -> Optional[str]:
84+
def get_dataschema(self) -> str | None:
8685
"""
8786
Retrieve the dataschema of the event.
8887
8988
:return: The dataschema of the event.
9089
"""
9190
...
9291

93-
def get_subject(self) -> Optional[str]:
92+
def get_subject(self) -> str | None:
9493
"""
9594
Retrieve the subject of the event.
9695
9796
:return: The subject of the event.
9897
"""
9998
...
10099

101-
def get_time(self) -> Optional[datetime]:
100+
def get_time(self) -> datetime | None:
102101
"""
103102
Retrieve the time of the event.
104103
@@ -115,7 +114,7 @@ def get_extension(self, extension_name: str) -> Any:
115114
"""
116115
...
117116

118-
def get_data(self) -> Optional[Union[dict[str, Any], str, bytes]]:
117+
def get_data(self) -> dict[str, Any] | str | bytes | None:
119118
"""
120119
Retrieve data of the event.
121120

src/cloudevents/core/bindings/http.py

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

1515
from dataclasses import dataclass
1616
from datetime import datetime
17-
from typing import Any, Callable, Final, Optional, Union
17+
from typing import Any, Callable, Final
1818
from urllib.parse import quote, unquote
1919

2020
from dateutil.parser import isoparse
@@ -140,7 +140,7 @@ def from_binary(
140140
message: HTTPMessage,
141141
event_format: Format,
142142
event_factory: Callable[
143-
[dict[str, Any], Optional[Union[dict[str, Any], str, bytes]]], BaseCloudEvent
143+
[dict[str, Any], dict[str, Any] | str | bytes | None], BaseCloudEvent
144144
],
145145
) -> BaseCloudEvent:
146146
"""
@@ -221,7 +221,7 @@ def from_structured(
221221
message: HTTPMessage,
222222
event_format: Format,
223223
event_factory: Callable[
224-
[dict[str, Any], Optional[Union[dict[str, Any], str, bytes]]], BaseCloudEvent
224+
[dict[str, Any], dict[str, Any] | str | bytes | None], BaseCloudEvent
225225
],
226226
) -> BaseCloudEvent:
227227
"""
@@ -252,7 +252,7 @@ def from_http(
252252
message: HTTPMessage,
253253
event_format: Format,
254254
event_factory: Callable[
255-
[dict[str, Any], Optional[Union[dict[str, Any], str, bytes]]], BaseCloudEvent
255+
[dict[str, Any], dict[str, Any] | str | bytes | None], BaseCloudEvent
256256
],
257257
) -> BaseCloudEvent:
258258
"""

src/cloudevents/core/formats/base.py

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

15-
16-
from typing import Any, Callable, Optional, Protocol, Union
15+
from typing import Any, Callable, Protocol
1716

1817
from cloudevents.core.base import BaseCloudEvent
1918

@@ -31,10 +30,10 @@ class Format(Protocol):
3130
def read(
3231
self,
3332
event_factory: Callable[
34-
[dict[str, Any], Optional[Union[dict[str, Any], str, bytes]]],
33+
[dict[str, Any], dict[str, Any] | str | bytes | None],
3534
BaseCloudEvent,
3635
],
37-
data: Union[str, bytes],
36+
data: str | bytes,
3837
) -> BaseCloudEvent:
3938
"""
4039
Deserialize a CloudEvent from its wire format representation.
@@ -60,8 +59,8 @@ def write(self, event: BaseCloudEvent) -> bytes:
6059

6160
def write_data(
6261
self,
63-
data: Optional[Union[dict[str, Any], str, bytes]],
64-
datacontenttype: Optional[str],
62+
data: dict[str, Any] | str | bytes | None,
63+
datacontenttype: str | None,
6564
) -> bytes:
6665
"""
6766
Serialize just the data payload for protocol bindings (e.g., HTTP binary mode).
@@ -73,8 +72,8 @@ def write_data(
7372
...
7473

7574
def read_data(
76-
self, body: bytes, datacontenttype: Optional[str]
77-
) -> Optional[Union[dict[str, Any], str, bytes]]:
75+
self, body: bytes, datacontenttype: str | None
76+
) -> dict[str, Any] | str | bytes | None:
7877
"""
7978
Deserialize data payload from protocol bindings (e.g., HTTP binary mode).
8079

src/cloudevents/core/formats/json.py

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

15-
1615
import base64
1716
import re
1817
from datetime import datetime
1918
from json import JSONEncoder, dumps, loads
20-
from typing import Any, Callable, Final, Optional, Pattern, Union
19+
from typing import Any, Callable, Final, Pattern
2120

2221
from dateutil.parser import isoparse
2322

@@ -51,10 +50,10 @@ class JSONFormat(Format):
5150
def read(
5251
self,
5352
event_factory: Callable[
54-
[dict[str, Any], Optional[Union[dict[str, Any], str, bytes]]],
53+
[dict[str, Any], dict[str, Any] | str | bytes | None],
5554
BaseCloudEvent,
5655
],
57-
data: Union[str, bytes],
56+
data: str | bytes,
5857
) -> BaseCloudEvent:
5958
"""
6059
Read a CloudEvent from a JSON formatted byte string.
@@ -74,7 +73,7 @@ def read(
7473
if "time" in event_attributes:
7574
event_attributes["time"] = isoparse(event_attributes["time"])
7675

77-
event_data: Union[dict[str, Any], str, bytes, None] = event_attributes.pop(
76+
event_data: dict[str, Any] | str | bytes | None = event_attributes.pop(
7877
"data", None
7978
)
8079
if event_data is None:
@@ -108,8 +107,8 @@ def write(self, event: BaseCloudEvent) -> bytes:
108107

109108
def write_data(
110109
self,
111-
data: Optional[Union[dict[str, Any], str, bytes]],
112-
datacontenttype: Optional[str],
110+
data: dict[str, Any] | str | bytes | None,
111+
datacontenttype: str | None,
113112
) -> bytes:
114113
"""
115114
Serialize just the data payload for HTTP binary mode.
@@ -143,8 +142,8 @@ def write_data(
143142
return str(data).encode("utf-8")
144143

145144
def read_data(
146-
self, body: bytes, datacontenttype: Optional[str]
147-
) -> Optional[Union[dict[str, Any], str, bytes]]:
145+
self, body: bytes, datacontenttype: str | None
146+
) -> dict[str, Any] | str | bytes | None:
148147
"""
149148
Deserialize data payload from HTTP binary mode body.
150149

src/cloudevents/core/v1/event.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import re
1616
from collections import defaultdict
1717
from datetime import datetime
18-
from typing import Any, Final, Optional, Union
18+
from typing import Any, Final
1919

2020
from cloudevents.core.base import BaseCloudEvent
2121
from cloudevents.core.v1.exceptions import (
@@ -40,11 +40,11 @@ class CloudEvent(BaseCloudEvent):
4040
def __init__(
4141
self,
4242
attributes: dict[str, Any],
43-
data: Optional[Union[dict[str, Any], str, bytes]] = None,
43+
data: dict[str, Any] | str | bytes | None = None,
4444
) -> None:
4545
self._validate_attribute(attributes=attributes)
4646
self._attributes: dict[str, Any] = attributes
47-
self._data: Optional[Union[dict[str, Any], str, bytes]] = data
47+
self._data: dict[str, Any] | str | bytes | None = data
4848

4949
@staticmethod
5050
def _validate_attribute(attributes: dict[str, Any]) -> None:
@@ -242,22 +242,22 @@ def get_type(self) -> str:
242242
def get_specversion(self) -> str:
243243
return self._attributes["specversion"] # type: ignore
244244

245-
def get_datacontenttype(self) -> Optional[str]:
245+
def get_datacontenttype(self) -> str | None:
246246
return self._attributes.get("datacontenttype")
247247

248-
def get_dataschema(self) -> Optional[str]:
248+
def get_dataschema(self) -> str | None:
249249
return self._attributes.get("dataschema")
250250

251-
def get_subject(self) -> Optional[str]:
251+
def get_subject(self) -> str | None:
252252
return self._attributes.get("subject")
253253

254-
def get_time(self) -> Optional[datetime]:
254+
def get_time(self) -> datetime | None:
255255
return self._attributes.get("time")
256256

257257
def get_extension(self, extension_name: str) -> Any:
258258
return self._attributes.get(extension_name)
259259

260-
def get_data(self) -> Optional[Union[dict[str, Any], str, bytes]]:
260+
def get_data(self) -> dict[str, Any] | str | bytes | None:
261261
return self._data
262262

263263
def get_attributes(self) -> dict[str, Any]:

tests/test_core/test_bindings/test_http.py

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

1515
from datetime import datetime, timezone
16-
from typing import Any, Optional, Union
16+
from typing import Any
1717

1818
import pytest
1919

@@ -41,8 +41,8 @@ def minimal_attributes() -> dict[str, str]:
4141

4242

4343
def create_event(
44-
extra_attrs: Optional[dict[str, Any]] = None,
45-
data: Optional[Union[dict[str, Any], str, bytes]] = None,
44+
extra_attrs: dict[str, Any] | None = None,
45+
data: dict[str, Any] | str | bytes | None = None,
4646
) -> CloudEvent:
4747
"""Helper to create CloudEvent with valid required attributes"""
4848
attrs: dict[str, Any] = {
@@ -908,7 +908,7 @@ def test_custom_event_factory() -> None:
908908
"""Test using custom event factory function"""
909909

910910
def custom_factory(
911-
attributes: dict[str, Any], data: Optional[Union[dict[str, Any], str, bytes]]
911+
attributes: dict[str, Any], data: dict[str, Any] | str | bytes | None
912912
) -> CloudEvent:
913913
# Custom factory that adds a prefix to the type
914914
attributes["type"] = f"custom.{attributes.get('type', 'unknown')}"

0 commit comments

Comments
 (0)