MCPcopy
hub / github.com/pydantic/pydantic / build_lenient_weakvaluedict

Function build_lenient_weakvaluedict

pydantic/_internal/_model_construction.py:815–832  ·  view source on GitHub ↗

Takes an input dictionary, and produces a new value that (invertibly) replaces the values with weakrefs. We can't just use a WeakValueDictionary because many types (including int, str, etc.) can't be stored as values in a WeakValueDictionary. The `unpack_lenient_weakvaluedict` function

(d: dict[str, Any] | None)

Source from the content-addressed store, hash-verified

813
814
815def build_lenient_weakvaluedict(d: dict[str, Any] | None) -> dict[str, Any] | None:
816 """Takes an input dictionary, and produces a new value that (invertibly) replaces the values with weakrefs.
817
818 We can't just use a WeakValueDictionary because many types (including int, str, etc.) can't be stored as values
819 in a WeakValueDictionary.
820
821 The `unpack_lenient_weakvaluedict` function can be used to reverse this operation.
822 """
823 if d is None:
824 return None
825 result = {}
826 for k, v in d.items():
827 try:
828 proxy = _PydanticWeakRef(v)
829 except TypeError:
830 proxy = v
831 result[k] = proxy
832 return result
833
834
835def unpack_lenient_weakvaluedict(d: dict[str, Any] | None) -> dict[str, Any] | None:

Callers 1

__new__Method · 0.85

Calls 2

_PydanticWeakRefClass · 0.85
itemsMethod · 0.45

Tested by

no test coverage detected