Skip to content

Commit c66e8e4

Browse files
committed
chore: merge latest master into current
Signed-off-by: Yurii Serhiichuk <[email protected]>
2 parents 75c0373 + a38933d commit c66e8e4

File tree

21 files changed

+176
-53
lines changed

21 files changed

+176
-53
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.12.0]
10+
11+
### Changed
12+
13+
- Dropped Python3.8 support while it has reached EOL. ([])
14+
15+
## [1.11.1]
16+
17+
### Fixed
18+
- Kafka `conversion` marshaller and unmarshaller typings ([#240])
19+
- Improved public API type annotations and fixed unit test type errors ([#248])
20+
921
## [1.11.0]
1022

1123
### Fixed
@@ -287,3 +299,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
287299
[#232]: https://github.com/cloudevents/sdk-python/pull/232
288300
[#235]: https://github.com/cloudevents/sdk-python/pull/235
289301
[#236]: https://github.com/cloudevents/sdk-python/pull/236
302+
[#240]: https://github.com/cloudevents/sdk-python/pull/240
303+
[#248]: https://github.com/cloudevents/sdk-python/pull/248

requirements/mypy.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mypy
2+
# mypy has the pydantic plugin enabled
3+
pydantic>=2.0.0,<3.0
4+
types-requests
5+
deprecation>=2.0,<3.0

samples/http-image-cloudevents/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
image_bytes = resp.content
2525

2626

27-
def send_binary_cloud_event(url: str):
27+
def send_binary_cloud_event(url: str) -> None:
2828
# Create cloudevent
2929
attributes = {
3030
"type": "com.example.string",
@@ -41,7 +41,7 @@ def send_binary_cloud_event(url: str):
4141
print(f"Sent {event['id']} of type {event['type']}")
4242

4343

44-
def send_structured_cloud_event(url: str):
44+
def send_structured_cloud_event(url: str) -> None:
4545
# Create cloudevent
4646
attributes = {
4747
"type": "com.example.base64",

src/cloudevents/v1/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
__version__ = "1.11.0"
15+
__version__ = "1.12.0"

src/cloudevents/v1/abstract/event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CloudEvent:
3232
@classmethod
3333
def create(
3434
cls: typing.Type[AnyCloudEvent],
35-
attributes: typing.Dict[str, typing.Any],
35+
attributes: typing.Mapping[str, typing.Any],
3636
data: typing.Optional[typing.Any],
3737
) -> AnyCloudEvent:
3838
"""

src/cloudevents/v1/conversion.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ def from_json(
9191

9292
def from_http(
9393
event_type: typing.Type[AnyCloudEvent],
94-
headers: typing.Mapping[str, str],
94+
headers: typing.Union[
95+
typing.Mapping[str, str], types.SupportsDuplicateItems[str, str]
96+
],
9597
data: typing.Optional[typing.Union[str, bytes]],
9698
data_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
9799
) -> AnyCloudEvent:
@@ -260,7 +262,7 @@ def best_effort_encode_attribute_value(value: typing.Any) -> typing.Any:
260262

261263
def from_dict(
262264
event_type: typing.Type[AnyCloudEvent],
263-
event: typing.Dict[str, typing.Any],
265+
event: typing.Mapping[str, typing.Any],
264266
) -> AnyCloudEvent:
265267
"""
266268
Constructs an Event object of a given `event_type` from

src/cloudevents/v1/http/conversion.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ def from_json(
3737

3838

3939
def from_http(
40-
headers: typing.Dict[str, str],
40+
headers: typing.Union[
41+
typing.Mapping[str, str], types.SupportsDuplicateItems[str, str]
42+
],
4143
data: typing.Optional[typing.Union[str, bytes]],
4244
data_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
4345
) -> CloudEvent:
@@ -58,7 +60,7 @@ def from_http(
5860

5961

6062
def from_dict(
61-
event: typing.Dict[str, typing.Any],
63+
event: typing.Mapping[str, typing.Any],
6264
) -> CloudEvent:
6365
"""
6466
Constructs a CloudEvent from a dict `event` representation.

src/cloudevents/v1/http/event.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ class CloudEvent(abstract.CloudEvent):
3434

3535
@classmethod
3636
def create(
37-
cls, attributes: typing.Dict[str, typing.Any], data: typing.Optional[typing.Any]
37+
cls,
38+
attributes: typing.Mapping[str, typing.Any],
39+
data: typing.Optional[typing.Any],
3840
) -> "CloudEvent":
3941
return cls(attributes, data)
4042

41-
def __init__(self, attributes: typing.Dict[str, str], data: typing.Any = None):
43+
def __init__(self, attributes: typing.Mapping[str, str], data: typing.Any = None):
4244
"""
4345
Event Constructor
4446
:param attributes: a dict with cloudevent attributes. Minimally

src/cloudevents/v1/kafka/conversion.py

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@
2121
from cloudevents_v1.kafka.exceptions import KeyMapperError
2222
from cloudevents_v1.sdk import types
2323

24-
DEFAULT_MARSHALLER: types.MarshallerType = json.dumps
25-
DEFAULT_UNMARSHALLER: types.MarshallerType = json.loads
26-
DEFAULT_EMBEDDED_DATA_MARSHALLER: types.MarshallerType = lambda x: x
24+
JSON_MARSHALLER: types.MarshallerType = json.dumps
25+
JSON_UNMARSHALLER: types.UnmarshallerType = json.loads
26+
IDENTITY_MARSHALLER = IDENTITY_UNMARSHALLER = lambda x: x
27+
28+
DEFAULT_MARSHALLER: types.MarshallerType = JSON_MARSHALLER
29+
DEFAULT_UNMARSHALLER: types.UnmarshallerType = JSON_UNMARSHALLER
30+
DEFAULT_EMBEDDED_DATA_MARSHALLER: types.MarshallerType = IDENTITY_MARSHALLER
31+
DEFAULT_EMBEDDED_DATA_UNMARSHALLER: types.UnmarshallerType = IDENTITY_UNMARSHALLER
2732

2833

2934
class KafkaMessage(typing.NamedTuple):
@@ -106,11 +111,29 @@ def to_binary(
106111
return KafkaMessage(headers, message_key, data)
107112

108113

114+
@typing.overload
109115
def from_binary(
110116
message: KafkaMessage,
111-
event_type: typing.Optional[typing.Type[AnyCloudEvent]] = None,
112-
data_unmarshaller: typing.Optional[types.MarshallerType] = None,
117+
event_type: None = None,
118+
data_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
119+
) -> http.CloudEvent:
120+
pass
121+
122+
123+
@typing.overload
124+
def from_binary(
125+
message: KafkaMessage,
126+
event_type: typing.Type[AnyCloudEvent],
127+
data_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
113128
) -> AnyCloudEvent:
129+
pass
130+
131+
132+
def from_binary(
133+
message: KafkaMessage,
134+
event_type: typing.Optional[typing.Type[AnyCloudEvent]] = None,
135+
data_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
136+
) -> typing.Union[http.CloudEvent, AnyCloudEvent]:
114137
"""
115138
Returns a CloudEvent from a KafkaMessage in binary format.
116139
@@ -139,10 +162,11 @@ def from_binary(
139162
raise cloud_exceptions.DataUnmarshallerError(
140163
f"Failed to unmarshall data with error: {type(e).__name__}('{e}')"
141164
)
165+
result: typing.Union[http.CloudEvent, AnyCloudEvent]
142166
if event_type:
143167
result = event_type.create(attributes, data)
144168
else:
145-
result = http.CloudEvent.create(attributes, data) # type: ignore
169+
result = http.CloudEvent.create(attributes, data)
146170
return result
147171

148172

@@ -205,12 +229,32 @@ def to_structured(
205229
return KafkaMessage(headers, message_key, value)
206230

207231

232+
@typing.overload
208233
def from_structured(
209234
message: KafkaMessage,
210-
event_type: typing.Optional[typing.Type[AnyCloudEvent]] = None,
211-
data_unmarshaller: typing.Optional[types.MarshallerType] = None,
235+
event_type: None = None,
236+
data_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
237+
envelope_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
238+
) -> http.CloudEvent:
239+
pass
240+
241+
242+
@typing.overload
243+
def from_structured(
244+
message: KafkaMessage,
245+
event_type: typing.Type[AnyCloudEvent],
246+
data_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
212247
envelope_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
213248
) -> AnyCloudEvent:
249+
pass
250+
251+
252+
def from_structured(
253+
message: KafkaMessage,
254+
event_type: typing.Optional[typing.Type[AnyCloudEvent]] = None,
255+
data_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
256+
envelope_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
257+
) -> typing.Union[http.CloudEvent, AnyCloudEvent]:
214258
"""
215259
Returns a CloudEvent from a KafkaMessage in structured format.
216260
@@ -222,7 +266,7 @@ def from_structured(
222266
:returns: CloudEvent
223267
"""
224268

225-
data_unmarshaller = data_unmarshaller or DEFAULT_EMBEDDED_DATA_MARSHALLER
269+
data_unmarshaller = data_unmarshaller or DEFAULT_EMBEDDED_DATA_UNMARSHALLER
226270
envelope_unmarshaller = envelope_unmarshaller or DEFAULT_UNMARSHALLER
227271
try:
228272
structure = envelope_unmarshaller(message.value)
@@ -259,8 +303,9 @@ def from_structured(
259303
attributes["datacontenttype"] = val.decode()
260304
else:
261305
attributes[header.lower()] = val.decode()
306+
result: typing.Union[AnyCloudEvent, http.CloudEvent]
262307
if event_type:
263308
result = event_type.create(attributes, data)
264309
else:
265-
result = http.CloudEvent.create(attributes, data) # type: ignore
310+
result = http.CloudEvent.create(attributes, data)
266311
return result

src/cloudevents/v1/pydantic/v1/conversion.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222

2323
def from_http(
24-
headers: typing.Dict[str, str],
24+
headers: typing.Union[
25+
typing.Mapping[str, str], types.SupportsDuplicateItems[str, str]
26+
],
2527
data: typing.Optional[typing.AnyStr],
2628
data_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
2729
) -> CloudEvent:
@@ -63,7 +65,7 @@ def from_json(
6365

6466

6567
def from_dict(
66-
event: typing.Dict[str, typing.Any],
68+
event: typing.Mapping[str, typing.Any],
6769
) -> CloudEvent:
6870
"""
6971
Construct an CloudEvent from a dict `event` representation.

0 commit comments

Comments
 (0)