Skip to content

Commit ad111ae

Browse files
fix __eq__ operator raises attribute error on non-cloudevent values (#172)
* fix: non-cloudevents values must not equal to cloudevents values (#171) Signed-off-by: Alexander Tkachev <[email protected]> * test: refactor move fixtures to beginning Signed-off-by: Alexander Tkachev <[email protected]> * test: cloudevent equality bug regression (#171) Signed-off-by: Alexander Tkachev <[email protected]> * style: remove redundent else Signed-off-by: Alexander Tkachev <[email protected]> * test: remove redundent test Signed-off-by: Alexander Tkachev <[email protected]> * test: refactor non_cloudevent_value into a parameterization Signed-off-by: Alexander Tkachev <[email protected]> * docs: update changelog Signed-off-by: Alexander Tkachev <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * docs: fix bad merge Signed-off-by: Alexander Tkachev <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f39b964 commit ad111ae

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Added `.get` accessor for even properties ([#165])
1111
- Added type information for all event member functions ([#173])
1212

13+
### Fixed
14+
- Fixed event `__eq__` operator raising `AttributeError` on non-CloudEvent values ([#172])
15+
1316
### Changed
1417
- Code quality and styling tooling is unified and configs compatibility is ensured ([#167])
1518
- CI configurations updated and added macOS and Windows tests ([#169])
@@ -18,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1821
### Removed
1922
- `docs` folder and related unused tooling ([#168])
2023

24+
2125
## [1.3.0] — 2022-09-07
2226
### Added
2327
- Python 3.9 support ([#144])
@@ -156,4 +160,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
156160
[#168]: https://github.com/cloudevents/sdk-python/pull/168
157161
[#169]: https://github.com/cloudevents/sdk-python/pull/169
158162
[#170]: https://github.com/cloudevents/sdk-python/pull/170
163+
[#172]: https://github.com/cloudevents/sdk-python/pull/172
159164
[#173]: https://github.com/cloudevents/sdk-python/pull/173

cloudevents/http/event.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ def __init__(self, attributes: typing.Dict[str, str], data: typing.Any = None):
6868
)
6969

7070
def __eq__(self, other: typing.Any) -> bool:
71-
return self.data == other.data and self._attributes == other._attributes
71+
if isinstance(other, CloudEvent):
72+
return self.data == other.data and self._attributes == other._attributes
73+
return False
7274

7375
# Data access is handled via `.data` member
7476
# Attribute access is managed via Mapping type

cloudevents/tests/test_http_cloudevent.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ def your_dummy_data():
4747
return '{"name":"paul"}'
4848

4949

50+
@pytest.fixture()
51+
def dummy_event(dummy_attributes, my_dummy_data):
52+
return CloudEvent(attributes=dummy_attributes, data=my_dummy_data)
53+
54+
55+
@pytest.fixture()
56+
def non_exiting_attribute_name(dummy_event):
57+
result = "nonexisting"
58+
assert result not in dummy_event
59+
return result
60+
61+
5062
def test_http_cloudevent_equality(dummy_attributes, my_dummy_data, your_dummy_data):
5163
data = my_dummy_data
5264
event1 = CloudEvent(dummy_attributes, data)
@@ -71,6 +83,21 @@ def test_http_cloudevent_equality(dummy_attributes, my_dummy_data, your_dummy_da
7183
assert event1 != event2 and event3 != event1
7284

7385

86+
@pytest.mark.parametrize(
87+
"non_cloudevent_value",
88+
(
89+
1,
90+
None,
91+
object(),
92+
"Hello World",
93+
),
94+
)
95+
def test_http_cloudevent_must_not_equal_to_non_cloudevent_value(
96+
dummy_event, non_cloudevent_value
97+
):
98+
assert not dummy_event == non_cloudevent_value
99+
100+
74101
def test_http_cloudevent_mutates_equality(
75102
dummy_attributes, my_dummy_data, your_dummy_data
76103
):
@@ -145,18 +172,6 @@ def test_none_json_or_string():
145172
assert _json_or_string(None) is None
146173

147174

148-
@pytest.fixture()
149-
def dummy_event(dummy_attributes, my_dummy_data):
150-
return CloudEvent(attributes=dummy_attributes, data=my_dummy_data)
151-
152-
153-
@pytest.fixture()
154-
def non_exiting_attribute_name(dummy_event):
155-
result = "nonexisting"
156-
assert result not in dummy_event
157-
return result
158-
159-
160175
def test_get_operation_on_non_existing_attribute_must_not_raise_exception(
161176
dummy_event, non_exiting_attribute_name
162177
):

0 commit comments

Comments
 (0)