Skip to content

Commit

Permalink
Add support for set type collections in the pretty-printer
Browse files Browse the repository at this point in the history
Our snapshot files rely on consistent order for all rendered
collections, but we were assuming that all input objects would
be strictly ordered. Unfortunately, the fallback method of using
Python's pretty-printer does not allow for ordered sets, so
any inputs involving set or frozenset types will be fundamentally
unstable.

This expands our list-like formatted print handling to cover set
types, indicating the distinction between set and frozenset and
printing all values in a stable sort order. This does assume all
set values are sortable, which should be the case.

As you can see from the snapshot updates, this is a pre-existing
problem in the codebase. We've gotten away with it because all of
the sets in our snapshots have, thus far, all been of length 1. This
will change in coming updates.
  • Loading branch information
tlento committed May 3, 2024
1 parent 6a230bc commit 900ace8
Show file tree
Hide file tree
Showing 19 changed files with 73 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def _is_pydantic_base_model(obj: Any): # type:ignore
# Checking the attribute as the BaseModel check fails for newer version of Pydantic.
return isinstance(obj, BaseModel) or hasattr(obj, "__config__")

def _handle_sequence_obj(self, list_like_obj: Union[list, tuple], remaining_line_width: Optional[int]) -> str:
def _handle_sequence_obj(
self, list_like_obj: Union[list, tuple, set, frozenset], remaining_line_width: Optional[int]
) -> str:
"""Pretty prints a sequence object i.e. list or tuple.
Args:
Expand All @@ -55,6 +57,10 @@ def _handle_sequence_obj(self, list_like_obj: Union[list, tuple], remaining_line
elif isinstance(list_like_obj, tuple):
left_enclose_str = "("
right_enclose_str = ")"
elif isinstance(list_like_obj, set) or isinstance(list_like_obj, frozenset):
left_enclose_str = f"{type(list_like_obj).__name__}("
right_enclose_str = ")"
list_like_obj = sorted(list_like_obj)
else:
raise RuntimeError(f"Unhandled type: {type(list_like_obj)}")

Expand Down Expand Up @@ -317,7 +323,7 @@ def _handle_any_obj(self, obj: Any, remaining_line_width: Optional[int]) -> str:
if isinstance(obj, Enum):
return obj.name

if isinstance(obj, (list, tuple)):
if isinstance(obj, (list, tuple, set, frozenset)):
return self._handle_sequence_obj(obj, remaining_line_width=remaining_line_width)

if isinstance(obj, dict):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GroupByItemResolution(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(METRIC_TIME,),
time_granularity=MONTH,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ GroupByItemResolution(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=MONTH,
),
),
Expand Down Expand Up @@ -63,7 +65,9 @@ GroupByItemResolution(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=YEAR,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GroupByItemResolution(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(METRIC_TIME,),
time_granularity=MONTH,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ GroupByItemResolution(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=MONTH,
),
),
Expand Down Expand Up @@ -58,7 +60,9 @@ GroupByItemResolution(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=YEAR,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GroupByItemResolution(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(METRIC_TIME,),
time_granularity=MONTH,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GroupByItemResolution(
LinkableDimension(
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(METRIC_TIME,),
time_granularity=DAY,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GroupByItemResolution(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(METRIC_TIME,),
time_granularity=MONTH,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=MONTH,
),
),
Expand Down Expand Up @@ -91,7 +93,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=MONTH,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=MONTH,
),
),
Expand Down Expand Up @@ -92,7 +94,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=YEAR,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=MONTH,
),
),
Expand Down Expand Up @@ -95,7 +97,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=YEAR,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=MONTH,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=MONTH,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=MONTH,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=MONTH,
),
),
Expand Down Expand Up @@ -92,7 +94,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=YEAR,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=MONTH,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=MONTH,
),
),
Expand Down Expand Up @@ -92,7 +94,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=YEAR,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=MONTH,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ FilterSpecResolutionLookUp(
),
element_name='metric_time',
dimension_type=TIME,
properties=frozenset({<LinkableElementProperty.METRIC_TIME: 'metric_time'>}),
properties=frozenset(
METRIC_TIME,
),
time_granularity=MONTH,
),
),
Expand Down

0 comments on commit 900ace8

Please sign in to comment.