Fix formatting (#131)

* Fix formatting for latest black

Signed-off-by: Dustin Ingram <di@users.noreply.github.com>

* Add flake8 for linting

Signed-off-by: Dustin Ingram <di@users.noreply.github.com>

* Fix flake8 lint errors

Signed-off-by: Dustin Ingram <di@users.noreply.github.com>
This commit is contained in:
Dustin Ingram 2020-10-20 11:31:02 -05:00 committed by GitHub
parent 390f5944c0
commit 8773319279
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 39 additions and 60 deletions

View File

@ -11,16 +11,14 @@
# 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 json
import typing
from cloudevents.http.event import CloudEvent
from cloudevents.http.event_type import is_binary, is_structured
from cloudevents.http.http_methods import (
from cloudevents.http.event import CloudEvent # noqa
from cloudevents.http.event_type import is_binary, is_structured # noqa
from cloudevents.http.http_methods import ( # noqa
from_http,
to_binary,
to_binary_http,
to_structured,
to_structured_http,
)
from cloudevents.http.json_methods import from_json, to_json
from cloudevents.http.json_methods import from_json, to_json # noqa

View File

@ -7,7 +7,8 @@ def is_binary(headers: typing.Dict[str, str]) -> bool:
"""Uses internal marshallers to determine whether this event is binary
:param headers: the HTTP headers
:type headers: typing.Dict[str, str]
:returns bool: returns a bool indicating whether the headers indicate a binary event type
:returns bool: returns a bool indicating whether the headers indicate
a binary event type
"""
headers = {key.lower(): value for key, value in headers.items()}
content_type = headers.get("content-type", "")
@ -19,7 +20,8 @@ def is_structured(headers: typing.Dict[str, str]) -> bool:
"""Uses internal marshallers to determine whether this event is structured
:param headers: the HTTP headers
:type headers: typing.Dict[str, str]
:returns bool: returns a bool indicating whether the headers indicate a structured event type
:returns bool: returns a bool indicating whether the headers indicate
a structured event type
"""
headers = {key.lower(): value for key, value in headers.items()}
content_type = headers.get("content-type", "")

View File

@ -5,7 +5,7 @@ from deprecation import deprecated
import cloudevents.exceptions as cloud_exceptions
from cloudevents.http.event import CloudEvent
from cloudevents.http.event_type import is_binary, is_structured
from cloudevents.http.event_type import is_binary
from cloudevents.http.mappings import _marshaller_by_format, _obj_by_version
from cloudevents.http.util import _json_or_string
from cloudevents.sdk import converters, marshaller, types
@ -124,7 +124,7 @@ def _to_http(
def to_structured(
event: CloudEvent, data_marshaller: types.MarshallerType = None,
event: CloudEvent, data_marshaller: types.MarshallerType = None
) -> (dict, typing.Union[bytes, str]):
"""
Returns a tuple of HTTP headers/body dicts representing this cloudevent. If
@ -142,7 +142,7 @@ def to_structured(
def to_binary(
event: CloudEvent, data_marshaller: types.MarshallerType = None,
event: CloudEvent, data_marshaller: types.MarshallerType = None
) -> (dict, typing.Union[bytes, str]):
"""
Returns a tuple of HTTP headers/body dicts representing this cloudevent
@ -163,13 +163,13 @@ def to_binary(
@deprecated(deprecated_in="1.0.2", details="Use to_binary function instead")
def to_binary_http(
event: CloudEvent, data_marshaller: types.MarshallerType = None,
event: CloudEvent, data_marshaller: types.MarshallerType = None
) -> (dict, typing.Union[bytes, str]):
return to_binary(event, data_marshaller)
@deprecated(deprecated_in="1.0.2", details="Use to_structured function instead")
def to_structured_http(
event: CloudEvent, data_marshaller: types.MarshallerType = None,
event: CloudEvent, data_marshaller: types.MarshallerType = None
) -> (dict, typing.Union[bytes, str]):
return to_structured(event, data_marshaller)

View File

@ -16,5 +16,5 @@ def _json_or_string(content: typing.Union[str, bytes]):
return None
try:
return json.loads(content)
except (json.JSONDecodeError, TypeError) as e:
except (json.JSONDecodeError, TypeError):
return content

View File

@ -27,7 +27,7 @@ class JSONHTTPCloudEventConverter(base.Converter):
MIME_TYPE = "application/cloudevents+json"
def can_read(
self, content_type: str, headers: typing.Dict[str, str] = {},
self, content_type: str, headers: typing.Dict[str, str] = {}
) -> bool:
return (
isinstance(content_type, str)

View File

@ -30,4 +30,4 @@ def test_get_nonexistent_optional(event_class):
event = event_class()
event.SetExtensions({"ext1": "val"})
res = event.Get("ext1")
assert res[0] == "val" and res[1] == True
assert res[0] == "val" and res[1] is True

View File

@ -14,7 +14,7 @@
import pytest
from cloudevents.sdk import exceptions
from cloudevents.sdk.converters import base, binary, structured
from cloudevents.sdk.converters import base, binary
def test_binary_converter_raise_unsupported():

View File

@ -12,15 +12,12 @@
# License for the specific language governing permissions and limitations
# under the License.
import copy
import io
import json
from uuid import uuid4
import pytest
from cloudevents.sdk import converters, marshaller
from cloudevents.sdk.converters import structured
from cloudevents.sdk.event import v1, v03
from cloudevents.tests import data
@ -71,7 +68,6 @@ def test_general_binary_properties(event_class):
@pytest.mark.parametrize("event_class", [v03.Event, v1.Event])
def test_general_structured_properties(event_class):
copy_of_ce = copy.deepcopy(data.json_ce[event_class])
m = marshaller.NewDefaultHTTPMarshaller()
http_headers = {"content-type": "application/cloudevents+json"}
event = m.FromRequest(

View File

@ -12,12 +12,11 @@
# License for the specific language governing permissions and limitations
# under the License.
import io
import json
import pytest
from cloudevents.sdk import exceptions, marshaller
from cloudevents.sdk import marshaller
from cloudevents.sdk.converters import binary, structured
from cloudevents.sdk.event import v1, v03
from cloudevents.tests import data

View File

@ -12,7 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import io
import json
import pytest

View File

@ -12,14 +12,11 @@
# License for the specific language governing permissions and limitations
# under the License.
import copy
import io
import json
import pytest
from cloudevents.sdk import converters, marshaller
from cloudevents.sdk.converters import structured
from cloudevents.sdk.event import v1, v03
from cloudevents.tests import data
@ -45,7 +42,6 @@ def test_binary_event_to_request_upstream(event_class):
@pytest.mark.parametrize("event_class", [v03.Event, v1.Event])
def test_structured_event_to_request_upstream(event_class):
copy_of_ce = copy.deepcopy(data.json_ce[event_class])
m = marshaller.NewDefaultHTTPMarshaller()
http_headers = {"content-type": "application/cloudevents+json"}
event = m.FromRequest(

View File

@ -13,7 +13,6 @@
# under the License.
import bz2
import copy
import io
import json
@ -27,9 +26,7 @@ from cloudevents.http import (
is_binary,
is_structured,
to_binary,
to_binary_http,
to_structured,
to_structured_http,
)
from cloudevents.sdk import converters
@ -90,10 +87,10 @@ 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) as e:
with pytest.raises(cloud_exceptions.MissingRequiredFields):
_ = from_http(
{"Content-Type": "application/cloudevents+json"}, json.dumps(body),
{"Content-Type": "application/cloudevents+json"}, json.dumps(body)
)
@ -220,7 +217,7 @@ def test_valid_binary_events(specversion):
headers = {
"ce-id": f"id{i}",
"ce-source": f"source{i}.com.test",
"ce-type": f"cloudevent.test.type",
"ce-type": "cloudevent.test.type",
"ce-specversion": specversion,
}
data = {"payload": f"payload-{i}"}
@ -289,14 +286,14 @@ def test_empty_data_structured_event(specversion):
event = from_http(
{"content-type": "application/cloudevents+json"}, json.dumps(attributes)
)
assert event.data == None
assert event.data is None
attributes["data"] = ""
# Data of empty string will be marshalled into None
event = from_http(
{"content-type": "application/cloudevents+json"}, json.dumps(attributes)
)
assert event.data == None
assert event.data is None
@pytest.mark.parametrize("specversion", ["1.0", "0.3"])
@ -311,12 +308,12 @@ def test_empty_data_binary_event(specversion):
"ce-source": "<source-url>",
}
event = from_http(headers, None)
assert event.data == None
assert event.data is None
data = ""
# Data of empty string will be marshalled into None
event = from_http(headers, data)
assert event.data == None
assert event.data is None
@pytest.mark.parametrize("specversion", ["1.0", "0.3"])
@ -328,7 +325,7 @@ def test_valid_structured_events(specversion):
event = {
"id": f"id{i}",
"source": f"source{i}.com.test",
"type": f"cloudevent.test.type",
"type": "cloudevent.test.type",
"specversion": specversion,
"data": {"payload": f"payload-{i}"},
}
@ -465,9 +462,7 @@ def test_empty_json_structured():
headers = {"Content-Type": "application/cloudevents+json"}
data = ""
with pytest.raises(cloud_exceptions.MissingRequiredFields) as e:
from_http(
headers, data,
)
from_http(headers, data)
assert "Failed to read specversion from both headers and data" in str(
e.value
)
@ -484,10 +479,10 @@ def test_uppercase_headers_with_none_data_binary():
for key in headers:
assert event[key.lower()[3:]] == headers[key]
assert event.data == None
assert event.data is None
_, new_data = to_binary(event)
assert new_data == None
assert new_data is None
def test_generic_exception():
@ -524,9 +519,7 @@ def test_non_dict_data_no_headers_bug():
headers = {"Content-Type": "application/cloudevents+json"}
data = "123"
with pytest.raises(cloud_exceptions.MissingRequiredFields) as e:
from_http(
headers, data,
)
from_http(headers, data)
assert "Failed to read specversion from both headers and data" in str(
e.value
)

View File

@ -18,7 +18,7 @@ import pytest
import cloudevents.exceptions as cloud_exceptions
from cloudevents.http import CloudEvent, from_http, to_binary, to_structured
from cloudevents.sdk import converters, exceptions, marshaller
from cloudevents.sdk import exceptions, marshaller
from cloudevents.sdk.converters import binary, structured
from cloudevents.sdk.event import v1
@ -61,7 +61,7 @@ def test_to_request_wrong_marshaller():
def test_from_request_cannot_read(binary_headers):
with pytest.raises(exceptions.UnsupportedEventConverter):
m = marshaller.HTTPMarshaller(
[binary.NewBinaryHTTPCloudEventConverter(),]
[binary.NewBinaryHTTPCloudEventConverter()]
)
m.FromRequest(v1.Event(), {}, "")

View File

@ -11,8 +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.
import pytest
from cloudevents.sdk.event import v03

View File

@ -12,8 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import pytest
from cloudevents.sdk.event import v1

View File

@ -18,7 +18,7 @@ import requests
from cloudevents.http import CloudEvent, to_binary, to_structured
resp = requests.get(
"https://raw.githubusercontent.com/cncf/artwork/master/projects/cloudevents/horizontal/color/cloudevents-horizontal-color.png"
"https://raw.githubusercontent.com/cncf/artwork/master/projects/cloudevents/horizontal/color/cloudevents-horizontal-color.png" # noqa
)
image_bytes = resp.content

View File

@ -13,7 +13,7 @@
# under the License.
import io
from flask import Flask, Response, request
from flask import Flask, request
from PIL import Image
from cloudevents.http import from_http

View File

@ -11,7 +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.
import io
import sys
import requests

View File

@ -14,19 +14,20 @@ commands = pytest {env:PYTESTARGS} {posargs}
[testenv:reformat]
basepython=python3.8
deps =
deps =
black
isort
commands =
commands =
black .
isort cloudevents samples
[testenv:lint]
basepython = python3.8
deps =
deps =
black
isort
flake8
commands =
black --check .
isort -c cloudevents samples
flake8 cloudevents samples --ignore W503,E731 --max-line-length 88