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)
| 813 | |
| 814 | |
| 815 | def 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 | |
| 835 | def unpack_lenient_weakvaluedict(d: dict[str, Any] | None) -> dict[str, Any] | None: |
no test coverage detected