Skip to content

Commit

Permalink
Adjust IntFlag handling in syrupy (home-assistant#90223)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Mar 26, 2023
1 parent f843127 commit e0ec348
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions .core_files.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ tests: &tests
- tests/mock/**
- tests/pylint/**
- tests/scripts/**
- tests/syrupy.py
- tests/test_util/**
- tests/testing_config/**
- tests/util/**
Expand Down
6 changes: 3 additions & 3 deletions tests/components/elgato/snapshots/test_light.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
'supported_color_modes': list([
<ColorMode.COLOR_TEMP: 'color_temp'>,
]),
'supported_features': 0,
'supported_features': <LightEntityFeature: 0>,
'xy_color': tuple(
0.465,
0.376,
Expand Down Expand Up @@ -130,7 +130,7 @@
<ColorMode.COLOR_TEMP: 'color_temp'>,
<ColorMode.HS: 'hs'>,
]),
'supported_features': 0,
'supported_features': <LightEntityFeature: 0>,
'xy_color': tuple(
0.465,
0.376,
Expand Down Expand Up @@ -236,7 +236,7 @@
<ColorMode.COLOR_TEMP: 'color_temp'>,
<ColorMode.HS: 'hs'>,
]),
'supported_features': 0,
'supported_features': <LightEntityFeature: 0>,
'xy_color': tuple(
0.34,
0.327,
Expand Down
17 changes: 14 additions & 3 deletions tests/syrupy.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ def _serialize(
serializable_data = cls._serializable_config_entry(data)
elif dataclasses.is_dataclass(data):
serializable_data = dataclasses.asdict(data)
elif isinstance(data, IntFlag) and data == 0:
elif isinstance(data, IntFlag):
# The repr of an enum.IntFlag has changed between Python 3.10 and 3.11
# This only concerns the 0 case, which we normalize here
serializable_data = 0
# so we normalize it here.
serializable_data = _IntFlagWrapper(data)
else:
serializable_data = data
with suppress(TypeError):
Expand Down Expand Up @@ -201,6 +201,17 @@ def _serializable_state(cls, data: State) -> SerializableData:
)


class _IntFlagWrapper:
def __init__(self, flag: IntFlag) -> None:
self._flag = flag

def __repr__(self) -> str:
# 3.10: <ClimateEntityFeature.SWING_MODE|PRESET_MODE|FAN_MODE|TARGET_TEMPERATURE: 57>
# 3.11: <ClimateEntityFeature.TARGET_TEMPERATURE|FAN_MODE|PRESET_MODE|SWING_MODE: 57>
# Syrupy: <ClimateEntityFeature: 57>
return f"<{self._flag.__class__.__name__}: {self._flag.value}>"


class HomeAssistantSnapshotExtension(AmberSnapshotExtension):
"""Home Assistant extension for Syrupy."""

Expand Down

0 comments on commit e0ec348

Please sign in to comment.