Convert mutable types to immutable types.
(o: Any)
| 47 | |
| 48 | |
| 49 | def constify(o: Any) -> Any: |
| 50 | """ |
| 51 | Convert mutable types to immutable types. |
| 52 | """ |
| 53 | if isinstance(o, bytearray): |
| 54 | return bytes(o) |
| 55 | if isinstance(o, tuple): |
| 56 | try: |
| 57 | hash(o) |
| 58 | return o |
| 59 | except Exception: |
| 60 | return tuple(constify(elt) for elt in o) |
| 61 | if isinstance(o, list): |
| 62 | return tuple(constify(elt) for elt in o) |
| 63 | if isinstance(o, dict): |
| 64 | cdict = dict() |
| 65 | for k, v in o.items(): |
| 66 | cdict[k] = constify(v) |
| 67 | return Dict(cdict, True) |
| 68 | return o |