Skip to content

Commit 21572af

Browse files
authored
Fix Pydantic custom attributes (#229)
* 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]>
1 parent 8ada7d9 commit 21572af

File tree

6 files changed

+16
-9
lines changed

6 files changed

+16
-9
lines changed

.pre-commit-config.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ repos:
1111
- id: isort
1212
args: [ "--profile", "black", "--filter-files" ]
1313
- repo: https://github.com/psf/black
14-
rev: 23.9.1
14+
rev: 23.10.1
1515
hooks:
1616
- id: black
17-
language_version: python3.10
17+
language_version: python3.11
1818
- repo: https://github.com/pre-commit/mirrors-mypy
19-
rev: "v1.6.0"
19+
rev: v1.6.1
2020
hooks:
2121
- id: mypy
2222
files: ^(cloudevents/)
2323
exclude: ^(cloudevents/tests/)
2424
types: [ python ]
2525
args: [ ]
2626
additional_dependencies:
27-
- 'pydantic'
27+
- "pydantic"

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.10.1]
10+
11+
### Fixed
12+
- Fixed Pydantic v2 `to_json` (and `to_structured`) conversion ([#229])
13+
914
## [1.10.0] — 2023-09-25
1015
### Added
1116
- Pydantic v2 support. ([#219])
@@ -185,6 +190,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
185190
### Added
186191
- Initial release
187192

193+
[1.10.1]: https://github.com/cloudevents/sdk-python/compare/1.10.0...1.10.1
188194
[1.10.0]: https://github.com/cloudevents/sdk-python/compare/1.9.0...1.10.0
189195
[1.9.0]: https://github.com/cloudevents/sdk-python/compare/1.8.0...1.9.0
190196
[1.8.0]: https://github.com/cloudevents/sdk-python/compare/1.7.0...1.8.0
@@ -266,3 +272,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
266272
[#218]: https://github.com/cloudevents/sdk-python/pull/218
267273
[#219]: https://github.com/cloudevents/sdk-python/pull/219
268274
[#221]: https://github.com/cloudevents/sdk-python/pull/221
275+
[#229]: https://github.com/cloudevents/sdk-python/pull/229

cloudevents/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
__version__ = "1.10.0"
15+
__version__ = "1.10.1"

cloudevents/pydantic/v1/event.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def __init__( # type: ignore[no-untyped-def]
186186
)
187187
attributes = {k.lower(): v for k, v in attributes.items()}
188188
kwargs.update(attributes)
189-
super(CloudEvent, self).__init__(data=data, **kwargs)
189+
super().__init__(data=data, **kwargs)
190190

191191
class Config:
192192
extra: str = "allow" # this is the way we implement extensions

cloudevents/pydantic/v2/event.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def __init__( # type: ignore[no-untyped-def]
134134
)
135135
attributes = {k.lower(): v for k, v in attributes.items()}
136136
kwargs.update(attributes)
137-
super(CloudEvent, self).__init__(data=data, **kwargs)
137+
super().__init__(data=data, **kwargs)
138138

139139
model_config = ConfigDict(
140140
extra="allow", # this is the way we implement extensions
@@ -209,7 +209,7 @@ def _ce_json_dumps(self) -> typing.Dict[str, typing.Any]:
209209
def _get_attributes(self) -> typing.Dict[str, typing.Any]:
210210
return {
211211
key: conversion.best_effort_encode_attribute_value(value)
212-
for key, value in self.__dict__.items()
212+
for key, value in dict(BaseModel.__iter__(self)).items()
213213
if key not in ["data"]
214214
}
215215

cloudevents/tests/test_pydantic_conversions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
test_attributes = {
3434
"type": "com.example.string",
3535
"source": "https://example.com/event-producer",
36+
"extension-attribute": "extension-attribute-test-value",
3637
}
3738

38-
3939
_pydantic_implementation = {
4040
"v1": {
4141
"event": PydanticV1CloudEvent,

0 commit comments

Comments
 (0)