Skip to content

Commit

Permalink
Allow empty description in mf_pformat_dict.
Browse files Browse the repository at this point in the history
There are some cases where a description is not needed as it is added in other
ways. e.g. when building a snapshot file, the description is added through
snapshot helper methods.
  • Loading branch information
plypaul committed Nov 13, 2024
1 parent c0d9ebe commit 1d62c11
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections.abc import Mapping
from dataclasses import fields, is_dataclass
from enum import Enum
from typing import Any, Dict, List, Optional, Sized, Tuple, Union
from typing import Any, List, Optional, Sized, Tuple, Union

from dsi_pydantic_shim import BaseModel

Expand Down Expand Up @@ -429,8 +429,8 @@ def mf_pformat( # type: ignore


def mf_pformat_dict( # type: ignore
description: str,
obj_dict: Dict[str, Any],
description: Optional[str] = None,
obj_dict: Optional[Mapping[str, Any]] = None,
max_line_length: int = 120,
indent_prefix: str = " ",
include_object_field_names: bool = True,
Expand All @@ -444,7 +444,8 @@ def mf_pformat_dict( # type: ignore
representation of the string. e.g. if value="foo", then "foo" instead of "'foo'". Useful for values that contain
newlines.
"""
lines: List[str] = [description]
lines: List[str] = [description] if description is not None else []
obj_dict = obj_dict or {}
for key, value in obj_dict.items():
if preserve_raw_strings and isinstance(value, str):
value_str = value
Expand All @@ -471,5 +472,9 @@ def mf_pformat_dict( # type: ignore
else:
item_block_lines = (f"{key}: {value_str}",)
item_block = "\n".join(item_block_lines)
lines.append(indent(item_block))

if description is None:
lines.append(item_block)
else:
lines.append(indent(item_block))
return "\n".join(lines)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from dbt_semantic_interfaces.implementations.elements.dimension import PydanticDimension
from dbt_semantic_interfaces.type_enums import DimensionType
from metricflow_semantics.formatting.formatting_helpers import mf_dedent
from metricflow_semantics.mf_logging.formatting import indent
from metricflow_semantics.mf_logging.pretty_formattable import MetricFlowPrettyFormattable
from metricflow_semantics.mf_logging.pretty_print import mf_pformat, mf_pformat_dict
Expand Down Expand Up @@ -202,3 +203,18 @@ def pretty_format(self) -> Optional[str]:
return f"{self.__class__.__name__}({self.field_0:.2f})"

assert mf_pformat(_ExampleDataclass(1.2345)) == f"{_ExampleDataclass.__name__}(1.23)"


def test_pformat_dict_with_empty_message() -> None:
"""Test `mf_pformat_dict` without a description."""
result = mf_pformat_dict(obj_dict={"object_0": (1, 2, 3), "object_1": {4: 5}})

assert (
mf_dedent(
"""
object_0: (1, 2, 3)
object_1: {4: 5}
"""
)
== result
)

0 comments on commit 1d62c11

Please sign in to comment.