frozen.py 3.3 KB

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