Skip to content

Commit

Permalink
add py.typed
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-rogers-dbt committed Oct 1, 2024
1 parent a86e2b4 commit 666cc3b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
26 changes: 26 additions & 0 deletions core/dbt/config/external_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import Optional

from dbt_config.external_config import ExternalCatalogConfig

from dbt.clients.yaml_helper import load_yaml_text
from dbt.constants import EXTERNAL_CATALOG_FILE_NAME
from dbt_common.clients.system import load_file_contents, path_exists


def _load_yaml(path):
contents = load_file_contents(path)
return load_yaml_text(contents)


def _load_yml_dict(file_path):
if path_exists(file_path):
ret = _load_yaml(file_path) or {}
return ret
return None


def load_external_catalog_config(project_root) -> Optional[ExternalCatalogConfig]:
unparsed_config = _load_yml_dict(f"{project_root}/{EXTERNAL_CATALOG_FILE_NAME}")
if unparsed_config is not None:
return ExternalCatalogConfig.model_validate(unparsed_config)
return None
18 changes: 15 additions & 3 deletions core/dbt/config/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
Type,
)

from dbt_config.external_config import ExternalCatalogConfig

from dbt import tracking
from dbt.adapters.contracts.connection import (
AdapterRequiredConfig,
Expand All @@ -39,6 +41,7 @@
from dbt_common.events.functions import warn_or_error
from dbt_common.helper_types import DictDefaultEmptyStr, FQNPath, PathSet

from .external_config import load_external_catalog_config
from .profile import Profile
from .project import Project
from .renderer import DbtProjectYamlRenderer, ProfileRenderer
Expand Down Expand Up @@ -98,6 +101,7 @@ class RuntimeConfig(Project, Profile, AdapterRequiredConfig):
profile_name: str
cli_vars: Dict[str, Any]
dependencies: Optional[Mapping[str, "RuntimeConfig"]] = None
catalogs: Optional[ExternalCatalogConfig] = None

def __post_init__(self):
self.validate()
Expand Down Expand Up @@ -125,12 +129,15 @@ def from_parts(
profile: Profile,
args: Any,
dependencies: Optional[Mapping[str, "RuntimeConfig"]] = None,
catalogs: Optional[ExternalCatalogConfig] = None,
) -> "RuntimeConfig":
"""Instantiate a RuntimeConfig from its components.
:param profile: A parsed dbt Profile.
:param project: A parsed dbt Project.
:param args: The parsed command-line arguments.
:param dependencies: A mapping of project names to RuntimeConfigs.
:param catalogs: A parsed dbt ExternalCatalogConfig.
:returns RuntimeConfig: The new configuration.
"""
quoting: Dict[str, Any] = (
Expand Down Expand Up @@ -194,6 +201,7 @@ def from_parts(
dependencies=dependencies,
dbt_cloud=project.dbt_cloud,
flags=project.flags,
catalogs=catalogs,
)

# Called by 'load_projects' in this class
Expand Down Expand Up @@ -253,7 +261,9 @@ def validate(self):

# Called by RuntimeConfig.from_args
@classmethod
def collect_parts(cls: Type["RuntimeConfig"], args: Any) -> Tuple[Project, Profile]:
def collect_parts(
cls: Type["RuntimeConfig"], args: Any
) -> Tuple[Project, Profile, Optional[ExternalCatalogConfig]]:
# profile_name from the project
project_root = args.project_dir if args.project_dir else os.getcwd()
cli_vars: Dict[str, Any] = getattr(args, "vars", {})
Expand All @@ -264,7 +274,8 @@ def collect_parts(cls: Type["RuntimeConfig"], args: Any) -> Tuple[Project, Profi
)
flags = get_flags()
project = load_project(project_root, bool(flags.VERSION_CHECK), profile, cli_vars)
return project, profile
catalogs = load_external_catalog_config(project)
return project, profile, catalogs

# Called in task/base.py, in BaseTask.from_args
@classmethod
Expand All @@ -278,12 +289,13 @@ def from_args(cls, args: Any) -> "RuntimeConfig":
:raises DbtProfileError: If the profile is invalid or missing.
:raises DbtValidationError: If the cli variables are invalid.
"""
project, profile = cls.collect_parts(args)
project, profile, catalogs = cls.collect_parts(args)

return cls.from_parts(
project=project,
profile=profile,
args=args,
catalogs=catalogs,
)

def get_metadata(self) -> ManifestMetadata:
Expand Down
1 change: 1 addition & 0 deletions core/dbt/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
PACKAGES_FILE_NAME = "packages.yml"
DEPENDENCIES_FILE_NAME = "dependencies.yml"
PACKAGE_LOCK_FILE_NAME = "package-lock.yml"
EXTERNAL_CATALOG_FILE_NAME = "catalog.yml"
MANIFEST_FILE_NAME = "manifest.json"
SEMANTIC_MANIFEST_FILE_NAME = "semantic_manifest.json"
LEGACY_TIME_SPINE_MODEL_NAME = "metricflow_time_spine"
Expand Down
3 changes: 2 additions & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
git+https://github.com/dbt-labs/dbt-adapters.git@main
git+https://github.com/dbt-labs/dbt-adapters.git@main#subdirectory=dbt-tests-adapter
git+https://github.com/dbt-labs/dbt-common.git@main
git+https://github.com/dbt-labs/dbt-common.git@feature/externalCatalogConfig
git+https://github.com/dbt-labs/dbt-common.git@feature/externalCatalogConfig#egg=dbt-config&subdirectory=config
git+https://github.com/dbt-labs/dbt-postgres.git@main
# black must match what's in .pre-commit-config.yaml to be sure local env matches CI
black==24.3.0
Expand Down

0 comments on commit 666cc3b

Please sign in to comment.