Skip to content

Commit

Permalink
Create functors to initialize event types with str-type member attrib…
Browse files Browse the repository at this point in the history
…utes.

Before this change, the spec of various classes expected base_msg and msg params to be str's. This assumption did not always hold true. post_init hooks ensures the spec is obeyed.
  • Loading branch information
VersusFacit committed Oct 17, 2022
1 parent 73aebd8 commit ef4eae9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions core/dbt/events/adapter_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
)


# N.B. No guarantees for what type param msg is.
@dataclass
class AdapterLogger:
name: str
Expand Down
17 changes: 17 additions & 0 deletions core/dbt/events/base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ def level_tag(self) -> str:
return "error"


# Included to ensure classes with str-type message members are initialized correctly.
@dataclass # type: ignore[misc]
class AdapterEventStringFunctor:
def __post_init__(self):
super().__post_init__()
if not isinstance(self.base_msg, str):
self.base_msg = str(self.base_msg)


@dataclass # type: ignore[misc]
class EventStringFunctor:
def __post_init__(self):
super().__post_init__()
if not isinstance(self.msg, str):
self.msg = str(self.msg)


# prevents an event from going to the file
# This should rarely be used in core code. It is currently
# only used in integration tests and for the 'clean' command.
Expand Down
20 changes: 11 additions & 9 deletions core/dbt/events/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
WarnLevel,
ErrorLevel,
Cache,
AdapterEventStringFunctor,
EventStringFunctor,
)
from dbt.events.format import format_fancy_output_line, pluralize

Expand Down Expand Up @@ -309,7 +311,7 @@ def message(self) -> str:


@dataclass
class AdapterEventDebug(DebugLevel, pt.AdapterEventDebug): # noqa
class AdapterEventDebug(DebugLevel, AdapterEventStringFunctor, pt.AdapterEventDebug): # noqa
def code(self):
return "E001"

Expand All @@ -318,7 +320,7 @@ def message(self):


@dataclass
class AdapterEventInfo(InfoLevel, pt.AdapterEventInfo): # noqa
class AdapterEventInfo(InfoLevel, AdapterEventStringFunctor, pt.AdapterEventInfo): # noqa
def code(self):
return "E002"

Expand All @@ -327,7 +329,7 @@ def message(self):


@dataclass
class AdapterEventWarning(WarnLevel, pt.AdapterEventWarning): # noqa
class AdapterEventWarning(WarnLevel, AdapterEventStringFunctor, pt.AdapterEventWarning): # noqa
def code(self):
return "E003"

Expand All @@ -336,7 +338,7 @@ def message(self):


@dataclass
class AdapterEventError(ErrorLevel, pt.AdapterEventError): # noqa
class AdapterEventError(ErrorLevel, AdapterEventStringFunctor, pt.AdapterEventError): # noqa
def code(self):
return "E004"

Expand Down Expand Up @@ -1227,7 +1229,7 @@ def message(self) -> str:


@dataclass
class InvalidRefInTestNode(DebugLevel, pt.InvalidRefInTestNode):
class InvalidRefInTestNode(DebugLevel, EventStringFunctor, pt.InvalidRefInTestNode):
def code(self):
return "I051"

Expand Down Expand Up @@ -1334,7 +1336,7 @@ def message(self) -> str:


@dataclass
class MacroEventInfo(InfoLevel, pt.MacroEventInfo):
class MacroEventInfo(InfoLevel, EventStringFunctor, pt.MacroEventInfo):
def code(self):
return "M011"

Expand All @@ -1343,7 +1345,7 @@ def message(self) -> str:


@dataclass
class MacroEventDebug(DebugLevel, pt.MacroEventDebug):
class MacroEventDebug(DebugLevel, EventStringFunctor, pt.MacroEventDebug):
def code(self):
return "M012"

Expand Down Expand Up @@ -2308,7 +2310,7 @@ def message(self) -> str:


@dataclass
class AfterFirstRunResultError(ErrorLevel, pt.AfterFirstRunResultError):
class AfterFirstRunResultError(ErrorLevel, EventStringFunctor, pt.AfterFirstRunResultError):
def code(self):
return "Z029"

Expand Down Expand Up @@ -2476,7 +2478,7 @@ def message(self) -> str:


@dataclass
class RunResultWarningMessage(WarnLevel, pt.RunResultWarningMessage):
class RunResultWarningMessage(WarnLevel, EventStringFunctor, pt.RunResultWarningMessage):
def code(self):
return "Z049"

Expand Down

0 comments on commit ef4eae9

Please sign in to comment.