18
18
from cloudevents .exceptions import PydanticFeatureNotInstalled
19
19
20
20
try :
21
- import pydantic
21
+ from pydantic import VERSION as PYDANTIC_VERSION
22
+
23
+ pydantic_major_version = PYDANTIC_VERSION .split ("." )[0 ]
24
+ if pydantic_major_version == "2" :
25
+ from pydantic .v1 import BaseModel , Field
26
+ else :
27
+ from pydantic import BaseModel , Field
22
28
except ImportError : # pragma: no cover # hard to test
23
29
raise PydanticFeatureNotInstalled (
24
30
"CloudEvents pydantic feature is not installed. "
@@ -84,7 +90,7 @@ def _ce_json_loads( # type: ignore[no-untyped-def]
84
90
return conversion .to_dict (http .from_json (data ))
85
91
86
92
87
- class CloudEvent (abstract .CloudEvent , pydantic . BaseModel ): # type: ignore
93
+ class CloudEvent (abstract .CloudEvent , BaseModel ): # type: ignore
88
94
"""
89
95
A Python-friendly CloudEvent representation backed by Pydantic-modeled fields.
90
96
@@ -97,7 +103,7 @@ def create(
97
103
) -> "CloudEvent" :
98
104
return cls (attributes , data )
99
105
100
- data : typing .Optional [typing .Any ] = pydantic . Field (
106
+ data : typing .Optional [typing .Any ] = Field (
101
107
title = "Event Data" ,
102
108
description = (
103
109
"CloudEvents MAY include domain-specific information about the occurrence."
@@ -107,7 +113,7 @@ def create(
107
113
" when those respective attributes are present."
108
114
),
109
115
)
110
- source : str = pydantic . Field (
116
+ source : str = Field (
111
117
title = "Event Source" ,
112
118
description = (
113
119
"Identifies the context in which an event happened. Often this will include"
@@ -132,7 +138,7 @@ def create(
132
138
example = "https://github.com/cloudevents" ,
133
139
)
134
140
135
- id : str = pydantic . Field (
141
+ id : str = Field (
136
142
default_factory = attribute .default_id_selection_algorithm ,
137
143
title = "Event ID" ,
138
144
description = (
@@ -144,7 +150,7 @@ def create(
144
150
),
145
151
example = "A234-1234-1234" ,
146
152
)
147
- type : str = pydantic . Field (
153
+ type : str = Field (
148
154
title = "Event Type" ,
149
155
description = (
150
156
"This attribute contains a value describing the type of event related to"
@@ -154,7 +160,7 @@ def create(
154
160
),
155
161
example = "com.github.pull_request.opened" ,
156
162
)
157
- specversion : attribute .SpecVersion = pydantic . Field (
163
+ specversion : attribute .SpecVersion = Field (
158
164
default = attribute .DEFAULT_SPECVERSION ,
159
165
title = "Specification Version" ,
160
166
description = (
@@ -168,7 +174,7 @@ def create(
168
174
),
169
175
example = attribute .DEFAULT_SPECVERSION ,
170
176
)
171
- time : typing .Optional [datetime .datetime ] = pydantic . Field (
177
+ time : typing .Optional [datetime .datetime ] = Field (
172
178
default_factory = attribute .default_time_selection_algorithm ,
173
179
title = "Occurrence Time" ,
174
180
description = (
@@ -182,7 +188,7 @@ def create(
182
188
example = "2018-04-05T17:31:00Z" ,
183
189
)
184
190
185
- subject : typing .Optional [str ] = pydantic . Field (
191
+ subject : typing .Optional [str ] = Field (
186
192
title = "Event Subject" ,
187
193
description = (
188
194
"This describes the subject of the event in the context of the event"
@@ -202,7 +208,7 @@ def create(
202
208
),
203
209
example = "123" ,
204
210
)
205
- datacontenttype : typing .Optional [str ] = pydantic . Field (
211
+ datacontenttype : typing .Optional [str ] = Field (
206
212
title = "Event Data Content Type" ,
207
213
description = (
208
214
"Content type of data value. This attribute enables data to carry any type"
@@ -211,7 +217,7 @@ def create(
211
217
),
212
218
example = "text/xml" ,
213
219
)
214
- dataschema : typing .Optional [str ] = pydantic . Field (
220
+ dataschema : typing .Optional [str ] = Field (
215
221
title = "Event Data Schema" ,
216
222
description = (
217
223
"Identifies the schema that data adheres to. "
0 commit comments