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

Support for dbt core changes to support mashumaro 3.15 #228

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
48 changes: 24 additions & 24 deletions dbt_common/contracts/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,30 @@ def same_contents(cls, unrendered: Dict[str, Any], other: Dict[str, Any]) -> boo
"object": ["snapshot_meta_column_names"],
}

@classmethod
def update_from(
cls,
orig_dict: Dict[str, Any],
new_dict: Dict[str, Any],
adapter_config_cls: Type[BaseConfig],
) -> Dict[str, Any]:
"""Update and validate config given a dict.

Given a dict of keys, update the current config from them, validate
it, and return a new config with the updated values
"""

self_merged = cls._merge_dicts(orig_dict, new_dict)
new_dict.update(self_merged)

adapter_merged = adapter_config_cls._merge_dicts(orig_dict, new_dict)
new_dict.update(adapter_merged)

# any remaining fields must be "clobber"
orig_dict.update(new_dict)

return orig_dict

@classmethod
def _merge_dicts(cls, src: Dict[str, Any], data: Dict[str, Any]) -> Dict[str, Any]:
"""Mutate input to return merge results.
Expand Down Expand Up @@ -150,30 +174,6 @@ def _merge_dicts(cls, src: Dict[str, Any], data: Dict[str, Any]) -> Dict[str, An
)
return result

def update_from(
Copy link
Collaborator

Choose a reason for hiding this comment

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

there don't appear to be explicit usages of update_from in dbt-adapters: https://github.com/search?q=repo%3Adbt-labs%2Fdbt-adapters%20update_from&type=code

Copy link
Collaborator

Choose a reason for hiding this comment

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

The signature change here looks like it will have a very minor impact (just to dbt-core). We could release a dbt-common major version bump for this change since it is technically breaking, but that might be overkill given its low usage. At least a minor bump makes sense to me though.

self: T, data: Dict[str, Any], config_cls: Type[BaseConfig], validate: bool = True
) -> T:
"""Update and validate config given a dict.

Given a dict of keys, update the current config from them, validate
it, and return a new config with the updated values
"""
dct = self.to_dict(omit_none=False)

self_merged = self._merge_dicts(dct, data)
dct.update(self_merged)

adapter_merged = config_cls._merge_dicts(dct, data)
dct.update(adapter_merged)

# any remaining fields must be "clobber"
dct.update(data)

# any validation failures must have come from the update
if validate:
self.validate(dct)
return self.from_dict(dct)

def finalize_and_validate(self: T) -> T:
dct = self.to_dict(omit_none=False)
self.validate(dct)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dependencies = [
"isodate>=0.6,<0.7",
"jsonschema>=4.0,<5.0",
"Jinja2>=3.1.3,<4",
"mashumaro[msgpack]>=3.9,<4.0",
"mashumaro[msgpack]>=3.15,<4.0",
"pathspec>=0.9,<0.13",
"protobuf>=5.0,<6.0",
"python-dateutil>=2.0,<3.0",
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/test_model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ def test_update_from() -> None:
"grants": {"two": "fine", "+one": "some"},
"snapshot_table_column_names": {"first_column": "dbt_ack", "second_column": "dbt_more"},
}
updated_obj = initial_obj.update_from(update_dct.copy(), SubstituteAdapterConfig)
updated_dict = initial_obj.update_from(
initial_obj.to_dict(), update_dct.copy(), SubstituteAdapterConfig
)
updated_obj = ThingWithMergeBehavior.from_dict(updated_dict)

assert updated_obj.default_behavior == 3
assert updated_obj.tags == ["one", "two", "five"]
Expand Down
Loading