Skip to content

Commit 8db1e29

Browse files
committed
chore: add typings and docstrings
Signed-off-by: Tudor Plugaru <plugaru.tudor@protonmail.com>
1 parent a2ac762 commit 8db1e29

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

src/cloudevents/core/v1/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
CloudEvent implementation for v1.0
3+
"""

src/cloudevents/core/v1/event.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
1-
from typing import Optional
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+
15+
from typing import Any, Optional
216
from datetime import datetime
317

418
REQUIRED_ATTRIBUTES = {"id", "source", "type", "specversion"}
519
OPTIONAL_ATTRIBUTES = {"datacontenttype", "dataschema", "subject", "time"}
620

721

822
class CloudEvent:
9-
def __init__(self, attributes: dict, data: Optional[dict] = None):
23+
def __init__(self, attributes: dict, data: Optional[dict] = None) -> 'CloudEvent':
24+
"""
25+
Create a new CloudEvent instance.
26+
27+
:param attributes: The attributes of the CloudEvent instance.
28+
:type attributes: dict
29+
:param data: The payload of the CloudEvent instance.
30+
:type data: Optional[dict]
31+
32+
:raises ValueError: If any of the required attributes are missing or have invalid values.
33+
:raises TypeError: If any of the attributes have invalid types.
34+
"""
1035
self.__validate_attribute(attributes)
1136
self._attributes = attributes
1237
self._data = data
@@ -64,8 +89,23 @@ def __validate_attribute(self, attributes: dict):
6489
if not attributes["dataschema"]:
6590
raise ValueError("Attribute 'dataschema' must not be empty")
6691

67-
def get_attribute(self, attribute: str):
92+
def get_attribute(self, attribute: str) -> Optional[Any]:
93+
"""
94+
Retrieve a value of an attribute of the event denoted by the given `attribute`.
95+
96+
:param attribute: The name of the event attribute to retrieve the value for.
97+
:type attribute: str
98+
99+
:return: The event attribute value.
100+
:rtype: Optional[Any]
101+
"""
68102
return self._attributes[attribute]
69103

70-
def get_data(self):
104+
def get_data(self) -> Optional[dict]:
105+
"""
106+
Retrieve data of the event.
107+
108+
:return: The data of the event.
109+
:rtype: Optional[dict]
110+
"""
71111
return self._data

tests/test_core/test_v1/test_event.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
({"id": "1", "source": "/", "type": "test"}, "specversion"),
1414
],
1515
)
16-
def test_missing_required_attribute(attributes, missing_attribute):
16+
def test_missing_required_attribute(attributes, missing_attribute) -> None:
1717
with pytest.raises(ValueError) as e:
1818
CloudEvent(attributes)
1919

@@ -27,15 +27,15 @@ def test_missing_required_attribute(attributes, missing_attribute):
2727
(12, "Attribute 'id' must be a string"),
2828
],
2929
)
30-
def test_id_validation(id, error):
30+
def test_id_validation(id, error) -> None:
3131
with pytest.raises((ValueError, TypeError)) as e:
3232
CloudEvent({"id": id, "source": "/", "type": "test", "specversion": "1.0"})
3333

3434
assert str(e.value) == error
3535

3636

3737
@pytest.mark.parametrize("source,error", [(123, "Attribute 'source' must be a string")])
38-
def test_source_validation(source, error):
38+
def test_source_validation(source, error) -> None:
3939
with pytest.raises((ValueError, TypeError)) as e:
4040
CloudEvent({"id": "1", "source": source, "type": "test", "specversion": "1.0"})
4141

@@ -49,7 +49,7 @@ def test_source_validation(source, error):
4949
("1.4", "Attribute 'specversion' must be '1.0'"),
5050
],
5151
)
52-
def test_specversion_validation(specversion, error):
52+
def test_specversion_validation(specversion, error) -> None:
5353
with pytest.raises((ValueError, TypeError)) as e:
5454
CloudEvent(
5555
{"id": "1", "source": "/", "type": "test", "specversion": specversion}
@@ -68,7 +68,7 @@ def test_specversion_validation(specversion, error):
6868
),
6969
],
7070
)
71-
def test_time_validation(time, error):
71+
def test_time_validation(time, error) -> None:
7272
with pytest.raises((ValueError, TypeError)) as e:
7373
CloudEvent(
7474
{
@@ -93,7 +93,7 @@ def test_time_validation(time, error):
9393
),
9494
],
9595
)
96-
def test_subject_validation(subject, error):
96+
def test_subject_validation(subject, error) -> None:
9797
with pytest.raises((ValueError, TypeError)) as e:
9898
CloudEvent(
9999
{
@@ -118,7 +118,7 @@ def test_subject_validation(subject, error):
118118
),
119119
],
120120
)
121-
def test_datacontenttype_validation(datacontenttype, error):
121+
def test_datacontenttype_validation(datacontenttype, error) -> None:
122122
with pytest.raises((ValueError, TypeError)) as e:
123123
CloudEvent(
124124
{
@@ -143,7 +143,7 @@ def test_datacontenttype_validation(datacontenttype, error):
143143
),
144144
],
145145
)
146-
def test_dataschema_validation(dataschema, error):
146+
def test_dataschema_validation(dataschema, error) -> None:
147147
with pytest.raises((ValueError, TypeError)) as e:
148148
CloudEvent(
149149
{

0 commit comments

Comments
 (0)