Return hash based on the session state.
(self)
| 92 | return self.get_state(as_dict=True) == other.get_state(as_dict=True) |
| 93 | |
| 94 | def __hash__(self) -> int: |
| 95 | """Return hash based on the session state.""" |
| 96 | state = self.get_state(as_dict=True) |
| 97 | hashable_items = list[tuple[str, int]]() |
| 98 | |
| 99 | # Convert dict to tuple of sorted items for consistent hashing. Exclude non-hashable values like cookies |
| 100 | # and convert them to their string representation. |
| 101 | for key, value in sorted(state.items()): |
| 102 | if key == 'cookies': |
| 103 | # Use hash of the cookies object if it has __hash__ method. |
| 104 | hashable_items.append((key, hash(self._cookies))) |
| 105 | elif isinstance(value, (list, dict)): |
| 106 | # Convert collections to tuples for hashing. |
| 107 | if isinstance(value, list): |
| 108 | hashable_items.append((key, hash(tuple(value)))) |
| 109 | else: |
| 110 | hashable_items.append((key, hash(tuple(sorted(value.items()))))) |
| 111 | else: |
| 112 | hashable_items.append((key, hash(value))) |
| 113 | |
| 114 | return hash(tuple(hashable_items)) |
| 115 | |
| 116 | @property |
| 117 | def id(self) -> str: |