1818from cloudevents .exceptions import PydanticFeatureNotInstalled
1919
2020try :
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 # type: ignore
2228except ImportError : # pragma: no cover # hard to test
2329 raise PydanticFeatureNotInstalled (
2430 "CloudEvents pydantic feature is not installed. "
@@ -84,7 +90,7 @@ def _ce_json_loads( # type: ignore[no-untyped-def]
8490 return conversion .to_dict (http .from_json (data ))
8591
8692
87- class CloudEvent (abstract .CloudEvent , pydantic . BaseModel ): # type: ignore
93+ class CloudEvent (abstract .CloudEvent , BaseModel ): # type: ignore
8894 """
8995 A Python-friendly CloudEvent representation backed by Pydantic-modeled fields.
9096
@@ -97,7 +103,7 @@ def create(
97103 ) -> "CloudEvent" :
98104 return cls (attributes , data )
99105
100- data : typing .Optional [typing .Any ] = pydantic . Field (
106+ data : typing .Optional [typing .Any ] = Field (
101107 title = "Event Data" ,
102108 description = (
103109 "CloudEvents MAY include domain-specific information about the occurrence."
@@ -107,7 +113,7 @@ def create(
107113 " when those respective attributes are present."
108114 ),
109115 )
110- source : str = pydantic . Field (
116+ source : str = Field (
111117 title = "Event Source" ,
112118 description = (
113119 "Identifies the context in which an event happened. Often this will include"
@@ -132,7 +138,7 @@ def create(
132138 example = "https://github.com/cloudevents" ,
133139 )
134140
135- id : str = pydantic . Field (
141+ id : str = Field (
136142 default_factory = attribute .default_id_selection_algorithm ,
137143 title = "Event ID" ,
138144 description = (
@@ -144,7 +150,7 @@ def create(
144150 ),
145151 example = "A234-1234-1234" ,
146152 )
147- type : str = pydantic . Field (
153+ type : str = Field (
148154 title = "Event Type" ,
149155 description = (
150156 "This attribute contains a value describing the type of event related to"
@@ -154,7 +160,7 @@ def create(
154160 ),
155161 example = "com.github.pull_request.opened" ,
156162 )
157- specversion : attribute .SpecVersion = pydantic . Field (
163+ specversion : attribute .SpecVersion = Field (
158164 default = attribute .DEFAULT_SPECVERSION ,
159165 title = "Specification Version" ,
160166 description = (
@@ -168,7 +174,7 @@ def create(
168174 ),
169175 example = attribute .DEFAULT_SPECVERSION ,
170176 )
171- time : typing .Optional [datetime .datetime ] = pydantic . Field (
177+ time : typing .Optional [datetime .datetime ] = Field (
172178 default_factory = attribute .default_time_selection_algorithm ,
173179 title = "Occurrence Time" ,
174180 description = (
@@ -182,7 +188,7 @@ def create(
182188 example = "2018-04-05T17:31:00Z" ,
183189 )
184190
185- subject : typing .Optional [str ] = pydantic . Field (
191+ subject : typing .Optional [str ] = Field (
186192 title = "Event Subject" ,
187193 description = (
188194 "This describes the subject of the event in the context of the event"
@@ -202,7 +208,7 @@ def create(
202208 ),
203209 example = "123" ,
204210 )
205- datacontenttype : typing .Optional [str ] = pydantic . Field (
211+ datacontenttype : typing .Optional [str ] = Field (
206212 title = "Event Data Content Type" ,
207213 description = (
208214 "Content type of data value. This attribute enables data to carry any type"
@@ -211,7 +217,7 @@ def create(
211217 ),
212218 example = "text/xml" ,
213219 )
214- dataschema : typing .Optional [str ] = pydantic . Field (
220+ dataschema : typing .Optional [str ] = Field (
215221 title = "Event Data Schema" ,
216222 description = (
217223 "Identifies the schema that data adheres to. "
0 commit comments