Adjust IntFlag handling in syrupy (#90223)

This commit is contained in:
epenet 2023-03-26 14:20:05 +02:00 committed by GitHub
parent f8431278c8
commit e0ec3488d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 6 deletions

View File

@ -125,6 +125,7 @@ tests: &tests
- tests/mock/**
- tests/pylint/**
- tests/scripts/**
- tests/syrupy.py
- tests/test_util/**
- tests/testing_config/**
- tests/util/**

View File

@ -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,
@ -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,
@ -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,

View File

@ -111,10 +111,10 @@ class HomeAssistantSnapshotSerializer(AmberDataSerializer):
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):
@ -201,6 +201,17 @@ class HomeAssistantSnapshotSerializer(AmberDataSerializer):
)
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."""