Skip to content

Commit

Permalink
Handle metric case for ObjectBuilderNamingScheme.
Browse files Browse the repository at this point in the history
  • Loading branch information
plypaul committed Nov 18, 2024
1 parent df48dd8 commit 9da9dab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import logging
from typing import Optional, Sequence

from dbt_semantic_interfaces.call_parameter_sets import (
Expand All @@ -13,9 +14,12 @@
from dbt_semantic_interfaces.type_enums.date_part import DatePart
from typing_extensions import override

from metricflow_semantics.mf_logging.lazy_formattable import LazyFormat
from metricflow_semantics.specs.instance_spec import InstanceSpec
from metricflow_semantics.specs.spec_set import InstanceSpecSet, InstanceSpecSetTransform, group_spec_by_type

logger = logging.getLogger(__name__)


class ObjectBuilderNameConverter:
"""Methods for converting classes related to the object-builder naming scheme to strings.
Expand Down Expand Up @@ -85,14 +89,6 @@ class _ObjectBuilderNameTransform(InstanceSpecSetTransform[Sequence[str]]):

@override
def transform(self, spec_set: InstanceSpecSet) -> Sequence[str]:
assert (
len(spec_set.entity_specs)
+ len(spec_set.dimension_specs)
+ len(spec_set.time_dimension_specs)
+ len(spec_set.group_by_metric_specs)
== 1
)

names_to_return = []

for entity_spec in spec_set.entity_specs:
Expand Down Expand Up @@ -138,11 +134,13 @@ def transform(self, spec_set: InstanceSpecSet) -> Sequence[str]:
return names_to_return

@staticmethod
def input_str_from_spec(instance_spec: InstanceSpec) -> str: # noqa: D102
def input_str_from_spec(instance_spec: InstanceSpec) -> Optional[str]: # noqa: D102
names = ObjectBuilderNameConverter._ObjectBuilderNameTransform().transform(group_spec_by_type(instance_spec))

if len(names) != 1:
raise RuntimeError(f"Did not get exactly 1 name from {instance_spec}. Got {names}")
if len(names) == 0:
return None
elif len(names) > 1:
raise RuntimeError(str(LazyFormat("Expected at most one name", instance_spec=instance_spec, names=names)))

return names[0]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from metricflow_semantics.specs.entity_spec import EntitySpec
from metricflow_semantics.specs.group_by_metric_spec import GroupByMetricSpec
from metricflow_semantics.specs.instance_spec import LinkableInstanceSpec
from metricflow_semantics.specs.metric_spec import MetricSpec
from metricflow_semantics.specs.time_dimension_spec import TimeDimensionSpec
from metricflow_semantics.test_helpers.metric_time_dimension import MTD_SPEC_MONTH, MTD_SPEC_WEEK, MTD_SPEC_YEAR
from metricflow_semantics.time.granularity import ExpandedTimeGranularity
Expand Down Expand Up @@ -63,6 +64,8 @@ def test_input_str(object_builder_naming_scheme: ObjectBuilderNamingScheme) -> N
== "Metric('bookings', group_by=['listing'])"
)

assert object_builder_naming_scheme.input_str(MetricSpec("bookings")) is None


def test_input_follows_scheme(object_builder_naming_scheme: ObjectBuilderNamingScheme) -> None: # noqa: D103
assert object_builder_naming_scheme.input_str_follows_scheme(
Expand Down

0 comments on commit 9da9dab

Please sign in to comment.