Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working solution serialization bug. #5874

Merged
merged 3 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changes/unreleased/Fixes-20221016-173742.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
kind: Fixes
body: Add functors to ensure event types with str-type attributes are initialized
to spec, even when provided non-str type params.
time: 2022-10-16T17:37:42.846683-07:00
custom:
Author: versusfacit
Issue: "5436"
PR: "5874"
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to check if base_msg exists?

Copy link
Contributor Author

@VersusFacit VersusFacit Oct 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought about this. We're talking data classes so these objects must be instantiated with params. A little toy example:

from dataclasses import dataclass

@dataclass
class Item:
    a:str
    b:str
    c:str

asdf = Item()

Try running this and it complains:

TypeError: init() missing 3 required positional arguments: 'a', 'b', and 'c'

Hence, if a dataclass exists, it's gotta have the params. And this functor should only be used in classes that have this param. I think that renders an existential check redundant? (if it's not, tell me)

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
30 changes: 17 additions & 13 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 @@ -1218,7 +1220,9 @@ def message(self) -> str:

# TODO: switch to storing structured info and calling get_target_failure_msg
@dataclass
class InvalidDisabledSourceInTestNode(WarnLevel, pt.InvalidDisabledSourceInTestNode):
class InvalidDisabledSourceInTestNode(
WarnLevel, EventStringFunctor, pt.InvalidDisabledSourceInTestNode
):
def code(self):
return "I050"

Expand All @@ -1227,7 +1231,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 +1338,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 +1347,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 @@ -2261,7 +2265,7 @@ def message(self) -> str:


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

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


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

Expand All @@ -2308,7 +2312,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 @@ -2446,7 +2450,7 @@ def message(self) -> str:


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

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


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

Expand Down