warnings.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. 'PydanticDeprecatedSince212',
  11. 'PydanticDeprecationWarning',
  12. 'PydanticExperimentalWarning',
  13. 'ArbitraryTypeWarning',
  14. 'UnsupportedFieldAttributeWarning',
  15. 'TypedDictExtraConfigWarning',
  16. )
  17. class PydanticDeprecationWarning(DeprecationWarning):
  18. """A Pydantic specific deprecation warning.
  19. This warning is raised when using deprecated functionality in Pydantic. It provides information on when the
  20. deprecation was introduced and the expected version in which the corresponding functionality will be removed.
  21. Attributes:
  22. message: Description of the warning.
  23. since: Pydantic version in what the deprecation was introduced.
  24. expected_removal: Pydantic version in what the corresponding functionality expected to be removed.
  25. """
  26. message: str
  27. since: tuple[int, int]
  28. expected_removal: tuple[int, int]
  29. def __init__(
  30. self, message: str, *args: object, since: tuple[int, int], expected_removal: tuple[int, int] | None = None
  31. ) -> None:
  32. super().__init__(message, *args)
  33. self.message = message.rstrip('.')
  34. self.since = since
  35. self.expected_removal = expected_removal if expected_removal is not None else (since[0] + 1, 0)
  36. def __str__(self) -> str:
  37. message = (
  38. f'{self.message}. Deprecated in Pydantic V{self.since[0]}.{self.since[1]}'
  39. f' to be removed in V{self.expected_removal[0]}.{self.expected_removal[1]}.'
  40. )
  41. if self.since == (2, 0):
  42. message += f' See Pydantic V2 Migration Guide at https://errors.pydantic.dev/{version_short()}/migration/'
  43. return message
  44. class PydanticDeprecatedSince20(PydanticDeprecationWarning):
  45. """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.0."""
  46. def __init__(self, message: str, *args: object) -> None:
  47. super().__init__(message, *args, since=(2, 0), expected_removal=(3, 0))
  48. class PydanticDeprecatedSince26(PydanticDeprecationWarning):
  49. """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.6."""
  50. def __init__(self, message: str, *args: object) -> None:
  51. super().__init__(message, *args, since=(2, 6), expected_removal=(3, 0))
  52. class PydanticDeprecatedSince29(PydanticDeprecationWarning):
  53. """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.9."""
  54. def __init__(self, message: str, *args: object) -> None:
  55. super().__init__(message, *args, since=(2, 9), expected_removal=(3, 0))
  56. class PydanticDeprecatedSince210(PydanticDeprecationWarning):
  57. """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.10."""
  58. def __init__(self, message: str, *args: object) -> None:
  59. super().__init__(message, *args, since=(2, 10), expected_removal=(3, 0))
  60. class PydanticDeprecatedSince211(PydanticDeprecationWarning):
  61. """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.11."""
  62. def __init__(self, message: str, *args: object) -> None:
  63. super().__init__(message, *args, since=(2, 11), expected_removal=(3, 0))
  64. class PydanticDeprecatedSince212(PydanticDeprecationWarning):
  65. """A specific `PydanticDeprecationWarning` subclass defining functionality deprecated since Pydantic 2.12."""
  66. def __init__(self, message: str, *args: object) -> None:
  67. super().__init__(message, *args, since=(2, 12), expected_removal=(3, 0))
  68. class GenericBeforeBaseModelWarning(Warning):
  69. pass
  70. class PydanticExperimentalWarning(Warning):
  71. """A Pydantic specific experimental functionality warning.
  72. It is raised to warn users that the functionality may change or be removed in future versions of Pydantic.
  73. """
  74. class CoreSchemaGenerationWarning(UserWarning):
  75. """A warning raised during core schema generation."""
  76. class ArbitraryTypeWarning(CoreSchemaGenerationWarning):
  77. """A warning raised when Pydantic fails to generate a core schema for an arbitrary type."""
  78. class UnsupportedFieldAttributeWarning(CoreSchemaGenerationWarning):
  79. """A warning raised when a `Field()` attribute isn't supported in the context it is used."""
  80. class TypedDictExtraConfigWarning(CoreSchemaGenerationWarning):
  81. """A warning raised when the [`extra`][pydantic.ConfigDict.extra] configuration is incompatible with the `closed` or `extra_items` specification."""