10000 Improve public API type annotations & fix unit test type errors by h4l · Pull Request #248 · cloudevents/sdk-python · GitHub
[go: up one dir, main page]

Skip to content

Improve public API type annotations & fix unit test type errors #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 23, 2025
Merged
Prev Previous commit
Next Next commit
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 <hwtb2@cam.ac.uk>
  • Loading branch information
h4l committed Mar 25, 2025
commit f81c902843f8959a4d4e7a24d79c678409cfe386
6 changes: 3 additions & 3 deletions cloudevents/tests/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
def test_binary_converter_raise_unsupported():
with pytest.raises(exceptions.UnsupportedEvent):
cnvtr = binary.BinaryHTTPCloudEventConverter()
cnvtr.read(None, {}, None, None)
cnvtr.read(None, {}, None, None) # type: ignore[arg-type] # intentionally wrong type # noqa: E501


def test_base_converters_raise_exceptions():
Expand All @@ -35,8 +35,8 @@ def test_base_converters_raise_exceptions():

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

with pytest.raises(Exception):
cnvtr = base.Converter()
cnvtr.read(None, None, None, None)
cnvtr.read(None, None, None, None) # type: ignore[arg-type] # intentionally wrong type # noqa: E501
2 changes: 1 addition & 1 deletion cloudevents/tests/test_event_from_request_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@pytest.mark.parametrize("event_class", [v03.Event, v1.Event])
def test_binary_converter_upstream(event_class):
m = marshaller.NewHTTPMarshaller([binary.NewBinaryHTTPCloudEventConverter()])
event = m.FromRequest(event_class(), data.headers[event_class], None, lambda x: x)
event = m.FromRequest(event_class(), data.headers[event_class], b"", lambda x: x)
assert event is not None
assert event.EventType() == data.ce_type
assert event.EventID() == data.ce_id
Expand Down
2 changes: 1 addition & 1 deletion cloudevents/tests/test_event_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_object_event_v1():
_, structured_body = m.ToRequest(event)
assert isinstance(structured_body, bytes)
structured_obj = json.loads(structured_body)
error_msg = f"Body was {structured_body}, obj is {structured_obj}"
error_msg = f"Body was {structured_body!r}, obj is {structured_obj}"
assert isinstance(structured_obj, dict), error_msg
assert isinstance(structured_obj["data"], dict), error_msg
assert len(structured_obj["data"]) == 1, error_msg
Expand Down
13 changes: 7 additions & 6 deletions cloudevents/tests/test_http_events.py
EDBE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import annotations

import bz2
import io
Expand Down Expand Up @@ -241,11 +242,11 @@ def test_structured_to_request(specversion):
assert headers["content-type"] == "application/cloudevents+json"
for key in attributes:
assert body[key] == attributes[key]
assert body["data"] == data, f"|{body_bytes}|| {body}"
assert body["data"] == data, f"|{body_bytes!r}|| {body}"


@pytest.mark.parametrize("specversion", ["1.0", "0.3"])
def test_attributes_view_accessor(specversion: str):
def test_attributes_view_accessor(specversion: str) -> None:
attributes: dict[str, typing.Any] = {
"specversion": specversion,
"type": "word.found.name",
Expand Down Expand Up @@ -333,7 +334,7 @@ def test_valid_structured_events(specversion):
events_queue = []
num_cloudevents = 30
for i in range(num_cloudevents):
event = {
raw_event = {
"id": f"id{i}",
"source": f"source{i}.com.test",
"type": "cloudevent.test.type",
Expand All @@ -343,7 +344,7 @@ def test_valid_structured_events(specversion):
events_queue.append(
from_http(
{"content-type": "application/cloudevents+json"},
json.dumps(event),
json.dumps(raw_event),
)
)

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


Expand Down Expand Up @@ -526,7 +527,7 @@ def test_generic_exception():
e.errisinstance(cloud_exceptions.MissingRequiredFields)

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

with pytest.raises(cloud_exceptions.GenericException) as e:
Expand Down
7 changes: 5 additions & 2 deletions cloudevents/tests/test_kafka_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import pytest

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


def simple_deserialize(data: bytes) -> dict:
return json.loads(data.decode())
value = json.loads(data.decode())
assert isinstance(value, dict)
return value


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

def custom_key_mapper(self, _) -> str:
def custom_key_mapper(self, _: AnyCloudEvent) -> str:
return self.expected_custom_mapped_key

@pytest.fixture
Expand Down
4 changes: 2 additions & 2 deletions cloudevents/tests/test_marshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ def test_from_request_wrong_unmarshaller():
with pytest.raises(exceptions.InvalidDataUnmarshaller):
m = marshaller.NewDefaultHTTPMarshaller()
_ = m.FromRequest(
event=v1.Event(), headers={}, body="", data_unmarshaller=object()
event=v1.Event(), headers={}, body="", data_unmarshaller=object() # type: ignore[arg-type] # intentionally wrong type # noqa: E501
)


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


def test_from_request_cannot_read(binary_headers):
Expand Down
Loading
0