Skip to content

Commit 11520e3

Browse files
fabiobxSAVIKx
andauthored
Pydantic v2 (#235)
* Fixes examples when using Pydantic V2 Signed-off-by: Fabio Batista <[email protected]> * When type checking, uses the latest (V2) version of Pydantic Signed-off-by: Fabio Batista <[email protected]> --------- Signed-off-by: Fabio Batista <[email protected]> Co-authored-by: Yurii Serhiichuk <[email protected]>
1 parent eedc61e commit 11520e3

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ repos:
2424
types: [ python ]
2525
args: [ ]
2626
additional_dependencies:
27-
- "pydantic"
27+
- "pydantic~=2.7"

cloudevents/pydantic/__init__.py

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

15+
from typing import TYPE_CHECKING
16+
1517
from cloudevents.exceptions import PydanticFeatureNotInstalled
1618

1719
try:
18-
from pydantic import VERSION as PYDANTIC_VERSION
19-
20-
pydantic_major_version = PYDANTIC_VERSION.split(".")[0]
21-
if pydantic_major_version == "1":
22-
from cloudevents.pydantic.v1 import CloudEvent, from_dict, from_http, from_json
23-
20+
if TYPE_CHECKING:
21+
from cloudevents.pydantic.v2 import CloudEvent, from_dict, from_http, from_json
2422
else:
25-
from cloudevents.pydantic.v2 import ( # type: ignore
26-
CloudEvent,
27-
from_dict,
28-
from_http,
29-
from_json,
30-
)
23+
from pydantic import VERSION as PYDANTIC_VERSION
24+
25+
pydantic_major_version = PYDANTIC_VERSION.split(".")[0]
26+
if pydantic_major_version == "1":
27+
from cloudevents.pydantic.v1 import (
28+
CloudEvent,
29+
from_dict,
30+
from_http,
31+
from_json,
32+
)
33+
else:
34+
from cloudevents.pydantic.v2 import (
35+
CloudEvent,
36+
from_dict,
37+
from_http,
38+
from_json,
39+
)
3140

3241
except ImportError: # pragma: no cover # hard to test
3342
raise PydanticFeatureNotInstalled(

cloudevents/pydantic/v2/event.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -51,53 +51,53 @@ def create(
5151
data: typing.Optional[typing.Any] = Field(
5252
title=FIELD_DESCRIPTIONS["data"].get("title"),
5353
description=FIELD_DESCRIPTIONS["data"].get("description"),
54-
example=FIELD_DESCRIPTIONS["data"].get("example"),
54+
examples=[FIELD_DESCRIPTIONS["data"].get("example")],
5555
default=None,
5656
)
5757
source: str = Field(
5858
title=FIELD_DESCRIPTIONS["source"].get("title"),
5959
description=FIELD_DESCRIPTIONS["source"].get("description"),
60-
example=FIELD_DESCRIPTIONS["source"].get("example"),
60+
examples=[FIELD_DESCRIPTIONS["source"].get("example")],
6161
)
6262
id: str = Field(
6363
title=FIELD_DESCRIPTIONS["id"].get("title"),
6464
description=FIELD_DESCRIPTIONS["id"].get("description"),
65-
example=FIELD_DESCRIPTIONS["id"].get("example"),
65+
examples=[FIELD_DESCRIPTIONS["id"].get("example")],
6666
default_factory=attribute.default_id_selection_algorithm,
6767
)
6868
type: str = Field(
6969
title=FIELD_DESCRIPTIONS["type"].get("title"),
7070
description=FIELD_DESCRIPTIONS["type"].get("description"),
71-
example=FIELD_DESCRIPTIONS["type"].get("example"),
71+
examples=[FIELD_DESCRIPTIONS["type"].get("example")],
7272
)
7373
specversion: attribute.SpecVersion = Field(
7474
title=FIELD_DESCRIPTIONS["specversion"].get("title"),
7575
description=FIELD_DESCRIPTIONS["specversion"].get("description"),
76-
example=FIELD_DESCRIPTIONS["specversion"].get("example"),
76+
examples=[FIELD_DESCRIPTIONS["specversion"].get("example")],
7777
default=attribute.DEFAULT_SPECVERSION,
7878
)
7979
time: typing.Optional[datetime.datetime] = Field(
8080
title=FIELD_DESCRIPTIONS["time"].get("title"),
8181
description=FIELD_DESCRIPTIONS["time"].get("description"),
82-
example=FIELD_DESCRIPTIONS["time"].get("example"),
82+
examples=[FIELD_DESCRIPTIONS["time"].get("example")],
8383
default_factory=attribute.default_time_selection_algorithm,
8484
)
8585
subject: typing.Optional[str] = Field(
8686
title=FIELD_DESCRIPTIONS["subject"].get("title"),
8787
description=FIELD_DESCRIPTIONS["subject"].get("description"),
88-
example=FIELD_DESCRIPTIONS["subject"].get("example"),
88+
examples=[FIELD_DESCRIPTIONS["subject"].get("example")],
8989
default=None,
9090
)
9191
datacontenttype: typing.Optional[str] = Field(
9292
title=FIELD_DESCRIPTIONS["datacontenttype"].get("title"),
9393
description=FIELD_DESCRIPTIONS["datacontenttype"].get("description"),
94-
example=FIELD_DESCRIPTIONS["datacontenttype"].get("example"),
94+
examples=[FIELD_DESCRIPTIONS["datacontenttype"].get("example")],
9595
default=None,
9696
)
9797
dataschema: typing.Optional[str] = Field(
9898
title=FIELD_DESCRIPTIONS["dataschema"].get("title"),
9999
description=FIELD_DESCRIPTIONS["dataschema"].get("description"),
100-
example=FIELD_DESCRIPTIONS["dataschema"].get("example"),
100+
examples=[FIELD_DESCRIPTIONS["dataschema"].get("example")],
101101
default=None,
102102
)
103103

0 commit comments

Comments
 (0)