Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

73 linhas
2.6 KiB

  1. from __future__ import annotations as _annotations
  2. import warnings
  3. from typing import TYPE_CHECKING, Any, Literal
  4. from typing_extensions import deprecated
  5. from .._internal import _config
  6. from ..warnings import PydanticDeprecatedSince20
  7. if not TYPE_CHECKING:
  8. # See PyCharm issues https://youtrack.jetbrains.com/issue/PY-21915
  9. # and https://youtrack.jetbrains.com/issue/PY-51428
  10. DeprecationWarning = PydanticDeprecatedSince20
  11. __all__ = 'BaseConfig', 'Extra'
  12. class _ConfigMetaclass(type):
  13. def __getattr__(self, item: str) -> Any:
  14. try:
  15. obj = _config.config_defaults[item]
  16. warnings.warn(_config.DEPRECATION_MESSAGE, DeprecationWarning)
  17. return obj
  18. except KeyError as exc:
  19. raise AttributeError(f"type object '{self.__name__}' has no attribute {exc}") from exc
  20. @deprecated('BaseConfig is deprecated. Use the `pydantic.ConfigDict` instead.', category=PydanticDeprecatedSince20)
  21. class BaseConfig(metaclass=_ConfigMetaclass):
  22. """This class is only retained for backwards compatibility.
  23. !!! Warning "Deprecated"
  24. BaseConfig is deprecated. Use the [`pydantic.ConfigDict`][pydantic.ConfigDict] instead.
  25. """
  26. def __getattr__(self, item: str) -> Any:
  27. try:
  28. obj = super().__getattribute__(item)
  29. warnings.warn(_config.DEPRECATION_MESSAGE, DeprecationWarning)
  30. return obj
  31. except AttributeError as exc:
  32. try:
  33. return getattr(type(self), item)
  34. except AttributeError:
  35. # re-raising changes the displayed text to reflect that `self` is not a type
  36. raise AttributeError(str(exc)) from exc
  37. def __init_subclass__(cls, **kwargs: Any) -> None:
  38. warnings.warn(_config.DEPRECATION_MESSAGE, DeprecationWarning)
  39. return super().__init_subclass__(**kwargs)
  40. class _ExtraMeta(type):
  41. def __getattribute__(self, __name: str) -> Any:
  42. # The @deprecated decorator accesses other attributes, so we only emit a warning for the expected ones
  43. if __name in {'allow', 'ignore', 'forbid'}:
  44. warnings.warn(
  45. "`pydantic.config.Extra` is deprecated, use literal values instead (e.g. `extra='allow'`)",
  46. DeprecationWarning,
  47. stacklevel=2,
  48. )
  49. return super().__getattribute__(__name)
  50. @deprecated(
  51. "Extra is deprecated. Use literal values instead (e.g. `extra='allow'`)", category=PydanticDeprecatedSince20
  52. )
  53. class Extra(metaclass=_ExtraMeta):
  54. allow: Literal['allow'] = 'allow'
  55. ignore: Literal['ignore'] = 'ignore'
  56. forbid: Literal['forbid'] = 'forbid'