exceptions.py 1.9 KB

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