mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 04:37:06 +00:00
Improve HA snapshot serializer typing (#122218)
This commit is contained in:
parent
f0b9a806d1
commit
24b12bc509
@ -12,13 +12,7 @@ import attr
|
||||
import attrs
|
||||
from syrupy.extensions.amber import AmberDataSerializer, AmberSnapshotExtension
|
||||
from syrupy.location import PyTestLocation
|
||||
from syrupy.types import (
|
||||
PropertyFilter,
|
||||
PropertyMatcher,
|
||||
PropertyPath,
|
||||
SerializableData,
|
||||
SerializedData,
|
||||
)
|
||||
from syrupy.types import PropertyFilter, PropertyMatcher, PropertyPath, SerializableData
|
||||
import voluptuous as vol
|
||||
import voluptuous_serialize
|
||||
|
||||
@ -90,7 +84,7 @@ class HomeAssistantSnapshotSerializer(AmberDataSerializer):
|
||||
matcher: PropertyMatcher | None = None,
|
||||
path: PropertyPath = (),
|
||||
visited: set[Any] | None = None,
|
||||
) -> SerializedData:
|
||||
) -> str:
|
||||
"""Pre-process data before serializing.
|
||||
|
||||
This allows us to handle specific cases for Home Assistant data structures.
|
||||
@ -111,7 +105,7 @@ class HomeAssistantSnapshotSerializer(AmberDataSerializer):
|
||||
serializable_data = voluptuous_serialize.convert(data)
|
||||
elif isinstance(data, ConfigEntry):
|
||||
serializable_data = cls._serializable_config_entry(data)
|
||||
elif dataclasses.is_dataclass(data):
|
||||
elif dataclasses.is_dataclass(type(data)):
|
||||
serializable_data = dataclasses.asdict(data)
|
||||
elif isinstance(data, IntFlag):
|
||||
# The repr of an enum.IntFlag has changed between Python 3.10 and 3.11
|
||||
@ -120,7 +114,7 @@ class HomeAssistantSnapshotSerializer(AmberDataSerializer):
|
||||
else:
|
||||
serializable_data = data
|
||||
with suppress(TypeError):
|
||||
if attr.has(data):
|
||||
if attr.has(type(data)):
|
||||
serializable_data = attrs.asdict(data)
|
||||
|
||||
return super()._serialize(
|
||||
@ -136,7 +130,7 @@ class HomeAssistantSnapshotSerializer(AmberDataSerializer):
|
||||
@classmethod
|
||||
def _serializable_area_registry_entry(cls, data: ar.AreaEntry) -> SerializableData:
|
||||
"""Prepare a Home Assistant area registry entry for serialization."""
|
||||
serialized = AreaRegistryEntrySnapshot(attrs.asdict(data) | {"id": ANY})
|
||||
serialized = AreaRegistryEntrySnapshot(dataclasses.asdict(data) | {"id": ANY})
|
||||
serialized.pop("_json_repr")
|
||||
return serialized
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user