_mock.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # mypy: allow-untyped-defs
  2. _magic_methods = [
  3. "__subclasscheck__",
  4. "__hex__",
  5. "__rmul__",
  6. "__float__",
  7. "__idiv__",
  8. "__setattr__",
  9. "__div__",
  10. "__invert__",
  11. "__nonzero__",
  12. "__rshift__",
  13. "__eq__",
  14. "__pos__",
  15. "__round__",
  16. "__rand__",
  17. "__or__",
  18. "__complex__",
  19. "__divmod__",
  20. "__len__",
  21. "__reversed__",
  22. "__copy__",
  23. "__reduce__",
  24. "__deepcopy__",
  25. "__rdivmod__",
  26. "__rrshift__",
  27. "__ifloordiv__",
  28. "__hash__",
  29. "__iand__",
  30. "__xor__",
  31. "__isub__",
  32. "__oct__",
  33. "__ceil__",
  34. "__imod__",
  35. "__add__",
  36. "__truediv__",
  37. "__unicode__",
  38. "__le__",
  39. "__delitem__",
  40. "__sizeof__",
  41. "__sub__",
  42. "__ne__",
  43. "__pow__",
  44. "__bytes__",
  45. "__mul__",
  46. "__itruediv__",
  47. "__bool__",
  48. "__iter__",
  49. "__abs__",
  50. "__gt__",
  51. "__iadd__",
  52. "__enter__",
  53. "__floordiv__",
  54. "__call__",
  55. "__neg__",
  56. "__and__",
  57. "__ixor__",
  58. "__getitem__",
  59. "__exit__",
  60. "__cmp__",
  61. "__getstate__",
  62. "__index__",
  63. "__contains__",
  64. "__floor__",
  65. "__lt__",
  66. "__getattr__",
  67. "__mod__",
  68. "__trunc__",
  69. "__delattr__",
  70. "__instancecheck__",
  71. "__setitem__",
  72. "__ipow__",
  73. "__ilshift__",
  74. "__long__",
  75. "__irshift__",
  76. "__imul__",
  77. "__lshift__",
  78. "__dir__",
  79. "__ge__",
  80. "__int__",
  81. "__ior__",
  82. ]
  83. class MockedObject:
  84. _name: str
  85. def __new__(cls, *args, **kwargs):
  86. # _suppress_err is set by us in the mocked module impl, so that we can
  87. # construct instances of MockedObject to hand out to people looking up
  88. # module attributes.
  89. # Any other attempt to construct a MockedObject instance (say, in the
  90. # unpickling process) should give an error.
  91. if not kwargs.get("_suppress_err"):
  92. raise NotImplementedError(
  93. f"Object '{cls._name}' was mocked out during packaging "
  94. f"but it is being used in '__new__'. If this error is "
  95. "happening during 'load_pickle', please ensure that your "
  96. "pickled object doesn't contain any mocked objects."
  97. )
  98. # Otherwise, this is just a regular object creation
  99. # (e.g. `x = MockedObject("foo")`), so pass it through normally.
  100. return super().__new__(cls)
  101. def __init__(self, name: str, _suppress_err: bool):
  102. self.__dict__["_name"] = name
  103. def __repr__(self):
  104. return f"MockedObject({self._name})"
  105. def install_method(method_name):
  106. def _not_implemented(self, *args, **kwargs):
  107. raise NotImplementedError(
  108. f"Object '{self._name}' was mocked out during packaging but it is being used in {method_name}"
  109. )
  110. setattr(MockedObject, method_name, _not_implemented)
  111. for method_name in _magic_methods:
  112. install_method(method_name)