From 47c08abc1c6890fb9fb9582aa2402b3d14b28328 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Wed, 25 Oct 2023 16:38:54 -0400 Subject: [PATCH 1/2] remove usage of dbt.config.PartialProject from dbt/adapters --- core/dbt/adapters/base/plugin.py | 15 ++------- tests/unit/test_adapter_factory.py | 53 +++++++++++++++--------------- tests/unit/test_manifest.py | 37 ++++++++++----------- 3 files changed, 47 insertions(+), 58 deletions(-) diff --git a/core/dbt/adapters/base/plugin.py b/core/dbt/adapters/base/plugin.py index 5faa2163a4a..dc41fb68110 100644 --- a/core/dbt/adapters/base/plugin.py +++ b/core/dbt/adapters/base/plugin.py @@ -1,20 +1,10 @@ from typing import List, Optional, Type +from pathlib import Path from dbt.adapters.base import Credentials -from dbt.exceptions import CompilationError from dbt.adapters.protocol import AdapterProtocol -def project_name_from_path(include_path: str) -> str: - # avoid an import cycle - from dbt.config.project import PartialProject - - partial = PartialProject.from_project_root(include_path) - if partial.project_name is None: - raise CompilationError(f"Invalid project at {include_path}: name not set!") - return partial.project_name - - class AdapterPlugin: """Defines the basic requirements for a dbt adapter plugin. @@ -29,12 +19,13 @@ def __init__( credentials: Type[Credentials], include_path: str, dependencies: Optional[List[str]] = None, + project_name: Optional[str] = None, ) -> None: self.adapter: Type[AdapterProtocol] = adapter self.credentials: Type[Credentials] = credentials self.include_path: str = include_path - self.project_name: str = project_name_from_path(include_path) + self.project_name: str = project_name or f"dbt_{Path(include_path).name}" self.dependencies: List[str] if dependencies is None: self.dependencies = [] diff --git a/tests/unit/test_adapter_factory.py b/tests/unit/test_adapter_factory.py index 366e7b32e3d..c67b61d7fc0 100644 --- a/tests/unit/test_adapter_factory.py +++ b/tests/unit/test_adapter_factory.py @@ -10,33 +10,32 @@ class TestGetPackageNames(unittest.TestCase): def setUp(self): - with mock.patch("dbt.adapters.base.plugin.project_name_from_path") as get_name: - get_name.return_value = "root" - self.root_plugin = AdapterPlugin( - adapter=mock.MagicMock(), - credentials=mock.MagicMock(), - include_path="/path/to/root/plugin", - dependencies=["childa", "childb"], - ) - get_name.return_value = "pkg_childa" - self.childa = AdapterPlugin( - adapter=mock.MagicMock(), - credentials=mock.MagicMock(), - include_path="/path/to/childa", - ) - get_name.return_value = "pkg_childb" - self.childb = AdapterPlugin( - adapter=mock.MagicMock(), - credentials=mock.MagicMock(), - include_path="/path/to/childb", - dependencies=["childc"], - ) - get_name.return_value = "pkg_childc" - self.childc = AdapterPlugin( - adapter=mock.MagicMock(), - credentials=mock.MagicMock(), - include_path="/path/to/childc", - ) + self.root_plugin = AdapterPlugin( + adapter=mock.MagicMock(), + credentials=mock.MagicMock(), + include_path="/path/to/root/plugin", + dependencies=["childa", "childb"], + project_name="root", + ) + self.childa = AdapterPlugin( + adapter=mock.MagicMock(), + credentials=mock.MagicMock(), + include_path="/path/to/childa", + project_name="pkg_childa", + ) + self.childb = AdapterPlugin( + adapter=mock.MagicMock(), + credentials=mock.MagicMock(), + include_path="/path/to/childb", + dependencies=["childc"], + project_name="pkg_childb", + ) + self.childc = AdapterPlugin( + adapter=mock.MagicMock(), + credentials=mock.MagicMock(), + include_path="/path/to/childc", + project_name="pkg_childc", + ) self._mock_modules = { "root": self.root_plugin, diff --git a/tests/unit/test_manifest.py b/tests/unit/test_manifest.py index 22af78e5623..c85895349a5 100644 --- a/tests/unit/test_manifest.py +++ b/tests/unit/test_manifest.py @@ -1246,25 +1246,24 @@ def test_find_generate_macros_by_name(macros, expectations): def _materialization_parameter_sets(): # inject the plugins used for materialization parameter tests - with mock.patch("dbt.adapters.base.plugin.project_name_from_path") as get_name: - get_name.return_value = "foo" - FooPlugin = AdapterPlugin( - adapter=mock.MagicMock(), - credentials=mock.MagicMock(), - include_path="/path/to/root/plugin", - ) - FooPlugin.adapter.type.return_value = "foo" - inject_plugin(FooPlugin) - - get_name.return_value = "bar" - BarPlugin = AdapterPlugin( - adapter=mock.MagicMock(), - credentials=mock.MagicMock(), - include_path="/path/to/root/plugin", - dependencies=["foo"], - ) - BarPlugin.adapter.type.return_value = "bar" - inject_plugin(BarPlugin) + FooPlugin = AdapterPlugin( + adapter=mock.MagicMock(), + credentials=mock.MagicMock(), + include_path="/path/to/root/plugin", + project_name="foo", + ) + FooPlugin.adapter.type.return_value = "foo" + inject_plugin(FooPlugin) + + BarPlugin = AdapterPlugin( + adapter=mock.MagicMock(), + credentials=mock.MagicMock(), + include_path="/path/to/root/plugin", + dependencies=["foo"], + project_name="bar", + ) + BarPlugin.adapter.type.return_value = "bar" + inject_plugin(BarPlugin) sets = [ FindMaterializationSpec(macros=[], adapter_type="foo", expected=None), From 1db60c55b23c0f0c30bb21b97b138f325fc4feaf Mon Sep 17 00:00:00 2001 From: Colin Date: Thu, 26 Oct 2023 12:39:20 -0700 Subject: [PATCH 2/2] add changie --- .changes/unreleased/Features-20231026-123913.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Features-20231026-123913.yaml diff --git a/.changes/unreleased/Features-20231026-123913.yaml b/.changes/unreleased/Features-20231026-123913.yaml new file mode 100644 index 00000000000..d3ada7cb691 --- /dev/null +++ b/.changes/unreleased/Features-20231026-123913.yaml @@ -0,0 +1,6 @@ +kind: Features +body: remove usage of dbt.config.PartialProject from dbt/adapters +time: 2023-10-26T12:39:13.904116-07:00 +custom: + Author: MichelleArk + Issue: "8928"