From cf267509f69e060242eba236a7cabe8f4d614216 Mon Sep 17 00:00:00 2001 From: Yurii Serhiichuk Date: Sun, 29 Oct 2023 22:23:25 +0100 Subject: [PATCH 1/9] Add custom extension attribute to the test set. Replicates bug test data from the https://github.com/cloudevents/sdk-python/issues/228 Signed-off-by: Yurii Serhiichuk --- cloudevents/tests/test_pydantic_conversions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cloudevents/tests/test_pydantic_conversions.py b/cloudevents/tests/test_pydantic_conversions.py index 4beb981a..86119236 100644 --- a/cloudevents/tests/test_pydantic_conversions.py +++ b/cloudevents/tests/test_pydantic_conversions.py @@ -33,9 +33,9 @@ test_attributes = { "type": "com.example.string", "source": "https://example.com/event-producer", + "extension-attribute": "extension-attribute-test-value", } - _pydantic_implementation = { "v1": { "event": PydanticV1CloudEvent, @@ -177,3 +177,4 @@ def test_from_dict(cloudevents_implementation): "type": "dummy.type", } assert cloudevents_implementation["from_dict"](given).dict() == given + From e90f9199915f3dce4a1b047fe75d75edf8d129c8 Mon Sep 17 00:00:00 2001 From: Yurii Serhiichuk Date: Sun, 29 Oct 2023 22:24:05 +0100 Subject: [PATCH 2/9] use modern `super` syntax Signed-off-by: Yurii Serhiichuk --- cloudevents/pydantic/v1/event.py | 2 +- cloudevents/pydantic/v2/event.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cloudevents/pydantic/v1/event.py b/cloudevents/pydantic/v1/event.py index cd387014..d18736a4 100644 --- a/cloudevents/pydantic/v1/event.py +++ b/cloudevents/pydantic/v1/event.py @@ -186,7 +186,7 @@ def __init__( # type: ignore[no-untyped-def] ) attributes = {k.lower(): v for k, v in attributes.items()} kwargs.update(attributes) - super(CloudEvent, self).__init__(data=data, **kwargs) + super().__init__(data=data, **kwargs) class Config: extra: str = "allow" # this is the way we implement extensions diff --git a/cloudevents/pydantic/v2/event.py b/cloudevents/pydantic/v2/event.py index 17ed8d97..890c03d1 100644 --- a/cloudevents/pydantic/v2/event.py +++ b/cloudevents/pydantic/v2/event.py @@ -134,7 +134,7 @@ def __init__( # type: ignore[no-untyped-def] ) attributes = {k.lower(): v for k, v in attributes.items()} kwargs.update(attributes) - super(CloudEvent, self).__init__(data=data, **kwargs) + super().__init__(data=data, **kwargs) model_config = ConfigDict( extra="allow", # this is the way we implement extensions From 96407d03e596e43dd1d3156118a36fadcdcff3c4 Mon Sep 17 00:00:00 2001 From: Yurii Serhiichuk Date: Sun, 29 Oct 2023 22:26:29 +0100 Subject: [PATCH 3/9] Fix `black` language version Signed-off-by: Yurii Serhiichuk --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1169a0a3..acd1f3d3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,7 +14,7 @@ repos: rev: 23.9.1 hooks: - id: black - language_version: python3.10 + language_version: python3.11 - repo: https://github.com/pre-commit/mirrors-mypy rev: "v1.6.0" hooks: From 215c9717e489106f5c9cf0e13eea26cc395fd75d Mon Sep 17 00:00:00 2001 From: Yurii Serhiichuk Date: Sun, 29 Oct 2023 22:27:24 +0100 Subject: [PATCH 4/9] Fixes https://github.com/cloudevents/sdk-python/issues/228 Pydantic v2 .__dict__ has different behavior from what Pydantic v1 had and is not giving us `extra` fields anymore. On the other hand the iterator over the event gives us extras as well Signed-off-by: Yurii Serhiichuk --- cloudevents/pydantic/v2/event.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudevents/pydantic/v2/event.py b/cloudevents/pydantic/v2/event.py index 890c03d1..4ae8bb5c 100644 --- a/cloudevents/pydantic/v2/event.py +++ b/cloudevents/pydantic/v2/event.py @@ -209,7 +209,7 @@ def _ce_json_dumps(self) -> typing.Dict[str, typing.Any]: def _get_attributes(self) -> typing.Dict[str, typing.Any]: return { key: conversion.best_effort_encode_attribute_value(value) - for key, value in self.__dict__.items() + for key, value in dict(BaseModel.__iter__(self)).items() if key not in ["data"] } From 2248928669a4b838e5510db36a915078e8cb0e91 Mon Sep 17 00:00:00 2001 From: Yurii Serhiichuk Date: Sun, 29 Oct 2023 22:28:04 +0100 Subject: [PATCH 5/9] Add missing EOF Signed-off-by: Yurii Serhiichuk --- cloudevents/tests/test_pydantic_conversions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cloudevents/tests/test_pydantic_conversions.py b/cloudevents/tests/test_pydantic_conversions.py index 86119236..801b76bd 100644 --- a/cloudevents/tests/test_pydantic_conversions.py +++ b/cloudevents/tests/test_pydantic_conversions.py @@ -177,4 +177,3 @@ def test_from_dict(cloudevents_implementation): "type": "dummy.type", } assert cloudevents_implementation["from_dict"](given).dict() == given - From c5e4b9288674362a6c828f915af8622547268a29 Mon Sep 17 00:00:00 2001 From: Yurii Serhiichuk Date: Sun, 29 Oct 2023 22:29:32 +0100 Subject: [PATCH 6/9] Add Pydantic fix to the changelog Signed-off-by: Yurii Serhiichuk --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44e991b5..7d0dca53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- Fixed Pydantic v2 `to_json` (and `to_structured`) conversion () + ## [1.10.0] — 2023-09-25 ### Added - Pydantic v2 support. ([#219]) From 61c9991f4a312477fab87d2fd6a66e8ee4e59c50 Mon Sep 17 00:00:00 2001 From: Yurii Serhiichuk Date: Sun, 29 Oct 2023 22:33:08 +0100 Subject: [PATCH 7/9] Add links to the changelog Signed-off-by: Yurii Serhiichuk --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d0dca53..51fcb0e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.10.1] + ### Fixed -- Fixed Pydantic v2 `to_json` (and `to_structured`) conversion () +- Fixed Pydantic v2 `to_json` (and `to_structured`) conversion ([#229]) ## [1.10.0] — 2023-09-25 ### Added @@ -188,6 +190,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Initial release +[1.10.1]: https://github.com/cloudevents/sdk-python/compare/1.10.0...1.10.1 [1.10.0]: https://github.com/cloudevents/sdk-python/compare/1.9.0...1.10.0 [1.9.0]: https://github.com/cloudevents/sdk-python/compare/1.8.0...1.9.0 [1.8.0]: https://github.com/cloudevents/sdk-python/compare/1.7.0...1.8.0 @@ -269,3 +272,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#218]: https://github.com/cloudevents/sdk-python/pull/218 [#219]: https://github.com/cloudevents/sdk-python/pull/219 [#221]: https://github.com/cloudevents/sdk-python/pull/221 +[#229]: https://github.com/cloudevents/sdk-python/pull/229 From 7b8d4aa77c1585f50ae0a04d8dfa3a10b0e2bf79 Mon Sep 17 00:00:00 2001 From: Yurii Serhiichuk Date: Sun, 29 Oct 2023 22:33:32 +0100 Subject: [PATCH 8/9] Bump version Signed-off-by: Yurii Serhiichuk --- cloudevents/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudevents/__init__.py b/cloudevents/__init__.py index 1cabc336..c6e11514 100644 --- a/cloudevents/__init__.py +++ b/cloudevents/__init__.py @@ -12,4 +12,4 @@ # License for the specific language governing permissions and limitations # under the License. -__version__ = "1.10.0" +__version__ = "1.10.1" From f79c866a1c7b3b37dce3d471da0f2eae60c79403 Mon Sep 17 00:00:00 2001 From: Yurii Serhiichuk Date: Sun, 29 Oct 2023 22:34:20 +0100 Subject: [PATCH 9/9] Update Black and MyPy versions Signed-off-by: Yurii Serhiichuk --- .pre-commit-config.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index acd1f3d3..15ab6545 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,12 +11,12 @@ repos: - id: isort args: [ "--profile", "black", "--filter-files" ] - repo: https://github.com/psf/black - rev: 23.9.1 + rev: 23.10.1 hooks: - id: black language_version: python3.11 - repo: https://github.com/pre-commit/mirrors-mypy - rev: "v1.6.0" + rev: v1.6.1 hooks: - id: mypy files: ^(cloudevents/) @@ -24,4 +24,4 @@ repos: types: [ python ] args: [ ] additional_dependencies: - - 'pydantic' + - "pydantic"