Skip to content

Commit

Permalink
chore: cleanup codebase and fix flake errors (#188)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
xSAVIKx and pre-commit-ci[bot] authored Aug 14, 2022
1 parent f5bb285 commit 8a88ffe
Show file tree
Hide file tree
Showing 27 changed files with 106 additions and 120 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
24 changes: 0 additions & 24 deletions cloudevents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 3 additions & 1 deletion cloudevents/abstract/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
3 changes: 1 addition & 2 deletions cloudevents/abstract/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import typing
from abc import abstractmethod
from typing import TypeVar


class CloudEvent:
Expand Down Expand Up @@ -134,4 +133,4 @@ def __repr__(self) -> str:
return str({"attributes": self._get_attributes(), "data": self._get_data()})


AnyCloudEvent = TypeVar("AnyCloudEvent", bound=CloudEvent)
AnyCloudEvent = typing.TypeVar("AnyCloudEvent", bound=CloudEvent)
16 changes: 2 additions & 14 deletions cloudevents/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
30 changes: 19 additions & 11 deletions cloudevents/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
15 changes: 14 additions & 1 deletion cloudevents/http/conversion.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 4 additions & 4 deletions cloudevents/http/http_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
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

Expand All @@ -28,7 +28,7 @@

@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
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions cloudevents/http/json_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
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


@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,
Expand Down
2 changes: 1 addition & 1 deletion cloudevents/http/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
14 changes: 4 additions & 10 deletions cloudevents/pydantic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
1 change: 0 additions & 1 deletion cloudevents/pydantic/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
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

Expand Down
3 changes: 1 addition & 2 deletions cloudevents/pydantic/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"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
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions cloudevents/sdk/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
5 changes: 5 additions & 0 deletions cloudevents/sdk/converters/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@


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
Expand Down
20 changes: 10 additions & 10 deletions cloudevents/tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@

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 = {
v03.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,
},
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,
},
}

Expand All @@ -45,16 +45,16 @@
"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,
},
}
3 changes: 2 additions & 1 deletion cloudevents/tests/test_backwards_compatability.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions cloudevents/tests/test_data_encaps_refs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 8a88ffe

Please sign in to comment.