ds_common_serde_py_lib._serializable_deserialize

File: _serializable_deserialize.py Region: ds_common_serde_py_lib

Description

Defines internal helpers used by Serializable.deserialize() for inspecting dataclass fields and type hints, resolving type variables, converting field values, and setting init=False fields after construction.

Example

from dataclasses import dataclass, field

from ds_common_serde_py_lib import Serializable

@dataclass
class WithInitFalse(Serializable):
    name: str
    computed: str = field(init=False, default="computed")


obj = WithInitFalse.deserialize({"name": "x"})
assert obj.computed == "computed"

Attributes

logger

TypeVarType

T

Functions

_build_type_var_map(→ dict[Any, Any])

Build a mapping from TypeVars to their concrete types.

_get_class_type_hints(→ dict[str, Any])

Get type hints from the class itself (best-effort).

_get_dataclass_fields(→ tuple[Any, Ellipsis])

Get dataclass fields for the given class.

_resolve_type_hint_for_field(→ Any)

Resolve the type hint for a dataclass field.

_process_field(→ Any)

Process a single field during deserialization.

_set_init_false_fields(→ None)

Set fields with init=False and defaults after instance creation.

Module Contents

ds_common_serde_py_lib._serializable_deserialize.logger
ds_common_serde_py_lib._serializable_deserialize.TypeVarType
ds_common_serde_py_lib._serializable_deserialize.T
ds_common_serde_py_lib._serializable_deserialize._build_type_var_map(cls: type) dict[Any, Any][source]

Build a mapping from TypeVars to their concrete types.

Parameters:

cls – The class to build the type var map for.

Returns:

A mapping from TypeVars to their concrete types.

ds_common_serde_py_lib._serializable_deserialize._get_class_type_hints(cls: type) dict[str, Any][source]

Get type hints from the class itself (best-effort).

Parameters:

cls – The class to get the type hints for.

Returns:

A dictionary of type hints.

ds_common_serde_py_lib._serializable_deserialize._get_dataclass_fields(cls: type) tuple[Any, Ellipsis][source]

Get dataclass fields for the given class.

Parameters:

cls – The class to get the dataclass fields for.

Returns:

A tuple of dataclass fields.

ds_common_serde_py_lib._serializable_deserialize._resolve_type_hint_for_field(field: Any, field_name: str, cls_own_hints: dict[str, Any], type_var_map: dict[Any, Any], cls: type) Any[source]

Resolve the type hint for a dataclass field.

Parameters:
  • field – The field to resolve the type hint for.

  • field_name – The name of the field.

  • cls_own_hints – The type hints for the class.

  • type_var_map – A mapping from TypeVars to their concrete types.

  • cls – The class to resolve the type hint for.

Returns:

The resolved type hint.

ds_common_serde_py_lib._serializable_deserialize._process_field(field: Any, raw_value: Any, deserializers: dict[str, Any], cls_own_hints: dict[str, Any], type_var_map: dict[Any, Any], cls: type) Any[source]

Process a single field during deserialization.

Parameters:
  • field – The field to process.

  • raw_value – The raw value of the field.

  • deserializers – A dictionary of deserializers.

  • cls_own_hints – The type hints for the class.

  • type_var_map – A mapping from TypeVars to their concrete types.

  • cls – The class to process the field for.

Returns:

The processed value.

ds_common_serde_py_lib._serializable_deserialize._set_init_false_fields(instance: Any, class_fields: tuple[Any, Ellipsis]) None[source]

Set fields with init=False and defaults after instance creation.

Parameters:
  • instance – The instance to set the fields for.

  • class_fields – A tuple of dataclass fields.