From 9da9dab1d112d57aed190f33c7a0de779e597167 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Mon, 18 Nov 2024 11:51:57 -0800 Subject: [PATCH] Handle metric case for `ObjectBuilderNamingScheme`. --- .../naming/object_builder_str.py | 20 +++++++++---------- .../test_object_builder_naming_scheme.py | 3 +++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/metricflow-semantics/metricflow_semantics/naming/object_builder_str.py b/metricflow-semantics/metricflow_semantics/naming/object_builder_str.py index 3810bf34ed..126f100d4d 100644 --- a/metricflow-semantics/metricflow_semantics/naming/object_builder_str.py +++ b/metricflow-semantics/metricflow_semantics/naming/object_builder_str.py @@ -1,5 +1,6 @@ from __future__ import annotations +import logging from typing import Optional, Sequence from dbt_semantic_interfaces.call_parameter_sets import ( @@ -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. @@ -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: @@ -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] diff --git a/metricflow-semantics/tests_metricflow_semantics/naming/test_object_builder_naming_scheme.py b/metricflow-semantics/tests_metricflow_semantics/naming/test_object_builder_naming_scheme.py index 1b8a058a32..ed62631a17 100644 --- a/metricflow-semantics/tests_metricflow_semantics/naming/test_object_builder_naming_scheme.py +++ b/metricflow-semantics/tests_metricflow_semantics/naming/test_object_builder_naming_scheme.py @@ -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 @@ -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(