diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 15ab654..df163d3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -24,4 +24,4 @@ repos: types: [ python ] args: [ ] additional_dependencies: - - "pydantic" + - "pydantic~=2.7" diff --git a/cloudevents/pydantic/__init__.py b/cloudevents/pydantic/__init__.py index 409eb44..f8556ca 100644 --- a/cloudevents/pydantic/__init__.py +++ b/cloudevents/pydantic/__init__.py @@ -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(