Skip to content

Commit

Permalink
Fix Pydantic custom attributes (#229)
Browse files Browse the repository at this point in the history
* Add custom extension attribute to the test set.

Replicates bug test data from the #228

Signed-off-by: Yurii Serhiichuk <[email protected]>

* use modern `super` syntax

Signed-off-by: Yurii Serhiichuk <[email protected]>

* Fix `black` language version

Signed-off-by: Yurii Serhiichuk <[email protected]>

* Fixes #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 <[email protected]>

* Add missing EOF

Signed-off-by: Yurii Serhiichuk <[email protected]>

* Add Pydantic fix to the changelog

Signed-off-by: Yurii Serhiichuk <[email protected]>

* Add links to the changelog

Signed-off-by: Yurii Serhiichuk <[email protected]>

* Bump version

Signed-off-by: Yurii Serhiichuk <[email protected]>

* Update Black and MyPy versions

Signed-off-by: Yurii Serhiichuk <[email protected]>

---------

Signed-off-by: Yurii Serhiichuk <[email protected]>
  • Loading branch information
xSAVIKx authored Oct 30, 2023
1 parent 8ada7d9 commit 21572af
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ 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.10
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/)
exclude: ^(cloudevents/tests/)
types: [ python ]
args: [ ]
additional_dependencies:
- 'pydantic'
- "pydantic"
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ 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 ([#229])

## [1.10.0] — 2023-09-25
### Added
- Pydantic v2 support. ([#219])
Expand Down Expand Up @@ -185,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
Expand Down Expand Up @@ -266,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
2 changes: 1 addition & 1 deletion cloudevents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# License for the specific language governing permissions and limitations
# under the License.

__version__ = "1.10.0"
__version__ = "1.10.1"
2 changes: 1 addition & 1 deletion cloudevents/pydantic/v1/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cloudevents/pydantic/v2/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
}

Expand Down
2 changes: 1 addition & 1 deletion cloudevents/tests/test_pydantic_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 21572af

Please sign in to comment.