| 8 | |
| 9 | @immutable |
| 10 | class Dict(collections.abc.Mapping): # lgtm[py/missing-equals] |
| 11 | def __init__( |
| 12 | self, |
| 13 | dictionary: Any, |
| 14 | no_copy: bool = False, |
| 15 | map_factory: Callable[[], collections.abc.MutableMapping] = dict, |
| 16 | ): |
| 17 | """Make an immutable dictionary from the specified dictionary. |
| 18 | |
| 19 | If *no_copy* is `True`, then *dictionary* will be wrapped instead |
| 20 | of copied. Only set this if you are sure there will be no external |
| 21 | references to the dictionary. |
| 22 | """ |
| 23 | if no_copy and isinstance(dictionary, collections.abc.MutableMapping): |
| 24 | self._odict = dictionary |
| 25 | else: |
| 26 | self._odict = map_factory() |
| 27 | self._odict.update(dictionary) |
| 28 | self._hash = None |
| 29 | |
| 30 | def __getitem__(self, key): |
| 31 | return self._odict.__getitem__(key) |
| 32 | |
| 33 | def __hash__(self): # pylint: disable=invalid-hash-returned |
| 34 | if self._hash is None: |
| 35 | h = 0 |
| 36 | for key in sorted(self._odict.keys()): |
| 37 | h ^= hash(key) |
| 38 | object.__setattr__(self, "_hash", h) |
| 39 | # this does return an int, but pylint doesn't figure that out |
| 40 | return self._hash |
| 41 | |
| 42 | def __len__(self): |
| 43 | return len(self._odict) |
| 44 | |
| 45 | def __iter__(self): |
| 46 | return iter(self._odict) |
| 47 | |
| 48 | |
| 49 | def constify(o: Any) -> Any: |
no outgoing calls
no test coverage detected
searching dependent graphs…