From e01eb30884571d7af1e2c313c6a4d2f8ca1e1387 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Wed, 6 Dec 2023 10:34:56 +0900 Subject: [PATCH] Remove usage of dbt.contracts.relation in dbt/adapters (#9207) --- .../Under the Hood-20231205-120559.yaml | 6 ++++++ core/dbt/adapters/base/relation.py | 2 +- core/dbt/{ => adapters}/contracts/relation.py | 5 ++--- core/dbt/adapters/protocol.py | 2 +- core/dbt/common/exceptions/__init__.py | 1 + core/dbt/common/exceptions/contracts.py | 17 +++++++++++++++++ core/dbt/config/runtime.py | 6 +++--- core/dbt/context/exceptions_jinja.py | 8 ++++++-- core/dbt/contracts/util.py | 6 +----- core/dbt/exceptions.py | 15 --------------- .../postgres/dbt/adapters/postgres/relation.py | 2 +- .../tests/adapter/materialized_view/basic.py | 2 +- .../tests/adapter/materialized_view/changes.py | 2 +- tests/unit/test_postgres_adapter.py | 2 +- tests/unit/test_relation.py | 2 +- 15 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 .changes/unreleased/Under the Hood-20231205-120559.yaml rename core/dbt/{ => adapters}/contracts/relation.py (96%) create mode 100644 core/dbt/common/exceptions/contracts.py diff --git a/.changes/unreleased/Under the Hood-20231205-120559.yaml b/.changes/unreleased/Under the Hood-20231205-120559.yaml new file mode 100644 index 00000000000..a209bda9f6c --- /dev/null +++ b/.changes/unreleased/Under the Hood-20231205-120559.yaml @@ -0,0 +1,6 @@ +kind: Under the Hood +body: Remove usage of dbt.contracts in dbt/adapters +time: 2023-12-05T12:05:59.936775+09:00 +custom: + Author: michelleark + Issue: "9208" diff --git a/core/dbt/adapters/base/relation.py b/core/dbt/adapters/base/relation.py index 040d5e94442..70a01398f0d 100644 --- a/core/dbt/adapters/base/relation.py +++ b/core/dbt/adapters/base/relation.py @@ -3,7 +3,7 @@ from typing import Optional, TypeVar, Any, Type, Dict, Iterator, Tuple, Set, Union, FrozenSet from dbt.contracts.graph.nodes import SourceDefinition, ManifestNode, ResultNode, ParsedNode -from dbt.contracts.relation import ( +from dbt.adapters.contracts.relation import ( RelationType, ComponentName, HasQuoting, diff --git a/core/dbt/contracts/relation.py b/core/dbt/adapters/contracts/relation.py similarity index 96% rename from core/dbt/contracts/relation.py rename to core/dbt/adapters/contracts/relation.py index 62a62d814ee..c4cead46e45 100644 --- a/core/dbt/contracts/relation.py +++ b/core/dbt/adapters/contracts/relation.py @@ -8,9 +8,8 @@ from dbt.common.dataclass_schema import dbtClassMixin, StrEnum -from dbt.contracts.util import Replaceable -from dbt.common.exceptions import CompilationError -from dbt.exceptions import DataclassNotDictError +from dbt.common.contracts.util import Replaceable +from dbt.common.exceptions import CompilationError, DataclassNotDictError from dbt.common.utils import deep_merge diff --git a/core/dbt/adapters/protocol.py b/core/dbt/adapters/protocol.py index 04c1b2fd0c5..45d86bcc307 100644 --- a/core/dbt/adapters/protocol.py +++ b/core/dbt/adapters/protocol.py @@ -14,10 +14,10 @@ import agate from dbt.adapters.contracts.connection import Connection, AdapterRequiredConfig, AdapterResponse +from dbt.adapters.contracts.relation import Policy, HasQuoting from dbt.contracts.graph.nodes import ResultNode from dbt.contracts.graph.model_config import BaseConfig from dbt.contracts.graph.manifest import Manifest -from dbt.contracts.relation import Policy, HasQuoting @dataclass diff --git a/core/dbt/common/exceptions/__init__.py b/core/dbt/common/exceptions/__init__.py index 208ba24dbf7..da49548aa9d 100644 --- a/core/dbt/common/exceptions/__init__.py +++ b/core/dbt/common/exceptions/__init__.py @@ -1,3 +1,4 @@ from dbt.common.exceptions.base import * # noqa from dbt.common.exceptions.events import * # noqa from dbt.common.exceptions.macros import * # noqa +from dbt.common.exceptions.contracts import * # noqa diff --git a/core/dbt/common/exceptions/contracts.py b/core/dbt/common/exceptions/contracts.py new file mode 100644 index 00000000000..6b32793dea5 --- /dev/null +++ b/core/dbt/common/exceptions/contracts.py @@ -0,0 +1,17 @@ +from typing import Any +from dbt.common.exceptions import CompilationError + + +# this is part of the context and also raised in dbt.contracts.relation.py +class DataclassNotDictError(CompilationError): + def __init__(self, obj: Any): + self.obj = obj + super().__init__(msg=self.get_message()) + + def get_message(self) -> str: + msg = ( + f'The object ("{self.obj}") was used as a dictionary. This ' + "capability has been removed from objects of this type." + ) + + return msg diff --git a/core/dbt/config/runtime.py b/core/dbt/config/runtime.py index 2a66f2f31b5..76b10e009da 100644 --- a/core/dbt/config/runtime.py +++ b/core/dbt/config/runtime.py @@ -15,13 +15,13 @@ Type, ) -from dbt.flags import get_flags from dbt.adapters.factory import get_include_paths, get_relation_class_by_name -from dbt.config.project import load_raw_project from dbt.adapters.contracts.connection import AdapterRequiredConfig, Credentials, HasCredentials +from dbt.adapters.contracts.relation import ComponentName +from dbt.flags import get_flags +from dbt.config.project import load_raw_project from dbt.contracts.graph.manifest import ManifestMetadata from dbt.contracts.project import Configuration, UserConfig -from dbt.contracts.relation import ComponentName from dbt.common.dataclass_schema import ValidationError from dbt.common.events.functions import warn_or_error from dbt.common.events.types import UnusedResourceConfigPath diff --git a/core/dbt/context/exceptions_jinja.py b/core/dbt/context/exceptions_jinja.py index 87d7977982f..87e79fea3ad 100644 --- a/core/dbt/context/exceptions_jinja.py +++ b/core/dbt/context/exceptions_jinja.py @@ -4,7 +4,12 @@ from dbt.common.events.functions import warn_or_error from dbt.common.events.types import JinjaLogWarning -from dbt.common.exceptions import DbtRuntimeError, NotImplementedError, DbtDatabaseError +from dbt.common.exceptions import ( + DbtRuntimeError, + NotImplementedError, + DbtDatabaseError, + DataclassNotDictError, +) from dbt.adapters.exceptions import ( MissingConfigError, ColumnTypeMissingError, @@ -15,7 +20,6 @@ MissingRelationError, AmbiguousAliasError, AmbiguousCatalogMatchError, - DataclassNotDictError, CompilationError, DependencyNotFoundError, DependencyError, diff --git a/core/dbt/contracts/util.py b/core/dbt/contracts/util.py index c22efbd1220..7c33c18dca1 100644 --- a/core/dbt/contracts/util.py +++ b/core/dbt/contracts/util.py @@ -10,6 +10,7 @@ ) from dbt.version import __version__ +from dbt.common.contracts.util import Replaceable from dbt.common.events.functions import get_metadata_vars from dbt.common.invocation import get_invocation_id from dbt.common.dataclass_schema import dbtClassMixin @@ -41,11 +42,6 @@ class Foo: return [] -class Replaceable: - def replace(self, **kwargs): - return dataclasses.replace(self, **kwargs) - - class Mergeable(Replaceable): def merged(self, *args): """Perform a shallow merge, where the last non-None write wins. This is diff --git a/core/dbt/exceptions.py b/core/dbt/exceptions.py index e3ad3a48163..d08b03a257b 100644 --- a/core/dbt/exceptions.py +++ b/core/dbt/exceptions.py @@ -1363,21 +1363,6 @@ def get_message(self) -> str: return msg -# this is part of the context and also raised in dbt.contracts.relation.py -class DataclassNotDictError(CompilationError): - def __init__(self, obj: Any): - self.obj = obj - super().__init__(msg=self.get_message()) - - def get_message(self) -> str: - msg = ( - f'The object ("{self.obj}") was used as a dictionary. This ' - "capability has been removed from objects of this type." - ) - - return msg - - class DependencyNotFoundError(CompilationError): def __init__(self, node, node_description, required_pkg): self.node = node diff --git a/plugins/postgres/dbt/adapters/postgres/relation.py b/plugins/postgres/dbt/adapters/postgres/relation.py index fbb358cde43..5c76052ebf4 100644 --- a/plugins/postgres/dbt/adapters/postgres/relation.py +++ b/plugins/postgres/dbt/adapters/postgres/relation.py @@ -7,7 +7,7 @@ RelationResults, ) from dbt.context.providers import RuntimeConfigObject -from dbt.contracts.relation import RelationType +from dbt.adapters.contracts.relation import RelationType from dbt.common.exceptions import DbtRuntimeError from dbt.adapters.postgres.relation_configs import ( diff --git a/tests/adapter/dbt/tests/adapter/materialized_view/basic.py b/tests/adapter/dbt/tests/adapter/materialized_view/basic.py index ec90d503650..9720945ba50 100644 --- a/tests/adapter/dbt/tests/adapter/materialized_view/basic.py +++ b/tests/adapter/dbt/tests/adapter/materialized_view/basic.py @@ -3,7 +3,7 @@ import pytest from dbt.adapters.base.relation import BaseRelation -from dbt.contracts.relation import RelationType +from dbt.adapters.contracts.relation import RelationType from dbt.tests.util import ( assert_message_in_logs, get_model_file, diff --git a/tests/adapter/dbt/tests/adapter/materialized_view/changes.py b/tests/adapter/dbt/tests/adapter/materialized_view/changes.py index 5fc933fbe0d..b31149a5ac2 100644 --- a/tests/adapter/dbt/tests/adapter/materialized_view/changes.py +++ b/tests/adapter/dbt/tests/adapter/materialized_view/changes.py @@ -4,7 +4,7 @@ from dbt.adapters.base.relation import BaseRelation from dbt.contracts.graph.model_config import OnConfigurationChangeOption -from dbt.contracts.relation import RelationType +from dbt.adapters.contracts.relation import RelationType from dbt.tests.util import ( assert_message_in_logs, get_model_file, diff --git a/tests/unit/test_postgres_adapter.py b/tests/unit/test_postgres_adapter.py index 8739e3e2784..1dfb7a9146a 100644 --- a/tests/unit/test_postgres_adapter.py +++ b/tests/unit/test_postgres_adapter.py @@ -7,7 +7,7 @@ from unittest import mock from dbt.adapters.base import BaseRelation -from dbt.contracts.relation import Path +from dbt.adapters.contracts.relation import Path from dbt.task.debug import DebugTask from dbt.adapters.base.query_headers import MacroQueryStringSetter diff --git a/tests/unit/test_relation.py b/tests/unit/test_relation.py index 94995958ba6..5a5c58de7a5 100644 --- a/tests/unit/test_relation.py +++ b/tests/unit/test_relation.py @@ -3,7 +3,7 @@ import pytest from dbt.adapters.base import BaseRelation -from dbt.contracts.relation import RelationType +from dbt.adapters.contracts.relation import RelationType @pytest.mark.parametrize(