Skip to content

Commit 42b4fe1

Browse files
committed
chore: Add copyright and fix missing type info
Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>
1 parent 35dee7d commit 42b4fe1

File tree

5 files changed

+87
-28
lines changed

5 files changed

+87
-28
lines changed

src/cloudevents/core/v1/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2018-Present The CloudEvents Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
115
"""
216
CloudEvent implementation for v1.0
317
"""

src/cloudevents/core/v1/event.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,48 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from typing import Any, Optional
15+
from typing import Any, Optional, Final
1616
from datetime import datetime
1717
import re
1818

19-
REQUIRED_ATTRIBUTES = {"id", "source", "type", "specversion"}
20-
OPTIONAL_ATTRIBUTES = {"datacontenttype", "dataschema", "subject", "time"}
19+
REQUIRED_ATTRIBUTES: Final[set[str]] = {"id", "source", "type", "specversion"}
20+
OPTIONAL_ATTRIBUTES: Final[set[str]] = {
21+
"datacontenttype",
22+
"dataschema",
23+
"subject",
24+
"time",
25+
}
2126

2227

2328
class CloudEvent:
24-
def __init__(self, attributes: dict, data: Optional[dict] = None) -> None:
29+
"""
30+
The CloudEvent Python wrapper contract exposing generically-available
31+
properties and APIs.
32+
33+
Implementations might handle fields and have other APIs exposed but are
34+
obliged to follow this contract.
35+
"""
36+
37+
def __init__(self, attributes: dict[str, Any], data: Optional[dict] = None) -> None:
2538
"""
2639
Create a new CloudEvent instance.
2740
2841
:param attributes: The attributes of the CloudEvent instance.
29-
:type attributes: dict
3042
:param data: The payload of the CloudEvent instance.
31-
:type data: Optional[dict]
3243
3344
:raises ValueError: If any of the required attributes are missing or have invalid values.
3445
:raises TypeError: If any of the attributes have invalid types.
3546
"""
3647
self._validate_attribute(attributes)
37-
self._attributes = attributes
38-
self._data = data
48+
self._attributes: dict = attributes
49+
self._data: Optional[dict] = data
3950

40-
def _validate_attribute(self, attributes: dict) -> None:
51+
@staticmethod
52+
def _validate_attribute(attributes: dict) -> None:
4153
"""
42-
Private method that validates the attributes of the CloudEvent as per the CloudEvents specification.
54+
Validates the attributes of the CloudEvent as per the CloudEvents specification.
55+
56+
See https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md#required-attributes
4357
"""
4458
missing_attributes = [
4559
attr for attr in REQUIRED_ATTRIBUTES if attr not in attributes
@@ -95,38 +109,31 @@ def _validate_attribute(self, attributes: dict) -> None:
95109
if not attributes["dataschema"]:
96110
raise ValueError("Attribute 'dataschema' must not be empty")
97111

98-
for custom_extension in (
112+
for extension_attributes in (
99113
set(attributes.keys()) - REQUIRED_ATTRIBUTES - OPTIONAL_ATTRIBUTES
100114
):
101-
if custom_extension == "data":
115+
if extension_attributes == "data":
102116
raise ValueError(
103117
"Extension attribute 'data' is reserved and must not be used"
104118
)
105119

106-
if not custom_extension[0].isalpha():
107-
raise ValueError(
108-
f"Extension attribute '{custom_extension}' should start with a letter"
109-
)
110-
111-
if not (5 <= len(custom_extension) <= 20):
120+
if not (1 <= len(extension_attributes) <= 20):
112121
raise ValueError(
113-
f"Extension attribute '{custom_extension}' should be between 5 and 20 characters long"
122+
f"Extension attribute '{extension_attributes}' should be between 1 and 20 characters long"
114123
)
115124

116-
if not re.match(r"^[a-z0-9]+$", custom_extension):
125+
if not re.match(r"^[a-z0-9]+$", extension_attributes):
117126
raise ValueError(
118-
f"Extension attribute '{custom_extension}' should only contain lowercase letters and numbers"
127+
f"Extension attribute '{extension_attributes}' should only contain lowercase letters and numbers"
119128
)
120129

121130
def get_attribute(self, attribute: str) -> Optional[Any]:
122131
"""
123132
Retrieve a value of an attribute of the event denoted by the given `attribute`.
124133
125134
:param attribute: The name of the event attribute to retrieve the value for.
126-
:type attribute: str
127135
128136
:return: The event attribute value.
129-
:rtype: Optional[Any]
130137
"""
131138
return self._attributes[attribute]
132139

@@ -135,6 +142,5 @@ def get_data(self) -> Optional[dict]:
135142
Retrieve data of the event.
136143
137144
:return: The data of the event.
138-
:rtype: Optional[dict]
139145
"""
140146
return self._data

tests/test_core/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2018-Present The CloudEvents Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.

tests/test_core/test_v1/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2018-Present The CloudEvents Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.

tests/test_core/test_v1/test_event.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2018-Present The CloudEvents Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
115
from cloudevents.core.v1.event import CloudEvent
216

317
import pytest
@@ -162,14 +176,13 @@ def test_dataschema_validation(dataschema: Any, error: str) -> None:
162176
@pytest.mark.parametrize(
163177
"extension_name,error",
164178
[
165-
("123", "Extension attribute '123' should start with a letter"),
166179
(
167-
"shrt",
168-
"Extension attribute 'shrt' should be between 5 and 20 characters long",
180+
"",
181+
"Extension attribute '' should be between 1 and 20 characters long",
169182
),
170183
(
171184
"thisisaverylongextension",
172-
"Extension attribute 'thisisaverylongextension' should be between 5 and 20 characters long",
185+
"Extension attribute 'thisisaverylongextension' should be between 1 and 20 characters long",
173186
),
174187
(
175188
"ThisIsNotValid",

0 commit comments

Comments
 (0)