1"""
2frozen (immutable) data structures to support MultiIndexing
3
4These are used for:
5
6- .names (FrozenList)
7
8"""
9
10from __future__ import annotations
11
12from typing import (
13 NoReturn,
14 Self,
15)
16
17from pandas.util._decorators import set_module
18
19from pandas.core.base import PandasObject
20
21from pandas.io.formats.printing import pprint_thing
22
23
24@set_module("pandas.api.typing")
25class FrozenList(PandasObject, list):
26 """
27 Container that doesn't allow setting item *but*
28 because it's technically hashable, will be used
29 for lookups, appropriately, etc.
30 """
31
32 # Side note: This has to be of type list. Otherwise,
33 # it messes up PyTables type checks.
34
35 def union(self, other) -> FrozenList:
36 """
37 Returns a FrozenList with other concatenated to the end of self.
38
39 Parameters
40 ----------
41 other : array-like
42 The array-like whose elements we are concatenating.
43
44 Returns
45 -------
46 FrozenList
47 The collection difference between self and other.
48 """
49 if isinstance(other, tuple):
50 other = list(other)
51 return type(self)(super().__add__(other))
52
53 def difference(self, other) -> FrozenList:
54 """
55 Returns a FrozenList with elements from other removed from self.
56
57 Parameters
58 ----------
59 other : array-like
60 The array-like whose elements we are removing self.
61
62 Returns
63 -------
64 FrozenList
65 The collection difference between self and other.
66 """
67 other = set(other)
68 temp = [x for x in self if x not in other]
69 return type(self)(temp)
70
71 # TODO: Consider deprecating these in favor of `union` (xref gh-15506)
72
73 __add__ = __iadd__ = union # pyright: ignore[reportAssignmentType]
74
75 def __getitem__(self, n):
76 if isinstance(n, slice):
77 return type(self)(super().__getitem__(n))
78 return super().__getitem__(n)
79
80 def __radd__(self, other) -> Self:
81 if isinstance(other, tuple):
82 other = list(other)
83 return type(self)(other + list(self))
84
85 def __eq__(self, other: object) -> bool:
86 if isinstance(other, (tuple, FrozenList)):
87 other = list(other)
88 return super().__eq__(other)
89
90 __req__ = __eq__
91
92 def __mul__(self, other) -> Self:
93 return type(self)(super().__mul__(other))
94
95 __imul__ = __mul__
96
97 def __reduce__(self):
98 return type(self), (list(self),)
99
100 # error: Signature of "__hash__" incompatible with supertype "list"
101 def __hash__(self) -> int: # type: ignore[override]
102 return hash(tuple(self))
103
104 def _disabled(self, *args, **kwargs) -> NoReturn:
105 """
106 This method will not function because object is immutable.
107 """
108 raise TypeError(f"'{type(self).__name__}' does not support mutable operations.")
109
110 def __str__(self) -> str:
111 return pprint_thing(
112 self, quote_strings=True, escape_chars=("\t", "\r", "\n", "'")
113 )
114
115 def __repr__(self) -> str:
116 return f"{type(self).__name__}({self!s})"
117
118 __setitem__ = __setslice__ = _disabled
119 __delitem__ = __delslice__ = _disabled
120 pop = append = extend = _disabled
121 remove = sort = insert = _disabled # pyright: ignore[reportAssignmentType]