chore: cleanup codebase and fix flake errors (#188)

* deps: `flake8-strict` and  `flake8-import-order` are not compatible with Black and modern Python anymore

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: Cleanup imports and remove obsolete `#noqa`.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: sort imports.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: Define `__all__`

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: Fix licenses and add __all__ to imports.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: Fix formatting

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: Export `from_http`

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* fix: Do not export functions of other modules from this one.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: Resolve more flake8 errors

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* chore: Fix more warnings

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* docs: add a note in the changelog about the fixes.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* fix: imports in tests.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: more import fixes.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* fix: use proper implementations as replacements.

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Yurii Serhiichuk 2022-08-15 01:47:38 +03:00 committed by GitHub
parent f5bb285d96
commit 8a88ffee10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 106 additions and 120 deletions

View File

@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
moved under `cloudevents.http.conversion`.
- Deprecated `cloudevents.http.util` module.
### Fixed
- Multiple PEP issues, license headers, module-level exports. ([#188])
## [1.5.0] — 2022-08-06
@ -199,3 +201,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#182]: https://github.com/cloudevents/sdk-python/pull/182
[#184]: https://github.com/cloudevents/sdk-python/pull/184
[#186]: https://github.com/cloudevents/sdk-python/pull/186
[#188]: https://github.com/cloudevents/sdk-python/pull/188

View File

@ -11,29 +11,5 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
__version__ = "1.5.0"

View File

@ -12,4 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
from cloudevents.abstract.event import AnyCloudEvent, CloudEvent # noqa
from cloudevents.abstract.event import AnyCloudEvent, CloudEvent
__all__ = [AnyCloudEvent, CloudEvent]

View File

@ -14,7 +14,6 @@
import typing
from abc import abstractmethod
from typing import TypeVar
class CloudEvent:
@ -134,4 +133,4 @@ class CloudEvent:
return str({"attributes": self._get_attributes(), "data": self._get_data()})
AnyCloudEvent = TypeVar("AnyCloudEvent", bound=CloudEvent)
AnyCloudEvent = typing.TypeVar("AnyCloudEvent", bound=CloudEvent)

View File

@ -11,18 +11,6 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import enum
import json
@ -42,10 +30,10 @@ def _best_effort_serialize_to_json(
Serializes the given value into a JSON-encoded string.
Given a None value returns None as is.
Given a non-JSON-serializable value returns return the value as is.
Given a non-JSON-serializable value returns the value as is.
:param value: The value to be serialized into a JSON string.
:return: JSON string of the given value OR None OR given value.
:returns: JSON string of the given value OR None OR given value.
"""
if value is None:
return None

View File

@ -13,17 +13,25 @@
# under the License.
from cloudevents.http.conversion import ( # noqa
from_dict,
from_http,
from_json,
from cloudevents.http.conversion import from_dict, from_http, from_json
from cloudevents.http.event import CloudEvent
from cloudevents.http.event_type import is_binary, is_structured # deprecated
from cloudevents.http.http_methods import ( # deprecated
to_binary,
to_dict,
to_json,
to_binary_http,
to_structured,
to_structured_http,
)
from cloudevents.http.event import CloudEvent # noqa
from cloudevents.http.http_methods import to_binary_http # deprecated # noqa
from cloudevents.http.http_methods import to_structured_http # deprecated # noqa
from cloudevents.sdk.converters.binary import is_binary # noqa
from cloudevents.sdk.converters.structured import is_structured # noqa
__all__ = [
to_binary,
to_structured,
from_json,
from_http,
from_dict,
CloudEvent,
is_binary,
is_structured,
to_binary_http,
to_structured_http,
]

View File

@ -1,9 +1,22 @@
# Copyright 2018-Present The CloudEvents Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import typing
from cloudevents.conversion import from_dict as _abstract_from_dict
from cloudevents.conversion import from_http as _abstract_from_http
from cloudevents.conversion import from_json as _abstract_from_json
from cloudevents.conversion import to_binary, to_dict, to_json, to_structured # noqa
from cloudevents.http.event import CloudEvent
from cloudevents.sdk import types

View File

@ -17,9 +17,9 @@ import typing
from deprecation import deprecated
from cloudevents.abstract import AnyCloudEvent
from cloudevents.conversion import to_binary as _moved_to_binary
from cloudevents.conversion import to_structured as _moved_to_structured
from cloudevents.http.conversion import from_http as _moved_from_http
from cloudevents.http.conversion import to_binary as _moved_to_binary
from cloudevents.http.conversion import to_structured as _moved_to_structured
from cloudevents.http.event import CloudEvent
from cloudevents.sdk import types
@ -28,7 +28,7 @@ from cloudevents.sdk import types
@deprecated(
deprecated_in="1.6.0",
details="Use cloudevents.http.to_binary function instead",
details="Use cloudevents.conversion.to_binary function instead",
)
def to_binary(
event: AnyCloudEvent, data_marshaller: types.MarshallerType = None
@ -38,7 +38,7 @@ def to_binary(
@deprecated(
deprecated_in="1.6.0",
details="Use cloudevents.http.to_structured function instead",
details="Use cloudevents.conversion.to_structured function instead",
)
def to_structured(
event: AnyCloudEvent,

View File

@ -17,9 +17,9 @@ import typing
from deprecation import deprecated
from cloudevents.abstract import AnyCloudEvent
from cloudevents.conversion import to_json as _moved_to_json
from cloudevents.http import CloudEvent
from cloudevents.http.conversion import from_json as _moved_from_json
from cloudevents.http.conversion import to_json as _moved_to_json
from cloudevents.sdk import types
# THIS MODULE IS DEPRECATED, YOU SHOULD NOT ADD NEW FUNCTIONALLY HERE
@ -27,7 +27,7 @@ from cloudevents.sdk import types
@deprecated(
deprecated_in="1.6.0",
details="Use cloudevents.http.to_json function instead",
details="Use cloudevents.conversion.to_json function instead",
)
def to_json(
event: AnyCloudEvent,

View File

@ -13,7 +13,7 @@
# under the License.
from deprecation import deprecated
from cloudevents.conversion import ( # noqa
from cloudevents.conversion import (
_best_effort_serialize_to_json as _moved_default_marshaller,
)

View File

@ -11,13 +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 cloudevents.pydantic.conversion import ( # noqa
from_dict,
from_http,
from_json,
to_binary,
to_dict,
to_json,
to_structured,
)
from cloudevents.pydantic.event import CloudEvent # noqa
from cloudevents.pydantic.conversion import from_dict, from_http, from_json
from cloudevents.pydantic.event import CloudEvent
__all__ = [CloudEvent, from_json, from_dict, from_http]

View File

@ -16,7 +16,6 @@ import typing
from cloudevents.conversion import from_dict as _abstract_from_dict
from cloudevents.conversion import from_http as _abstract_from_http
from cloudevents.conversion import from_json as _abstract_from_json
from cloudevents.conversion import to_binary, to_dict, to_json, to_structured # noqa
from cloudevents.pydantic.event import CloudEvent
from cloudevents.sdk import types

View File

@ -25,7 +25,6 @@ except ImportError: # pragma: no cover # hard to test
"Install it using pip install cloudevents[pydantic]"
)
import cloudevents.conversion
from cloudevents import abstract, conversion, http
from cloudevents.exceptions import IncompatibleArgumentsError
from cloudevents.sdk.event import attribute
@ -74,7 +73,7 @@ def _ce_json_loads(
"""
# Using HTTP from dict due to performance issues.
# Pydantic is known for initialization time lagging.
return cloudevents.conversion.to_dict(http.from_json(data))
return conversion.to_dict(http.from_json(data))
class CloudEvent(abstract.CloudEvent, pydantic.BaseModel):

View File

@ -13,8 +13,10 @@
# under the License.
from cloudevents.sdk.converters import binary, structured
from cloudevents.sdk.converters.binary import is_binary # noqa
from cloudevents.sdk.converters.structured import is_structured # noqa
from cloudevents.sdk.converters.binary import is_binary
from cloudevents.sdk.converters.structured import is_structured
TypeBinary = binary.BinaryHTTPCloudEventConverter.TYPE
TypeStructured = structured.JSONHTTPCloudEventConverter.TYPE
__all__ = [binary, structured, is_binary, is_structured, TypeBinary, TypeStructured]

View File

@ -16,6 +16,11 @@ import typing
def has_binary_headers(headers: typing.Dict[str, str]) -> bool:
"""Determines if all CloudEvents required headers are presents
in the `headers`.
:returns: True if all the headers are present, False otherwise.
"""
return (
"ce-specversion" in headers
and "ce-source" in headers

View File

@ -14,11 +14,11 @@
from cloudevents.sdk.event import v1, v03
contentType = "application/json"
content_type = "application/json"
ce_type = "word.found.exclamation"
ce_id = "16fb5f0b-211e-1102-3dfe-ea6e2806f124"
source = "pytest"
eventTime = "2018-10-23T12:28:23.3464579Z"
event_time = "2018-10-23T12:28:23.3464579Z"
body = '{"name":"john"}'
headers = {
@ -26,17 +26,17 @@ headers = {
"ce-specversion": "1.0",
"ce-type": ce_type,
"ce-id": ce_id,
"ce-time": eventTime,
"ce-time": event_time,
"ce-source": source,
"Content-Type": contentType,
"Content-Type": content_type,
},
v1.Event: {
"ce-specversion": "1.0",
"ce-type": ce_type,
"ce-id": ce_id,
"ce-time": eventTime,
"ce-time": event_time,
"ce-source": source,
"Content-Type": contentType,
"Content-Type": content_type,
},
}
@ -45,16 +45,16 @@ json_ce = {
"specversion": "1.0",
"type": ce_type,
"id": ce_id,
"time": eventTime,
"time": event_time,
"source": source,
"datacontenttype": contentType,
"datacontenttype": content_type,
},
v1.Event: {
"specversion": "1.0",
"type": ce_type,
"id": ce_id,
"time": eventTime,
"time": event_time,
"source": source,
"datacontenttype": contentType,
"datacontenttype": content_type,
},
}

View File

@ -23,7 +23,8 @@ def dummy_event():
def test_json_methods(dummy_event):
from cloudevents.http import from_json, to_json
from cloudevents.conversion import to_json
from cloudevents.http.conversion import from_json
from cloudevents.http.json_methods import from_json as deprecated_from_json
from cloudevents.http.json_methods import to_json as deprecated_to_json

View File

@ -40,7 +40,7 @@ def test_general_binary_properties(event_class):
assert event is not None
assert event.type == data.ce_type
assert event.id == data.ce_id
assert event.content_type == data.contentType
assert event.content_type == data.content_type
assert event.source == data.source
# Test setters
@ -80,7 +80,7 @@ def test_general_structured_properties(event_class):
assert event is not None
assert event.type == data.ce_type
assert event.id == data.ce_id
assert event.content_type == data.contentType
assert event.content_type == data.content_type
assert event.source == data.source
new_headers, _ = m.ToRequest(event, converters.TypeStructured, lambda x: x)

View File

@ -29,7 +29,7 @@ def test_binary_converter_upstream(event_class):
assert event is not None
assert event.EventType() == data.ce_type
assert event.EventID() == data.ce_id
assert event.ContentType() == data.contentType
assert event.ContentType() == data.content_type
@pytest.mark.parametrize("event_class", [v03.Event, v1.Event])
@ -45,7 +45,7 @@ def test_structured_converter_upstream(event_class):
assert event is not None
assert event.EventType() == data.ce_type
assert event.EventID() == data.ce_id
assert event.ContentType() == data.contentType
assert event.ContentType() == data.content_type
@pytest.mark.parametrize("event_class", [v03.Event, v1.Event])
@ -61,7 +61,7 @@ def test_default_http_marshaller_with_structured(event_class):
assert event is not None
assert event.EventType() == data.ce_type
assert event.EventID() == data.ce_id
assert event.ContentType() == data.contentType
assert event.ContentType() == data.content_type
@pytest.mark.parametrize("event_class", [v03.Event, v1.Event])
@ -77,5 +77,5 @@ def test_default_http_marshaller_with_binary(event_class):
assert event is not None
assert event.EventType() == data.ce_type
assert event.EventID() == data.ce_id
assert event.ContentType() == data.contentType
assert event.ContentType() == data.content_type
assert event.Data() == data.body

View File

@ -26,11 +26,11 @@ from cloudevents.tests import data
def test_event_pipeline_upstream(event_class):
event = (
event_class()
.SetContentType(data.contentType)
.SetContentType(data.content_type)
.SetData(data.body)
.SetEventID(data.ce_id)
.SetSource(data.source)
.SetEventTime(data.eventTime)
.SetEventTime(data.event_time)
.SetEventType(data.ce_type)
)
m = marshaller.NewDefaultHTTPMarshaller()
@ -74,17 +74,17 @@ def test_object_event_v1():
m = marshaller.NewDefaultHTTPMarshaller()
_, structuredBody = m.ToRequest(event)
assert isinstance(structuredBody, bytes)
structuredObj = json.loads(structuredBody)
errorMsg = f"Body was {structuredBody}, obj is {structuredObj}"
assert isinstance(structuredObj, dict), errorMsg
assert isinstance(structuredObj["data"], dict), errorMsg
assert len(structuredObj["data"]) == 1, errorMsg
assert structuredObj["data"]["name"] == "john", errorMsg
_, 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}"
assert isinstance(structured_obj, dict), error_msg
assert isinstance(structured_obj["data"], dict), error_msg
assert len(structured_obj["data"]) == 1, error_msg
assert structured_obj["data"]["name"] == "john", error_msg
headers, binaryBody = m.ToRequest(event, converters.TypeBinary)
headers, binary_body = m.ToRequest(event, converters.TypeBinary)
assert isinstance(headers, dict)
assert isinstance(binaryBody, bytes)
assert isinstance(binary_body, bytes)
assert headers["content-type"] == "application/json"
assert binaryBody == b'{"name": "john"}', f"Binary is {binaryBody!r}"
assert binary_body == b'{"name": "john"}', f"Binary is {binary_body!r}"

View File

@ -33,7 +33,7 @@ def test_binary_event_to_request_upstream(event_class):
assert event is not None
assert event.EventType() == data.ce_type
assert event.EventID() == data.ce_id
assert event.ContentType() == data.contentType
assert event.ContentType() == data.content_type
new_headers, _ = m.ToRequest(event, converters.TypeBinary, lambda x: x)
assert new_headers is not None
@ -50,7 +50,7 @@ def test_structured_event_to_request_upstream(event_class):
assert event is not None
assert event.EventType() == data.ce_type
assert event.EventID() == data.ce_id
assert event.ContentType() == data.contentType
assert event.ContentType() == data.content_type
new_headers, _ = m.ToRequest(event, converters.TypeStructured, lambda x: x)
for key in new_headers:

View File

@ -18,7 +18,8 @@ import json
import pytest
from cloudevents.http import CloudEvent, from_dict, from_json, to_dict, to_json
from cloudevents.conversion import to_dict, to_json
from cloudevents.http import CloudEvent, from_dict, from_json
from cloudevents.sdk.event.attribute import SpecVersion
test_data = json.dumps({"data-key": "val"})

View File

@ -398,10 +398,10 @@ def test_cloudevent_repr(specversion):
"ce-source": "<source-url>",
}
event = from_http(headers, "")
# Testing to make sure event is printable. I could runevent. __repr__() but
# Testing to make sure event is printable. I could run event. __repr__() but
# we had issues in the past where event.__repr__() could run but
# print(event) would fail.
print(event)
print(event) # noqa T201
@pytest.mark.parametrize("specversion", ["1.0", "0.3"])

View File

@ -18,7 +18,8 @@ import json
import pytest
from cloudevents.pydantic import CloudEvent, from_dict, from_json, to_json
from cloudevents.conversion import to_json
from cloudevents.pydantic import CloudEvent, from_dict, from_json
from cloudevents.sdk.event.attribute import SpecVersion
test_data = json.dumps({"data-key": "val"})

View File

@ -20,7 +20,8 @@ import pytest
from sanic import Sanic, response
import cloudevents.exceptions as cloud_exceptions
from cloudevents.pydantic import CloudEvent, from_http, to_binary, to_structured
from cloudevents.conversion import to_binary, to_structured
from cloudevents.pydantic import CloudEvent, from_http
from cloudevents.sdk import converters
from cloudevents.sdk.converters.binary import is_binary
from cloudevents.sdk.converters.structured import is_structured
@ -81,7 +82,6 @@ async def echo(request):
@pytest.mark.parametrize("body", invalid_cloudevent_request_body)
def test_missing_required_fields_structured(body):
with pytest.raises(cloud_exceptions.MissingRequiredFields):
_ = from_http(
{"Content-Type": "application/cloudevents+json"}, json.dumps(body)
)
@ -186,7 +186,6 @@ def test_missing_ce_prefix_binary_event(specversion):
"ce-specversion": specversion,
}
for key in headers:
# breaking prefix e.g. e-id instead of ce-id
prefixed_headers[key[1:]] = headers[key]
@ -383,10 +382,10 @@ def test_cloudevent_repr(specversion):
"ce-source": "<source-url>",
}
event = from_http(headers, "")
# Testing to make sure event is printable. I could runevent. __repr__() but
# Testing to make sure event is printable. I could run event. __repr__() but
# we had issues in the past where event.__repr__() could run but
# print(event) would fail.
print(event)
print(event) # noqa T201
@pytest.mark.parametrize("specversion", ["1.0", "0.3"])

View File

@ -2,8 +2,6 @@ black
isort
flake8
pep8-naming
flake8-import-order
flake8-print
flake8-strict
tox
pre-commit

View File

@ -1,8 +1,6 @@
flake8
pep8-naming
flake8-import-order
flake8-print
flake8-strict
pytest
pytest-cov
# web app tests