exceptions.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # SPDX-License-Identifier: MIT
  2. from __future__ import annotations
  3. class FrozenError(AttributeError):
  4. """
  5. A frozen/immutable instance or attribute have been attempted to be
  6. modified.
  7. It mirrors the behavior of ``namedtuples`` by using the same error message
  8. and subclassing `AttributeError`.
  9. .. versionadded:: 20.1.0
  10. """
  11. def __init__(self):
  12. msg = "can't set attribute"
  13. super().__init__(msg)
  14. self.msg = msg
  15. class FrozenInstanceError(FrozenError):
  16. """
  17. A frozen instance has been attempted to be modified.
  18. .. versionadded:: 16.1.0
  19. """
  20. class FrozenAttributeError(FrozenError):
  21. """
  22. A frozen attribute has been attempted to be modified.
  23. .. versionadded:: 20.1.0
  24. """
  25. class AttrsAttributeNotFoundError(ValueError):
  26. """
  27. An *attrs* function couldn't find an attribute that the user asked for.
  28. .. versionadded:: 16.2.0
  29. """
  30. class NotAnAttrsClassError(ValueError):
  31. """
  32. A non-*attrs* class has been passed into an *attrs* function.
  33. .. versionadded:: 16.2.0
  34. """
  35. class DefaultAlreadySetError(RuntimeError):
  36. """
  37. A default has been set when defining the field and is attempted to be reset
  38. using the decorator.
  39. .. versionadded:: 17.1.0
  40. """
  41. class UnannotatedAttributeError(RuntimeError):
  42. """
  43. A class with ``auto_attribs=True`` has a field without a type annotation.
  44. .. versionadded:: 17.3.0
  45. """
  46. class PythonTooOldError(RuntimeError):
  47. """
  48. It was attempted to use an *attrs* feature that requires a newer Python
  49. version.
  50. .. versionadded:: 18.2.0
  51. """
  52. class NotCallableError(TypeError):
  53. """
  54. A field requiring a callable has been set with a value that is not
  55. callable.
  56. .. versionadded:: 19.2.0
  57. """
  58. def __init__(self, msg, value):
  59. super(TypeError, self).__init__(msg, value)
  60. self.msg = msg
  61. self.value = value
  62. def __str__(self):
  63. return str(self.msg)