You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

97 line
3.7 KiB

  1. """Pydantic-specific warnings."""
  2. from __future__ import annotations as _annotations
  3. from .version import version_short
  4. __all__ = (
  5. 'PydanticDeprecatedSince20',
  6. 'PydanticDeprecatedSince26',
  7. 'PydanticDeprecatedSince29',
  8. 'PydanticDeprecatedSince210',
  9. 'PydanticDeprecatedSince211',
  10. 'PydanticDeprecationWarning',
  11. 'PydanticExperimentalWarning',
  12. )
  13. class PydanticDeprecationWarning(DeprecationWarning):
  14. """A Pydantic specific deprecation warning.
  15. This warning is raised when using deprecated functionality in Pydantic. It provides information on when the
  16. deprecation was introduced and the expected version in which the corresponding functionality will be removed.
  17. Attributes:
  18. message: Description of the warning.
  19. since: Pydantic version in what the deprecation was introduced.
  20. expected_removal: Pydantic version in what the corresponding functionality expected to be removed.
  21. """
  22. message: str
  23. since: tuple[int, int]
  24. expected_removal: tuple[int, int]
  25. def __init__(
  26. self, message: str, *args: object, since: tuple[int, int], expected_removal: tuple[int, int] | None = None
  27. ) -> None:
  28. super().__init__(message, *args)
  29. self.message = message.rstrip('.')
  30. self.since = since
  31. self.expected_removal = expected_removal if expected_removal is not None else (since[0] + 1, 0)
  32. def __str__(self) -> str:
  33. message = (
  34. f'{self.message}. Deprecated in Pydantic V{self.since[0]}.{self.since[1]}'
  35. f' to be removed in V{self.expected_removal[0]}.{self.expected_removal[1]}.'
  36. )
  37. if self.since == (2, 0):
  38. message += f' See Pydantic V2 Migration Guide at https://errors.pydantic.dev/{version_short()}/migration/'
  39. return message
  40. class PydanticDeprecatedSince20(PydanticDeprecationWarning):
  41. """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.0."""
  42. def __init__(self, message: str, *args: object) -> None:
  43. super().__init__(message, *args, since=(2, 0), expected_removal=(3, 0))
  44. class PydanticDeprecatedSince26(PydanticDeprecationWarning):
  45. """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.6."""
  46. def __init__(self, message: str, *args: object) -> None:
  47. super().__init__(message, *args, since=(2, 6), expected_removal=(3, 0))
  48. class PydanticDeprecatedSince29(PydanticDeprecationWarning):
  49. """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.9."""
  50. def __init__(self, message: str, *args: object) -> None:
  51. super().__init__(message, *args, since=(2, 9), expected_removal=(3, 0))
  52. class PydanticDeprecatedSince210(PydanticDeprecationWarning):
  53. """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.10."""
  54. def __init__(self, message: str, *args: object) -> None:
  55. super().__init__(message, *args, since=(2, 10), expected_removal=(3, 0))
  56. class PydanticDeprecatedSince211(PydanticDeprecationWarning):
  57. """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.11."""
  58. def __init__(self, message: str, *args: object) -> None:
  59. super().__init__(message, *args, since=(2, 11), expected_removal=(3, 0))
  60. class GenericBeforeBaseModelWarning(Warning):
  61. pass
  62. class PydanticExperimentalWarning(Warning):
  63. """A Pydantic specific experimental functionality warning.
  64. This warning is raised when using experimental functionality in Pydantic.
  65. It is raised to warn users that the functionality may change or be removed in future versions of Pydantic.
  66. """