Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

136 lignes
4.8 KiB

  1. """Support for alias configurations."""
  2. from __future__ import annotations
  3. import dataclasses
  4. from typing import Any, Callable, Literal
  5. from pydantic_core import PydanticUndefined
  6. from ._internal import _internal_dataclass
  7. __all__ = ('AliasGenerator', 'AliasPath', 'AliasChoices')
  8. @dataclasses.dataclass(**_internal_dataclass.slots_true)
  9. class AliasPath:
  10. """!!! abstract "Usage Documentation"
  11. [`AliasPath` and `AliasChoices`](../concepts/alias.md#aliaspath-and-aliaschoices)
  12. A data class used by `validation_alias` as a convenience to create aliases.
  13. Attributes:
  14. path: A list of string or integer aliases.
  15. """
  16. path: list[int | str]
  17. def __init__(self, first_arg: str, *args: str | int) -> None:
  18. self.path = [first_arg] + list(args)
  19. def convert_to_aliases(self) -> list[str | int]:
  20. """Converts arguments to a list of string or integer aliases.
  21. Returns:
  22. The list of aliases.
  23. """
  24. return self.path
  25. def search_dict_for_path(self, d: dict) -> Any:
  26. """Searches a dictionary for the path specified by the alias.
  27. Returns:
  28. The value at the specified path, or `PydanticUndefined` if the path is not found.
  29. """
  30. v = d
  31. for k in self.path:
  32. if isinstance(v, str):
  33. # disallow indexing into a str, like for AliasPath('x', 0) and x='abc'
  34. return PydanticUndefined
  35. try:
  36. v = v[k]
  37. except (KeyError, IndexError, TypeError):
  38. return PydanticUndefined
  39. return v
  40. @dataclasses.dataclass(**_internal_dataclass.slots_true)
  41. class AliasChoices:
  42. """!!! abstract "Usage Documentation"
  43. [`AliasPath` and `AliasChoices`](../concepts/alias.md#aliaspath-and-aliaschoices)
  44. A data class used by `validation_alias` as a convenience to create aliases.
  45. Attributes:
  46. choices: A list containing a string or `AliasPath`.
  47. """
  48. choices: list[str | AliasPath]
  49. def __init__(self, first_choice: str | AliasPath, *choices: str | AliasPath) -> None:
  50. self.choices = [first_choice] + list(choices)
  51. def convert_to_aliases(self) -> list[list[str | int]]:
  52. """Converts arguments to a list of lists containing string or integer aliases.
  53. Returns:
  54. The list of aliases.
  55. """
  56. aliases: list[list[str | int]] = []
  57. for c in self.choices:
  58. if isinstance(c, AliasPath):
  59. aliases.append(c.convert_to_aliases())
  60. else:
  61. aliases.append([c])
  62. return aliases
  63. @dataclasses.dataclass(**_internal_dataclass.slots_true)
  64. class AliasGenerator:
  65. """!!! abstract "Usage Documentation"
  66. [Using an `AliasGenerator`](../concepts/alias.md#using-an-aliasgenerator)
  67. A data class used by `alias_generator` as a convenience to create various aliases.
  68. Attributes:
  69. alias: A callable that takes a field name and returns an alias for it.
  70. validation_alias: A callable that takes a field name and returns a validation alias for it.
  71. serialization_alias: A callable that takes a field name and returns a serialization alias for it.
  72. """
  73. alias: Callable[[str], str] | None = None
  74. validation_alias: Callable[[str], str | AliasPath | AliasChoices] | None = None
  75. serialization_alias: Callable[[str], str] | None = None
  76. def _generate_alias(
  77. self,
  78. alias_kind: Literal['alias', 'validation_alias', 'serialization_alias'],
  79. allowed_types: tuple[type[str] | type[AliasPath] | type[AliasChoices], ...],
  80. field_name: str,
  81. ) -> str | AliasPath | AliasChoices | None:
  82. """Generate an alias of the specified kind. Returns None if the alias generator is None.
  83. Raises:
  84. TypeError: If the alias generator produces an invalid type.
  85. """
  86. alias = None
  87. if alias_generator := getattr(self, alias_kind):
  88. alias = alias_generator(field_name)
  89. if alias and not isinstance(alias, allowed_types):
  90. raise TypeError(
  91. f'Invalid `{alias_kind}` type. `{alias_kind}` generator must produce one of `{allowed_types}`'
  92. )
  93. return alias
  94. def generate_aliases(self, field_name: str) -> tuple[str | None, str | AliasPath | AliasChoices | None, str | None]:
  95. """Generate `alias`, `validation_alias`, and `serialization_alias` for a field.
  96. Returns:
  97. A tuple of three aliases - validation, alias, and serialization.
  98. """
  99. alias = self._generate_alias('alias', (str,), field_name)
  100. validation_alias = self._generate_alias('validation_alias', (str, AliasChoices, AliasPath), field_name)
  101. serialization_alias = self._generate_alias('serialization_alias', (str,), field_name)
  102. return alias, validation_alias, serialization_alias # type: ignore