Skip to content

Commit

Permalink
[#2401] Configs in schema files
Browse files Browse the repository at this point in the history
automatic commit by git-black, original commits:
  d5461cc
  • Loading branch information
gshank authored and iknox-fa committed Feb 8, 2022
1 parent d820eb7 commit 3aaa24a
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 72 deletions.
12 changes: 6 additions & 6 deletions core/dbt/context/context_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def calculate_node_config(
resource_type: NodeType,
project_name: str,
base: bool,
patch_config_dict: Dict[str, Any] = None
patch_config_dict: Dict[str, Any] = None,
) -> BaseConfig:
own_config = self.get_node_project(project_name)

Expand Down Expand Up @@ -192,15 +192,15 @@ def calculate_node_config_dict(
resource_type: NodeType,
project_name: str,
base: bool,
patch_config_dict: dict = None
patch_config_dict: dict = None,
) -> Dict[str, Any]:
config = self.calculate_node_config(
config_call_dict=config_call_dict,
fqn=fqn,
resource_type=resource_type,
project_name=project_name,
base=base,
patch_config_dict=patch_config_dict
patch_config_dict=patch_config_dict,
)
finalized = config.finalize_and_validate()
return finalized.to_dict(omit_none=True)
Expand All @@ -217,15 +217,15 @@ def calculate_node_config_dict(
resource_type: NodeType,
project_name: str,
base: bool,
patch_config_dict: dict = None
patch_config_dict: dict = None,
) -> Dict[str, Any]:
return self.calculate_node_config(
config_call_dict=config_call_dict,
fqn=fqn,
resource_type=resource_type,
project_name=project_name,
base=base,
patch_config_dict=patch_config_dict
patch_config_dict=patch_config_dict,
)

def initial_result(self, resource_type: NodeType, base: bool) -> Dict[str, Any]:
Expand Down Expand Up @@ -295,5 +295,5 @@ def build_config_dict(
resource_type=self._resource_type,
project_name=self._project_name,
base=base,
patch_config_dict=patch_config_dict
patch_config_dict=patch_config_dict,
)
5 changes: 3 additions & 2 deletions core/dbt/contracts/graph/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
CompileResultNode, ManifestNode, NonSourceCompiledNode, GraphMemberNode
)
from dbt.contracts.graph.parsed import (
ParsedMacro, ParsedDocumentation,
ParsedMacro,
ParsedDocumentation,
ParsedSourceDefinition,
ParsedExposure,
Expand All @@ -31,7 +31,8 @@
from dbt.dataclass_schema import dbtClassMixin
from dbt.exceptions import (
CompilationException,
raise_duplicate_resource_name, raise_compiler_error,
raise_duplicate_resource_name,
raise_compiler_error,
)
from dbt.helper_types import PathSet
from dbt.events.functions import fire_event
Expand Down
6 changes: 2 additions & 4 deletions core/dbt/contracts/graph/model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def same_contents(cls, unrendered: Dict[str, Any], other: Dict[str, Any]) -> boo
# 'meta' moved here from node
mergebehavior = {
"append": ['pre-hook', 'pre_hook', 'post-hook', 'post_hook', 'tags'],
"update": ['quoting', 'column_types', 'meta'],
"update": ["quoting", "column_types", "meta"],
}

@classmethod
Expand Down Expand Up @@ -375,9 +375,7 @@ class NodeAndTestConfig(BaseConfig):
)
tags: Union[List[str], str] = field(
default_factory=list_str,
metadata=metas(ShowBehavior.Hide,
MergeBehavior.Append,
CompareBehavior.Exclude),
metadata=metas(ShowBehavior.Hide, MergeBehavior.Append, CompareBehavior.Exclude),
)
meta: Dict[str, Any] = field(
default_factory=dict,
Expand Down
12 changes: 6 additions & 6 deletions core/dbt/contracts/graph/parsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ def _serialize(self):
return self.to_dict()

def __post_serialize__(self, dct):
if 'config_call_dict' in dct:
del dct['config_call_dict']
if "config_call_dict" in dct:
del dct["config_call_dict"]
if '_event_status' in dct:
del dct['_event_status']
return dct
Expand Down Expand Up @@ -287,15 +287,15 @@ def _deserialize(cls, dct: Dict[str, int]):
return cls.from_dict(dct)

def _persist_column_docs(self) -> bool:
if hasattr(self.config, 'persist_docs'):
if hasattr(self.config, "persist_docs"):
assert isinstance(self.config, NodeConfig)
return bool(self.config.persist_docs.get('columns'))
return bool(self.config.persist_docs.get("columns"))
return False

def _persist_relation_docs(self) -> bool:
if hasattr(self.config, 'persist_docs'):
if hasattr(self.config, "persist_docs"):
assert isinstance(self.config, NodeConfig)
return bool(self.config.persist_docs.get('relation'))
return bool(self.config.persist_docs.get("relation"))
return False

def same_body(self: T, other: T) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/contracts/graph/unparsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def file_id(self):


@dataclass
class HasConfig():
class HasConfig:
config: Dict[str, Any] = field(default_factory=dict)


Expand Down
11 changes: 7 additions & 4 deletions core/dbt/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,11 @@ def update_parsed_node_name(
self._update_node_alias(parsed_node, config_dict)

def update_parsed_node_config(
self, parsed_node: IntermediateNode, config: ContextConfig,
context=None, patch_config_dict=None
self,
parsed_node: IntermediateNode,
config: ContextConfig,
context=None,
patch_config_dict=None,
) -> None:
"""Given the ContextConfig used for parsing and the parsed node,
generate and set the true values to use, overriding the temporary parse
Expand All @@ -312,8 +315,8 @@ def update_parsed_node_config(

# If we have meta in the config, copy to node level, for backwards
# compatibility with earlier node-only config.
if 'meta' in config_dict and config_dict['meta']:
parsed_node.meta = config_dict['meta']
if "meta" in config_dict and config_dict["meta"]:
parsed_node.meta = config_dict["meta"]

# unrendered_config is used to compare the original database/schema/alias
# values and to handle 'same_config' and 'same_contents' calls
Expand Down
79 changes: 45 additions & 34 deletions core/dbt/parser/generic_test_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,18 @@ class TestBuilder(Generic[Testable]):
# args in the test entry representing test configs
CONFIG_ARGS = (
'severity', 'tags', 'enabled', 'where', 'limit', 'warn_if', 'error_if',
'fail_calc', 'store_failures', 'meta', 'database', 'schema', 'alias',
"tags",
"enabled",
"where",
"limit",
"warn_if",
"error_if",
"fail_calc",
"store_failures",
"meta",
"database",
"schema",
"alias",
)

def __init__(
Expand Down Expand Up @@ -235,19 +246,19 @@ def __init__(
for key in self.CONFIG_ARGS:
value = self.args.pop(key, None)
# 'modifier' config could be either top level arg or in config
if value and 'config' in self.args and key in self.args['config']:
if value and "config" in self.args and key in self.args["config"]:
raise_compiler_error(
'Test cannot have the same key at the top-level and in config'
"Test cannot have the same key at the top-level and in config"
)
if not value and 'config' in self.args:
value = self.args['config'].pop(key, None)
if not value and "config" in self.args:
value = self.args["config"].pop(key, None)
if isinstance(value, str):
value = get_rendered(value, render_ctx, native=True)
if value is not None:
self.config[key] = value

if 'config' in self.args:
del self.args['config']
if "config" in self.args:
del self.args["config"]

if self.namespace is not None:
self.package_name = self.namespace
Expand All @@ -257,8 +268,8 @@ def __init__(
self.fqn_name: str = fqn_name

# use hashed name as alias if too long
if compiled_name != fqn_name and 'alias' not in self.config:
self.config['alias'] = compiled_name
if compiled_name != fqn_name and "alias" not in self.config:
self.config["alias"] = compiled_name

def _bad_type(self) -> TypeError:
return TypeError('invalid target type "{}"'.format(type(self.target)))
Expand Down Expand Up @@ -299,86 +310,86 @@ def extract_test_args(test, name=None) -> Tuple[str, Dict[str, Any]]:

@property
def enabled(self) -> Optional[bool]:
return self.config.get('enabled')
return self.config.get("enabled")

@property
def alias(self) -> Optional[str]:
return self.config.get('alias')
return self.config.get("alias")

@property
def severity(self) -> Optional[str]:
sev = self.config.get('severity')
sev = self.config.get("severity")
if sev:
return sev.upper()
else:
return None

@property
def store_failures(self) -> Optional[bool]:
return self.config.get('store_failures')
return self.config.get("store_failures")

@property
def where(self) -> Optional[str]:
return self.config.get('where')
return self.config.get("where")

@property
def limit(self) -> Optional[int]:
return self.config.get('limit')
return self.config.get("limit")

@property
def warn_if(self) -> Optional[str]:
return self.config.get('warn_if')
return self.config.get("warn_if")

@property
def error_if(self) -> Optional[str]:
return self.config.get('error_if')
return self.config.get("error_if")

@property
def fail_calc(self) -> Optional[str]:
return self.config.get('fail_calc')
return self.config.get("fail_calc")

@property
def meta(self) -> Optional[dict]:
return self.config.get('meta')
return self.config.get("meta")

@property
def database(self) -> Optional[str]:
return self.config.get('database')
return self.config.get("database")

@property
def schema(self) -> Optional[str]:
return self.config.get('schema')
return self.config.get("schema")

def get_static_config(self):
config = {}
if self.alias is not None:
config['alias'] = self.alias
config["alias"] = self.alias
if self.severity is not None:
config['severity'] = self.severity
config["severity"] = self.severity
if self.enabled is not None:
config['enabled'] = self.enabled
config["enabled"] = self.enabled
if self.where is not None:
config['where'] = self.where
config["where"] = self.where
if self.limit is not None:
config['limit'] = self.limit
config["limit"] = self.limit
if self.warn_if is not None:
config['warn_if'] = self.warn_if
config["warn_if"] = self.warn_if
if self.error_if is not None:
config['error_if'] = self.error_if
if self.fail_calc is not None:
config['fail_calc'] = self.fail_calc
config["fail_calc"] = self.fail_calc
if self.store_failures is not None:
config['store_failures'] = self.store_failures
config["store_failures"] = self.store_failures
if self.meta is not None:
config['meta'] = self.meta
config["meta"] = self.meta
if self.database is not None:
config['database'] = self.database
config["database"] = self.database
if self.schema is not None:
config['schema'] = self.schema
config["schema"] = self.schema
return config

def tags(self) -> List[str]:
tags = self.config.get('tags', [])
tags = self.config.get("tags", [])
if isinstance(tags, str):
tags = [tags]
if not isinstance(tags, list):
Expand Down Expand Up @@ -417,7 +428,7 @@ def construct_config(self) -> str:
else str(value)
)
for key, value
in self.config.items()
else str(value)
])
if configs:
return f"{{{{ config({configs}) }}}}"
Expand Down
Loading

0 comments on commit 3aaa24a

Please sign in to comment.