diff --git a/.changes/unreleased/Dependencies-20231213-113016.yaml b/.changes/unreleased/Dependencies-20231213-113016.yaml new file mode 100644 index 00000000..d69adce5 --- /dev/null +++ b/.changes/unreleased/Dependencies-20231213-113016.yaml @@ -0,0 +1,7 @@ +kind: Dependencies +body: Relax Pydantic dependency to allow DSI to be installed in environments alongside + Pydantic v1 or v2 +time: 2023-12-13T11:30:16.376387Z +custom: + Author: bernardcooke53 + PR: "227" diff --git a/dbt_semantic_interfaces/dataclass_serialization.py b/dbt_semantic_interfaces/dataclass_serialization.py index 44ada7b1..1953ed08 100644 --- a/dbt_semantic_interfaces/dataclass_serialization.py +++ b/dbt_semantic_interfaces/dataclass_serialization.py @@ -19,8 +19,16 @@ get_type_hints, ) -import pydantic -from pydantic import BaseModel +try: + import pydantic.v1 as pydantic +except (ImportError, ModuleNotFoundError): + import pydantic + +try: + from pydantic.v1 import BaseModel # pydantic v2 +except ModuleNotFoundError: + from pydantic import BaseModel # pydantic v1 + from typing_extensions import TypeAlias from dbt_semantic_interfaces.pretty_print import pformat_big_objects diff --git a/dbt_semantic_interfaces/implementations/base.py b/dbt_semantic_interfaces/implementations/base.py index 31c9faeb..cf469ffc 100644 --- a/dbt_semantic_interfaces/implementations/base.py +++ b/dbt_semantic_interfaces/implementations/base.py @@ -5,7 +5,10 @@ from abc import ABC, abstractmethod from typing import Any, Callable, ClassVar, Generator, Generic, Type, TypeVar -from pydantic import BaseModel, root_validator +try: + from pydantic.v1 import BaseModel, root_validator # pydantic v2 +except ModuleNotFoundError: + from pydantic import BaseModel, root_validator # pydantic v1 from dbt_semantic_interfaces.errors import ParsingException from dbt_semantic_interfaces.parsing.yaml_loader import ( diff --git a/dbt_semantic_interfaces/implementations/export.py b/dbt_semantic_interfaces/implementations/export.py index 7a06d1b0..1263accf 100644 --- a/dbt_semantic_interfaces/implementations/export.py +++ b/dbt_semantic_interfaces/implementations/export.py @@ -2,7 +2,11 @@ from typing import Optional -from pydantic import Field +try: + from pydantic.v1 import Field # pydantic v2 +except ModuleNotFoundError: + from pydantic import Field # pydantic v1 + from typing_extensions import override from dbt_semantic_interfaces.implementations.base import HashableBaseModel diff --git a/dbt_semantic_interfaces/implementations/metric.py b/dbt_semantic_interfaces/implementations/metric.py index 366648a4..ad42d710 100644 --- a/dbt_semantic_interfaces/implementations/metric.py +++ b/dbt_semantic_interfaces/implementations/metric.py @@ -2,7 +2,10 @@ from typing import List, Optional, Sequence -from pydantic import Field +try: + from pydantic.v1 import Field # pydantic v2 +except ModuleNotFoundError: + from pydantic import Field # pydantic v1 from dbt_semantic_interfaces.enum_extension import assert_values_exhausted from dbt_semantic_interfaces.errors import ParsingException diff --git a/dbt_semantic_interfaces/implementations/project_configuration.py b/dbt_semantic_interfaces/implementations/project_configuration.py index 3e52fd91..ffacc4d9 100644 --- a/dbt_semantic_interfaces/implementations/project_configuration.py +++ b/dbt_semantic_interfaces/implementations/project_configuration.py @@ -3,7 +3,12 @@ from typing import List, Optional from importlib_metadata import version -from pydantic import validator + +try: + from pydantic.v1 import validator # pydantic v2 +except ModuleNotFoundError: + from pydantic import validator # pydantic v1 + from typing_extensions import override from dbt_semantic_interfaces.implementations.base import ( diff --git a/dbt_semantic_interfaces/implementations/semantic_model.py b/dbt_semantic_interfaces/implementations/semantic_model.py index 45b90ae0..7b6984d0 100644 --- a/dbt_semantic_interfaces/implementations/semantic_model.py +++ b/dbt_semantic_interfaces/implementations/semantic_model.py @@ -2,7 +2,11 @@ from typing import Any, List, Optional, Sequence -from pydantic import validator +try: + from pydantic.v1 import validator # pydantic v2 +except ModuleNotFoundError: + from pydantic import validator # pydantic v1 + from typing_extensions import override from dbt_semantic_interfaces.implementations.base import ( diff --git a/dbt_semantic_interfaces/validations/validator_helpers.py b/dbt_semantic_interfaces/validations/validator_helpers.py index 6ea60f48..35e2b9c9 100644 --- a/dbt_semantic_interfaces/validations/validator_helpers.py +++ b/dbt_semantic_interfaces/validations/validator_helpers.py @@ -20,7 +20,11 @@ ) import click -from pydantic import BaseModel, Extra + +try: + from pydantic.v1 import BaseModel, Extra # pydantic v2 +except ModuleNotFoundError: + from pydantic import BaseModel, Extra # pydantic v1 from dbt_semantic_interfaces.implementations.base import FrozenBaseModel from dbt_semantic_interfaces.protocols import Metadata, SemanticManifestT, SemanticModel diff --git a/mypy.ini b/mypy.ini index 6d2a7c15..8701113b 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,6 +1,9 @@ [mypy] plugins = pydantic.mypy +[mypy-pydantic.*] +ignore_missing_imports = True + [pydantic-mypy] init_forbid_extra = True init_typed = True diff --git a/pyproject.toml b/pyproject.toml index a892f39d..c133e501 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,7 +20,7 @@ classifiers = [ "Programming Language :: Python :: Implementation :: PyPy", ] dependencies = [ - "pydantic~=1.10", + "pydantic>=1.10,<3.0", "jsonschema~=4.0", "PyYAML~=6.0", "more-itertools>=8.0,<11.0",