Skip to content

Commit f81c902

Browse files
committed
chore: fix type errors in tests
mypy was failing with lots of type errors in test modules. I've not annotated all fixtures, mostly fixed existing type errors. Signed-off-by: Hal Blackburn <[email protected]>
1 parent eacac4f commit f81c902

File tree

7 files changed

+132
-53
lines changed

7 files changed

+132
-53
lines changed

cloudevents/tests/test_converters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
def test_binary_converter_raise_unsupported():
2222
with pytest.raises(exceptions.UnsupportedEvent):
2323
cnvtr = binary.BinaryHTTPCloudEventConverter()
24-
cnvtr.read(None, {}, None, None)
24+
cnvtr.read(None, {}, None, None) # type: ignore[arg-type] # intentionally wrong type # noqa: E501
2525

2626

2727
def test_base_converters_raise_exceptions():
@@ -35,8 +35,8 @@ def test_base_converters_raise_exceptions():
3535

3636
with pytest.raises(Exception):
3737
cnvtr = base.Converter()
38-
cnvtr.write(None, None)
38+
cnvtr.write(None, None) # type: ignore[arg-type] # intentionally wrong type
3939

4040
with pytest.raises(Exception):
4141
cnvtr = base.Converter()
42-
cnvtr.read(None, None, None, None)
42+
cnvtr.read(None, None, None, None) # type: ignore[arg-type] # intentionally wrong type # noqa: E501

cloudevents/tests/test_event_from_request_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
@pytest.mark.parametrize("event_class", [v03.Event, v1.Event])
2626
def test_binary_converter_upstream(event_class):
2727
m = marshaller.NewHTTPMarshaller([binary.NewBinaryHTTPCloudEventConverter()])
28-
event = m.FromRequest(event_class(), data.headers[event_class], None, lambda x: x)
28+
event = m.FromRequest(event_class(), data.headers[event_class], b"", lambda x: x)
2929
assert event is not None
3030
assert event.EventType() == data.ce_type
3131
assert event.EventID() == data.ce_id

cloudevents/tests/test_event_pipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_object_event_v1():
7777
_, structured_body = m.ToRequest(event)
7878
assert isinstance(structured_body, bytes)
7979
structured_obj = json.loads(structured_body)
80-
error_msg = f"Body was {structured_body}, obj is {structured_obj}"
80+
error_msg = f"Body was {structured_body!r}, obj is {structured_obj}"
8181
assert isinstance(structured_obj, dict), error_msg
8282
assert isinstance(structured_obj["data"], dict), error_msg
8383
assert len(structured_obj["data"]) == 1, error_msg

cloudevents/tests/test_http_events.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
14+
from __future__ import annotations
1415

1516
import bz2
1617
import io
@@ -241,11 +242,11 @@ def test_structured_to_request(specversion):
241242
assert headers["content-type"] == "application/cloudevents+json"
242243
for key in attributes:
243244
assert body[key] == attributes[key]
244-
assert body["data"] == data, f"|{body_bytes}|| {body}"
245+
assert body["data"] == data, f"|{body_bytes!r}|| {body}"
245246

246247

247248
@pytest.mark.parametrize("specversion", ["1.0", "0.3"])
248-
def test_attributes_view_accessor(specversion: str):
249+
def test_attributes_view_accessor(specversion: str) -> None:
249250
attributes: dict[str, typing.Any] = {
250251
"specversion": specversion,
251252
"type": "word.found.name",
@@ -333,7 +334,7 @@ def test_valid_structured_events(specversion):
333334
events_queue = []
334335
num_cloudevents = 30
335336
for i in range(num_cloudevents):
336-
event = {
337+
raw_event = {
337338
"id": f"id{i}",
338339
"source": f"source{i}.com.test",
339340
"type": "cloudevent.test.type",
@@ -343,7 +344,7 @@ def test_valid_structured_events(specversion):
343344
events_queue.append(
344345
from_http(
345346
{"content-type": "application/cloudevents+json"},
346-
json.dumps(event),
347+
json.dumps(raw_event),
347348
)
348349
)
349350

@@ -454,7 +455,7 @@ def test_invalid_data_format_structured_from_http():
454455
headers = {"Content-Type": "application/cloudevents+json"}
455456
data = 20
456457
with pytest.raises(cloud_exceptions.InvalidStructuredJSON) as e:
457-
from_http(headers, data)
458+
from_http(headers, data) # type: ignore[arg-type] # intentionally wrong type
458459
assert "Expected json of type (str, bytes, bytearray)" in str(e.value)
459460

460461

@@ -526,7 +527,7 @@ def test_generic_exception():
526527
e.errisinstance(cloud_exceptions.MissingRequiredFields)
527528

528529
with pytest.raises(cloud_exceptions.GenericException) as e:
529-
from_http({}, 123)
530+
from_http({}, 123) # type: ignore[arg-type] # intentionally wrong type
530531
e.errisinstance(cloud_exceptions.InvalidStructuredJSON)
531532

532533
with pytest.raises(cloud_exceptions.GenericException) as e:

cloudevents/tests/test_kafka_conversions.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import pytest
2020

2121
from cloudevents import exceptions as cloud_exceptions
22+
from cloudevents.abstract.event import AnyCloudEvent
2223
from cloudevents.http import CloudEvent
2324
from cloudevents.kafka.conversion import (
2425
KafkaMessage,
@@ -36,7 +37,9 @@ def simple_serialize(data: dict) -> bytes:
3637

3738

3839
def simple_deserialize(data: bytes) -> dict:
39-
return json.loads(data.decode())
40+
value = json.loads(data.decode())
41+
assert isinstance(value, dict)
42+
return value
4043

4144

4245
def failing_func(*args):
@@ -47,7 +50,7 @@ class KafkaConversionTestBase:
4750
expected_data = {"name": "test", "amount": 1}
4851
expected_custom_mapped_key = "custom-key"
4952

50-
def custom_key_mapper(self, _) -> str:
53+
def custom_key_mapper(self, _: AnyCloudEvent) -> str:
5154
return self.expected_custom_mapped_key
5255

5356
@pytest.fixture

cloudevents/tests/test_marshaller.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ def test_from_request_wrong_unmarshaller():
5050
with pytest.raises(exceptions.InvalidDataUnmarshaller):
5151
m = marshaller.NewDefaultHTTPMarshaller()
5252
_ = m.FromRequest(
53-
event=v1.Event(), headers={}, body="", data_unmarshaller=object()
53+
event=v1.Event(), headers={}, body="", data_unmarshaller=object() # type: ignore[arg-type] # intentionally wrong type # noqa: E501
5454
)
5555

5656

5757
def test_to_request_wrong_marshaller():
5858
with pytest.raises(exceptions.InvalidDataMarshaller):
5959
m = marshaller.NewDefaultHTTPMarshaller()
60-
_ = m.ToRequest(v1.Event(), data_marshaller="")
60+
_ = m.ToRequest(v1.Event(), data_marshaller="") # type: ignore[arg-type] # intentionally wrong type # noqa: E501
6161

6262

6363
def test_from_request_cannot_read(binary_headers):

0 commit comments

Comments
 (0)