Skip to content

Commit

Permalink
fix type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
emmyoop committed Dec 15, 2022
1 parent 91191bd commit bd34660
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
7 changes: 4 additions & 3 deletions core/dbt/context/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
from dbt.constants import SECRET_ENV_PREFIX, DEFAULT_ENV_PLACEHOLDER
from dbt.contracts.graph.nodes import Resource
from dbt.exceptions import (
CompilationException,
DisallowSecretEnvVar,
EnvVarMissing,
MacroReturn,
RequiredVarNotFound,
SetStrictWrongType,
ZipStrictWrongType,
)
from dbt.events.functions import fire_event, get_invocation_id
from dbt.events.types import JinjaLogInfo, JinjaLogDebug
Expand Down Expand Up @@ -492,7 +493,7 @@ def set_strict(value: Iterable[Any]) -> Set[Any]:
try:
return set(value)
except TypeError as e:
raise CompilationException(e)
raise SetStrictWrongType(e)

@contextmember("zip")
@staticmethod
Expand Down Expand Up @@ -536,7 +537,7 @@ def zip_strict(*args: Iterable[Any]) -> Iterable[Any]:
try:
return zip(*args)
except TypeError as e:
raise CompilationException(e)
raise ZipStrictWrongType(e)

@contextmember
@staticmethod
Expand Down
57 changes: 49 additions & 8 deletions core/dbt/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import builtins
import json
import re
from typing import Any, Dict, List, Mapping, NoReturn, Optional
from typing import Any, Dict, List, Mapping, NoReturn, Optional, Union

# from dbt.contracts.graph import ManifestNode # or ParsedNode?
from dbt.dataclass_schema import ValidationError
Expand Down Expand Up @@ -468,7 +468,7 @@ def __init__(
self,
cwd: str,
cmd: List[str],
returncode: str,
returncode: Union[int, Any],
stdout: bytes,
stderr: bytes,
msg: str = "Got a non-zero returncode",
Expand Down Expand Up @@ -557,14 +557,14 @@ def __init__(self, name: str, node):


class CaughtMacroExceptionWithNode(CompilationException):
def __init__(self, exc: Exception, node):
def __init__(self, exc, node):
self.exc = exc
self.node = node
super().__init__(msg=str(exc))


class CaughtMacroException(CompilationException):
def __init__(self, exc: Exception):
def __init__(self, exc):
self.exc = exc
super().__init__(msg=str(exc))

Expand Down Expand Up @@ -751,6 +751,20 @@ def get_message(self) -> str:
# context level exceptions


class ZipStrictWrongType(CompilationException):
def __init__(self, exc):
self.exc = exc
msg = str(self.exc)
super().__init__(msg=msg)


class SetStrictWrongType(CompilationException):
def __init__(self, exc):
self.exc = exc
msg = str(self.exc)
super().__init__(msg=msg)


class LoadAgateTableValueError(CompilationException):
def __init__(self, exc: ValueError, node):
self.exc = exc
Expand Down Expand Up @@ -866,7 +880,7 @@ def get_message(self) -> str:

class InvalidMacroArgType(CompilationException):
def __init__(
self, method_name: str, arg_name: str, got_value: Any, expected_type: str, version: str
self, method_name: str, arg_name: str, got_value: Any, expected_type, version: str
):
self.method_name = method_name
self.arg_name = arg_name
Expand Down Expand Up @@ -1072,7 +1086,7 @@ def __init__(self, exc: ValidationError, node):
super().__init__(msg=self.msg)


class YamlParseFailure(ParsingException):
class YamlParseListFailure(ParsingException):
def __init__(
self,
path: str,
Expand All @@ -1097,8 +1111,33 @@ def get_message(self) -> str:
return msg


class YamlParseDictFailure(ParsingException):
def __init__(
self,
path: str,
key: str,
yaml_data: Dict[str, Any],
cause,
):
self.path = path
self.key = key
self.yaml_data = yaml_data
self.cause = cause
super().__init__(msg=self.get_message())

def get_message(self) -> str:
if isinstance(self.cause, str):
reason = self.cause
elif isinstance(self.cause, ValidationError):
reason = self.validator_error_message(self.cause)
else:
reason = self.cause.msg
msg = f"Invalid {self.key} config given in {self.path} @ {self.key}: {self.yaml_data} - {reason}"
return msg


class YamlLoadFailure(ParsingException):
def __init__(self, project_name: str, path: str, exc: ValidationException):
def __init__(self, project_name: Optional[str], path: str, exc: ValidationException):
self.project_name = project_name
self.path = path
self.exc = exc
Expand Down Expand Up @@ -1153,7 +1192,9 @@ def __init__(self, test_name: str):


class CustomMacroPopulatingConfigValues(CompilationException):
def __init__(self, target_name: str, column_name: str, name: str, key: str, err_msg: str):
def __init__(
self, target_name: str, column_name: Optional[str], name: str, key: str, err_msg: str
):
self.target_name = target_name
self.column_name = column_name
self.name = name
Expand Down
15 changes: 9 additions & 6 deletions core/dbt/parser/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
PropertyYMLVersionNotInt,
ValidationException,
YamlLoadFailure,
YamlParseFailure,
YamlParseDictFailure,
YamlParseListFailure,
)
from dbt.events.functions import warn_or_error
from dbt.events.types import WrongResourceSchemaFile, NoNodeForYamlKey, MacroPatchNotFound
Expand Down Expand Up @@ -606,7 +607,9 @@ def get_key_dicts(self) -> Iterable[Dict[str, Any]]:
# check that entry is a dict and that all dict values
# are strings
if coerce_dict_str(entry) is None:
raise YamlParseFailure(path, self.key, data, "expected a dict with string keys")
raise YamlParseListFailure(
path, self.key, data, "expected a dict with string keys"
)

if "name" not in entry:
raise ParsingException("Entry did not contain a name")
Expand Down Expand Up @@ -653,7 +656,7 @@ def _target_from_dict(self, cls: Type[T], data: Dict[str, Any]) -> T:
cls.validate(data)
return cls.from_dict(data)
except (ValidationError, JSONValidationException) as exc:
raise YamlParseFailure(path, self.key, data, exc)
raise YamlParseDictFailure(path, self.key, data, exc)

# The other parse method returns TestBlocks. This one doesn't.
# This takes the yaml dictionaries in 'sources' keys and uses them
Expand Down Expand Up @@ -777,7 +780,7 @@ def get_unparsed_target(self) -> Iterable[NonSourceTarget]:
self.normalize_docs_attribute(data, path)
node = self._target_type().from_dict(data)
except (ValidationError, JSONValidationException) as exc:
raise YamlParseFailure(path, self.key, data, exc)
raise YamlParseDictFailure(path, self.key, data, exc)
else:
yield node

Expand Down Expand Up @@ -1059,7 +1062,7 @@ def parse(self):
UnparsedExposure.validate(data)
unparsed = UnparsedExposure.from_dict(data)
except (ValidationError, JSONValidationException) as exc:
raise YamlParseFailure(self.yaml.path, self.key, data, exc)
raise YamlParseDictFailure(self.yaml.path, self.key, data, exc)

self.parse_exposure(unparsed)

Expand Down Expand Up @@ -1175,5 +1178,5 @@ def parse(self):
unparsed = UnparsedMetric.from_dict(data)

except (ValidationError, JSONValidationException) as exc:
raise YamlParseFailure(self.yaml.path, self.key, data, exc)
raise YamlParseDictFailure(self.yaml.path, self.key, data, exc)
self.parse_metric(unparsed)

0 comments on commit bd34660

Please sign in to comment.