object_comparer.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from typing import Any
  2. class ObjectComparer: # pragma: no cover
  3. def __init__(self) -> None:
  4. pass # No operation performed in the constructor
  5. @staticmethod
  6. def is_same_object(obj1: Any, obj2: Any) -> bool:
  7. """
  8. Recursively compares two objects and ensures that:
  9. - Their types match
  10. - Their keys/structure match
  11. """
  12. if type(obj1) is not type(obj2):
  13. # Fail immediately if the types don't match
  14. return False
  15. if isinstance(obj1, dict):
  16. # Check that both are dicts and same length
  17. if not isinstance(obj2, dict) or len(obj1) != len(obj2):
  18. return False
  19. for key in obj1:
  20. if key not in obj2:
  21. return False
  22. # Recursively compare each value
  23. if not ObjectComparer.is_same_object(obj1[key], obj2[key]):
  24. return False
  25. return True
  26. if isinstance(obj1, list):
  27. # Check that both are lists and same length
  28. if not isinstance(obj2, list) or len(obj1) != len(obj2):
  29. return False
  30. # Recursively compare each item
  31. return all(ObjectComparer.is_same_object(obj1[i], obj2[i]) for i in range(len(obj1)))
  32. # For atomic values: types already match, so return True
  33. return True
  34. @staticmethod
  35. def is_strictly_empty(value: Any) -> bool:
  36. """
  37. Returns True if value is an empty container (str, list, dict, set, tuple).
  38. Returns False for non-containers like None, 0, False, etc.
  39. """
  40. return isinstance(value, str | list | dict | set | tuple) and len(value) == 0