_mock.py 2.8 KB

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