When type checking, uses the latest (V2) version of Pydantic

Signed-off-by: Fabio Batista <fabio@atelie.dev.br>
This commit is contained in:
Fabio Batista 2024-05-24 22:19:35 -03:00
parent 1f95af98ad
commit c231975efe
No known key found for this signature in database
GPG Key ID: 6CD55257DB018B72
2 changed files with 22 additions and 13 deletions

View File

@ -24,4 +24,4 @@ repos:
types: [ python ]
args: [ ]
additional_dependencies:
- "pydantic"
- "pydantic~=2.7"

View File

@ -12,22 +12,31 @@
# License for the specific language governing permissions and limitations
# under the License.
from typing import TYPE_CHECKING
from cloudevents.exceptions import PydanticFeatureNotInstalled
try:
from pydantic import VERSION as PYDANTIC_VERSION
pydantic_major_version = PYDANTIC_VERSION.split(".")[0]
if pydantic_major_version == "1":
from cloudevents.pydantic.v1 import CloudEvent, from_dict, from_http, from_json
if TYPE_CHECKING:
from cloudevents.pydantic.v2 import CloudEvent, from_dict, from_http, from_json
else:
from cloudevents.pydantic.v2 import ( # type: ignore
CloudEvent,
from_dict,
from_http,
from_json,
)
from pydantic import VERSION as PYDANTIC_VERSION
pydantic_major_version = PYDANTIC_VERSION.split(".")[0]
if pydantic_major_version == "1":
from cloudevents.pydantic.v1 import (
CloudEvent,
from_dict,
from_http,
from_json,
)
else:
from cloudevents.pydantic.v2 import (
CloudEvent,
from_dict,
from_http,
from_json,
)
except ImportError: # pragma: no cover # hard to test
raise PydanticFeatureNotInstalled(