Coverage for pydantic/aliases.py: 97.14%

52 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2024-06-21 17:00 +0000

1"""Support for alias configurations.""" 

2 

3from __future__ import annotations 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

4 

5import dataclasses 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

6from typing import Any, Callable, Literal 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

7 

8from pydantic_core import PydanticUndefined 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

9 

10from ._internal import _internal_dataclass 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

11 

12__all__ = ('AliasGenerator', 'AliasPath', 'AliasChoices') 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

13 

14 

15@dataclasses.dataclass(**_internal_dataclass.slots_true) 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

16class AliasPath: 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

17 """Usage docs: https://docs.pydantic.dev/2.8/concepts/alias#aliaspath-and-aliaschoices 

18 

19 A data class used by `validation_alias` as a convenience to create aliases. 

20 

21 Attributes: 

22 path: A list of string or integer aliases. 

23 """ 

24 

25 path: list[int | str] 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

26 

27 def __init__(self, first_arg: str, *args: str | int) -> None: 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

28 self.path = [first_arg] + list(args) 1abcdefghijklmnopqrstuvwxyzABCDEF

29 

30 def convert_to_aliases(self) -> list[str | int]: 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

31 """Converts arguments to a list of string or integer aliases. 

32 

33 Returns: 

34 The list of aliases. 

35 """ 

36 return self.path 1abcdefghijklmnopqrstuvwxyzABCDEF

37 

38 def search_dict_for_path(self, d: dict) -> Any: 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

39 """Searches a dictionary for the path specified by the alias. 

40 

41 Returns: 

42 The value at the specified path, or `PydanticUndefined` if the path is not found. 

43 """ 

44 v = d 1abcdefghijklmnopqrstuvwxyzABCDEF

45 for k in self.path: 1abcdefghijklmnopqrstuvwxyzABCDEF

46 if isinstance(v, str): 46 ↛ 48line 46 didn't jump to line 48, because the condition on line 46 was never true1abcdefghijklmnopqrstuvwxyzABCDEF

47 # disallow indexing into a str, like for AliasPath('x', 0) and x='abc' 

48 return PydanticUndefined 

49 try: 1abcdefghijklmnopqrstuvwxyzABCDEF

50 v = v[k] 1abcdefghijklmnopqrstuvwxyzABCDEF

51 except (KeyError, IndexError, TypeError): 1abcdefghijklmnopqrstuvwxyzABCDEF

52 return PydanticUndefined 1abcdefghijklmnopqrstuvwxyzABCDEF

53 return v 1abcdefghijklmnopqrstuvwxyzABCDEF

54 

55 

56@dataclasses.dataclass(**_internal_dataclass.slots_true) 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

57class AliasChoices: 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

58 """Usage docs: https://docs.pydantic.dev/2.8/concepts/alias#aliaspath-and-aliaschoices 

59 

60 A data class used by `validation_alias` as a convenience to create aliases. 

61 

62 Attributes: 

63 choices: A list containing a string or `AliasPath`. 

64 """ 

65 

66 choices: list[str | AliasPath] 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

67 

68 def __init__(self, first_choice: str | AliasPath, *choices: str | AliasPath) -> None: 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

69 self.choices = [first_choice] + list(choices) 1abcdefghijklmnopqrstuvwxyzABCDEF

70 

71 def convert_to_aliases(self) -> list[list[str | int]]: 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

72 """Converts arguments to a list of lists containing string or integer aliases. 

73 

74 Returns: 

75 The list of aliases. 

76 """ 

77 aliases: list[list[str | int]] = [] 1abcdefghijklmnopqrstuvwxyzABCDEF

78 for c in self.choices: 1abcdefghijklmnopqrstuvwxyzABCDEF

79 if isinstance(c, AliasPath): 1abcdefghijklmnopqrstuvwxyzABCDEF

80 aliases.append(c.convert_to_aliases()) 1abcdefghijklmnopqrstuvwxyzABCDEF

81 else: 

82 aliases.append([c]) 1abcdefghijklmnopqrstuvwxyzABCDEF

83 return aliases 1abcdefghijklmnopqrstuvwxyzABCDEF

84 

85 

86@dataclasses.dataclass(**_internal_dataclass.slots_true) 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

87class AliasGenerator: 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

88 """Usage docs: https://docs.pydantic.dev/2.8/concepts/alias#using-an-aliasgenerator 

89 

90 A data class used by `alias_generator` as a convenience to create various aliases. 

91 

92 Attributes: 

93 alias: A callable that takes a field name and returns an alias for it. 

94 validation_alias: A callable that takes a field name and returns a validation alias for it. 

95 serialization_alias: A callable that takes a field name and returns a serialization alias for it. 

96 """ 

97 

98 alias: Callable[[str], str] | None = None 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

99 validation_alias: Callable[[str], str | AliasPath | AliasChoices] | None = None 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

100 serialization_alias: Callable[[str], str] | None = None 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

101 

102 def _generate_alias( 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

103 self, 

104 alias_kind: Literal['alias', 'validation_alias', 'serialization_alias'], 

105 allowed_types: tuple[type[str] | type[AliasPath] | type[AliasChoices], ...], 

106 field_name: str, 

107 ) -> str | AliasPath | AliasChoices | None: 

108 """Generate an alias of the specified kind. Returns None if the alias generator is None. 

109 

110 Raises: 

111 TypeError: If the alias generator produces an invalid type. 

112 """ 

113 alias = None 1abcdefghijklmnopqrstuvwxyzABCDEF

114 if alias_generator := getattr(self, alias_kind): 1abcdefghijklmnopqrstuvwxyzABCDEF

115 alias = alias_generator(field_name) 1abcdefghijklmnopqrstuvwxyzABCDEF

116 if alias and not isinstance(alias, allowed_types): 1abcdefghijklmnopqrstuvwxyzABCDEF

117 raise TypeError( 1abcdefghijklmnopqrstuvwxyzABCDEF

118 f'Invalid `{alias_kind}` type. `{alias_kind}` generator must produce one of `{allowed_types}`' 

119 ) 

120 return alias 1abcdefghijklmnopqrstuvwxyzABCDEF

121 

122 def generate_aliases(self, field_name: str) -> tuple[str | None, str | AliasPath | AliasChoices | None, str | None]: 1abcdefghijklmnopqrstuvGHIJKLMNOwxyzABCDEF

123 """Generate `alias`, `validation_alias`, and `serialization_alias` for a field. 

124 

125 Returns: 

126 A tuple of three aliases - validation, alias, and serialization. 

127 """ 

128 alias = self._generate_alias('alias', (str,), field_name) 1abcdefghijklmnopqrstuvwxyzABCDEF

129 validation_alias = self._generate_alias('validation_alias', (str, AliasChoices, AliasPath), field_name) 1abcdefghijklmnopqrstuvwxyzABCDEF

130 serialization_alias = self._generate_alias('serialization_alias', (str,), field_name) 1abcdefghijklmnopqrstuvwxyzABCDEF

131 

132 return alias, validation_alias, serialization_alias # type: ignore 1abcdefghijklmnopqrstuvwxyzABCDEF