Skip to content

Commit e9b6301

Browse files
committed
chore: improve typing of functions returning AnyCloudEvent
kafka.conversion.from_binary() and from_structured() return AnyCloudEvent type var according to their event_type argument, but when event_type is None, type checkers cannot infer the return type. We now use an overload to declare that the return type is http.CloudEvent when event_type is None. Previously users had to explicitly annotate this type when calling without event_type. This happens quite a lot in this repo's test_kafka_conversions.py — this fixes quite a few type errors like: > error: Need type annotation for "result" [var-annotated] Signed-off-by: Hal Blackburn <[email protected]>
1 parent c5645d8 commit e9b6301

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

cloudevents/kafka/conversion.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,29 @@ def to_binary(
111111
return KafkaMessage(headers, message_key, data)
112112

113113

114+
@typing.overload
114115
def from_binary(
115116
message: KafkaMessage,
116-
event_type: typing.Optional[typing.Type[AnyCloudEvent]] = 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],
117127
data_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
118128
) -> 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]:
119137
"""
120138
Returns a CloudEvent from a KafkaMessage in binary format.
121139
@@ -144,10 +162,11 @@ def from_binary(
144162
raise cloud_exceptions.DataUnmarshallerError(
145163
f"Failed to unmarshall data with error: {type(e).__name__}('{e}')"
146164
)
165+
result: typing.Union[http.CloudEvent, AnyCloudEvent]
147166
if event_type:
148167
result = event_type.create(attributes, data)
149168
else:
150-
result = http.CloudEvent.create(attributes, data) # type: ignore
169+
result = http.CloudEvent.create(attributes, data)
151170
return result
152171

153172

@@ -210,12 +229,32 @@ def to_structured(
210229
return KafkaMessage(headers, message_key, value)
211230

212231

232+
@typing.overload
213233
def from_structured(
214234
message: KafkaMessage,
215-
event_type: typing.Optional[typing.Type[AnyCloudEvent]] = 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],
216246
data_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
217247
envelope_unmarshaller: typing.Optional[types.UnmarshallerType] = None,
218248
) -> 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]:
219258
"""
220259
Returns a CloudEvent from a KafkaMessage in structured format.
221260
@@ -264,8 +303,9 @@ def from_structured(
264303
attributes["datacontenttype"] = val.decode()
265304
else:
266305
attributes[header.lower()] = val.decode()
306+
result: typing.Union[AnyCloudEvent, http.CloudEvent]
267307
if event_type:
268308
result = event_type.create(attributes, data)
269309
else:
270-
result = http.CloudEvent.create(attributes, data) # type: ignore
310+
result = http.CloudEvent.create(attributes, data)
271311
return result

0 commit comments

Comments
 (0)