utils.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. import keyword
  2. import warnings
  3. import weakref
  4. from collections import OrderedDict, defaultdict, deque
  5. from copy import deepcopy
  6. from itertools import islice, zip_longest
  7. from types import BuiltinFunctionType, CodeType, FunctionType, GeneratorType, LambdaType, ModuleType
  8. from typing import (
  9. TYPE_CHECKING,
  10. AbstractSet,
  11. Any,
  12. Callable,
  13. Collection,
  14. Dict,
  15. Generator,
  16. Iterable,
  17. Iterator,
  18. List,
  19. Mapping,
  20. NoReturn,
  21. Optional,
  22. Set,
  23. Tuple,
  24. Type,
  25. TypeVar,
  26. Union,
  27. )
  28. from typing_extensions import Annotated
  29. from .errors import ConfigError
  30. from .typing import (
  31. NoneType,
  32. WithArgsTypes,
  33. all_literal_values,
  34. display_as_type,
  35. get_args,
  36. get_origin,
  37. is_literal_type,
  38. is_union,
  39. )
  40. from .version import version_info
  41. if TYPE_CHECKING:
  42. from inspect import Signature
  43. from pathlib import Path
  44. from .config import BaseConfig
  45. from .dataclasses import Dataclass
  46. from .fields import ModelField
  47. from .main import BaseModel
  48. from .typing import AbstractSetIntStr, DictIntStrAny, IntStr, MappingIntStrAny, ReprArgs
  49. RichReprResult = Iterable[Union[Any, Tuple[Any], Tuple[str, Any], Tuple[str, Any, Any]]]
  50. __all__ = (
  51. 'import_string',
  52. 'sequence_like',
  53. 'validate_field_name',
  54. 'lenient_isinstance',
  55. 'lenient_issubclass',
  56. 'in_ipython',
  57. 'is_valid_identifier',
  58. 'deep_update',
  59. 'update_not_none',
  60. 'almost_equal_floats',
  61. 'get_model',
  62. 'to_camel',
  63. 'is_valid_field',
  64. 'smart_deepcopy',
  65. 'PyObjectStr',
  66. 'Representation',
  67. 'GetterDict',
  68. 'ValueItems',
  69. 'version_info', # required here to match behaviour in v1.3
  70. 'ClassAttribute',
  71. 'path_type',
  72. 'ROOT_KEY',
  73. 'get_unique_discriminator_alias',
  74. 'get_discriminator_alias_and_values',
  75. 'DUNDER_ATTRIBUTES',
  76. )
  77. ROOT_KEY = '__root__'
  78. # these are types that are returned unchanged by deepcopy
  79. IMMUTABLE_NON_COLLECTIONS_TYPES: Set[Type[Any]] = {
  80. int,
  81. float,
  82. complex,
  83. str,
  84. bool,
  85. bytes,
  86. type,
  87. NoneType,
  88. FunctionType,
  89. BuiltinFunctionType,
  90. LambdaType,
  91. weakref.ref,
  92. CodeType,
  93. # note: including ModuleType will differ from behaviour of deepcopy by not producing error.
  94. # It might be not a good idea in general, but considering that this function used only internally
  95. # against default values of fields, this will allow to actually have a field with module as default value
  96. ModuleType,
  97. NotImplemented.__class__,
  98. Ellipsis.__class__,
  99. }
  100. # these are types that if empty, might be copied with simple copy() instead of deepcopy()
  101. BUILTIN_COLLECTIONS: Set[Type[Any]] = {
  102. list,
  103. set,
  104. tuple,
  105. frozenset,
  106. dict,
  107. OrderedDict,
  108. defaultdict,
  109. deque,
  110. }
  111. def import_string(dotted_path: str) -> Any:
  112. """
  113. Stolen approximately from django. Import a dotted module path and return the attribute/class designated by the
  114. last name in the path. Raise ImportError if the import fails.
  115. """
  116. from importlib import import_module
  117. try:
  118. module_path, class_name = dotted_path.strip(' ').rsplit('.', 1)
  119. except ValueError as e:
  120. raise ImportError(f'"{dotted_path}" doesn\'t look like a module path') from e
  121. module = import_module(module_path)
  122. try:
  123. return getattr(module, class_name)
  124. except AttributeError as e:
  125. raise ImportError(f'Module "{module_path}" does not define a "{class_name}" attribute') from e
  126. def truncate(v: Union[str], *, max_len: int = 80) -> str:
  127. """
  128. Truncate a value and add a unicode ellipsis (three dots) to the end if it was too long
  129. """
  130. warnings.warn('`truncate` is no-longer used by pydantic and is deprecated', DeprecationWarning)
  131. if isinstance(v, str) and len(v) > (max_len - 2):
  132. # -3 so quote + string + … + quote has correct length
  133. return (v[: (max_len - 3)] + '…').__repr__()
  134. try:
  135. v = v.__repr__()
  136. except TypeError:
  137. v = v.__class__.__repr__(v) # in case v is a type
  138. if len(v) > max_len:
  139. v = v[: max_len - 1] + '…'
  140. return v
  141. def sequence_like(v: Any) -> bool:
  142. return isinstance(v, (list, tuple, set, frozenset, GeneratorType, deque))
  143. def validate_field_name(bases: List[Type['BaseModel']], field_name: str) -> None:
  144. """
  145. Ensure that the field's name does not shadow an existing attribute of the model.
  146. """
  147. for base in bases:
  148. if getattr(base, field_name, None):
  149. raise NameError(
  150. f'Field name "{field_name}" shadows a BaseModel attribute; '
  151. f'use a different field name with "alias=\'{field_name}\'".'
  152. )
  153. def lenient_isinstance(o: Any, class_or_tuple: Union[Type[Any], Tuple[Type[Any], ...], None]) -> bool:
  154. try:
  155. return isinstance(o, class_or_tuple) # type: ignore[arg-type]
  156. except TypeError:
  157. return False
  158. def lenient_issubclass(cls: Any, class_or_tuple: Union[Type[Any], Tuple[Type[Any], ...], None]) -> bool:
  159. try:
  160. return isinstance(cls, type) and issubclass(cls, class_or_tuple) # type: ignore[arg-type]
  161. except TypeError:
  162. if isinstance(cls, WithArgsTypes):
  163. return False
  164. raise # pragma: no cover
  165. def in_ipython() -> bool:
  166. """
  167. Check whether we're in an ipython environment, including jupyter notebooks.
  168. """
  169. try:
  170. eval('__IPYTHON__')
  171. except NameError:
  172. return False
  173. else: # pragma: no cover
  174. return True
  175. def is_valid_identifier(identifier: str) -> bool:
  176. """
  177. Checks that a string is a valid identifier and not a Python keyword.
  178. :param identifier: The identifier to test.
  179. :return: True if the identifier is valid.
  180. """
  181. return identifier.isidentifier() and not keyword.iskeyword(identifier)
  182. KeyType = TypeVar('KeyType')
  183. def deep_update(mapping: Dict[KeyType, Any], *updating_mappings: Dict[KeyType, Any]) -> Dict[KeyType, Any]:
  184. updated_mapping = mapping.copy()
  185. for updating_mapping in updating_mappings:
  186. for k, v in updating_mapping.items():
  187. if k in updated_mapping and isinstance(updated_mapping[k], dict) and isinstance(v, dict):
  188. updated_mapping[k] = deep_update(updated_mapping[k], v)
  189. else:
  190. updated_mapping[k] = v
  191. return updated_mapping
  192. def update_not_none(mapping: Dict[Any, Any], **update: Any) -> None:
  193. mapping.update({k: v for k, v in update.items() if v is not None})
  194. def almost_equal_floats(value_1: float, value_2: float, *, delta: float = 1e-8) -> bool:
  195. """
  196. Return True if two floats are almost equal
  197. """
  198. return abs(value_1 - value_2) <= delta
  199. def generate_model_signature(
  200. init: Callable[..., None], fields: Dict[str, 'ModelField'], config: Type['BaseConfig']
  201. ) -> 'Signature':
  202. """
  203. Generate signature for model based on its fields
  204. """
  205. from inspect import Parameter, Signature, signature
  206. from .config import Extra
  207. present_params = signature(init).parameters.values()
  208. merged_params: Dict[str, Parameter] = {}
  209. var_kw = None
  210. use_var_kw = False
  211. for param in islice(present_params, 1, None): # skip self arg
  212. if param.kind is param.VAR_KEYWORD:
  213. var_kw = param
  214. continue
  215. merged_params[param.name] = param
  216. if var_kw: # if custom init has no var_kw, fields which are not declared in it cannot be passed through
  217. allow_names = config.allow_population_by_field_name
  218. for field_name, field in fields.items():
  219. param_name = field.alias
  220. if field_name in merged_params or param_name in merged_params:
  221. continue
  222. elif not is_valid_identifier(param_name):
  223. if allow_names and is_valid_identifier(field_name):
  224. param_name = field_name
  225. else:
  226. use_var_kw = True
  227. continue
  228. # TODO: replace annotation with actual expected types once #1055 solved
  229. kwargs = {'default': field.default} if not field.required else {}
  230. merged_params[param_name] = Parameter(
  231. param_name, Parameter.KEYWORD_ONLY, annotation=field.annotation, **kwargs
  232. )
  233. if config.extra is Extra.allow:
  234. use_var_kw = True
  235. if var_kw and use_var_kw:
  236. # Make sure the parameter for extra kwargs
  237. # does not have the same name as a field
  238. default_model_signature = [
  239. ('__pydantic_self__', Parameter.POSITIONAL_OR_KEYWORD),
  240. ('data', Parameter.VAR_KEYWORD),
  241. ]
  242. if [(p.name, p.kind) for p in present_params] == default_model_signature:
  243. # if this is the standard model signature, use extra_data as the extra args name
  244. var_kw_name = 'extra_data'
  245. else:
  246. # else start from var_kw
  247. var_kw_name = var_kw.name
  248. # generate a name that's definitely unique
  249. while var_kw_name in fields:
  250. var_kw_name += '_'
  251. merged_params[var_kw_name] = var_kw.replace(name=var_kw_name)
  252. return Signature(parameters=list(merged_params.values()), return_annotation=None)
  253. def get_model(obj: Union[Type['BaseModel'], Type['Dataclass']]) -> Type['BaseModel']:
  254. from .main import BaseModel
  255. try:
  256. model_cls = obj.__pydantic_model__ # type: ignore
  257. except AttributeError:
  258. model_cls = obj
  259. if not issubclass(model_cls, BaseModel):
  260. raise TypeError('Unsupported type, must be either BaseModel or dataclass')
  261. return model_cls
  262. def to_camel(string: str) -> str:
  263. return ''.join(word.capitalize() for word in string.split('_'))
  264. def to_lower_camel(string: str) -> str:
  265. if len(string) >= 1:
  266. pascal_string = to_camel(string)
  267. return pascal_string[0].lower() + pascal_string[1:]
  268. return string.lower()
  269. T = TypeVar('T')
  270. def unique_list(
  271. input_list: Union[List[T], Tuple[T, ...]],
  272. *,
  273. name_factory: Callable[[T], str] = str,
  274. ) -> List[T]:
  275. """
  276. Make a list unique while maintaining order.
  277. We update the list if another one with the same name is set
  278. (e.g. root validator overridden in subclass)
  279. """
  280. result: List[T] = []
  281. result_names: List[str] = []
  282. for v in input_list:
  283. v_name = name_factory(v)
  284. if v_name not in result_names:
  285. result_names.append(v_name)
  286. result.append(v)
  287. else:
  288. result[result_names.index(v_name)] = v
  289. return result
  290. class PyObjectStr(str):
  291. """
  292. String class where repr doesn't include quotes. Useful with Representation when you want to return a string
  293. representation of something that valid (or pseudo-valid) python.
  294. """
  295. def __repr__(self) -> str:
  296. return str(self)
  297. class Representation:
  298. """
  299. Mixin to provide __str__, __repr__, and __pretty__ methods. See #884 for more details.
  300. __pretty__ is used by [devtools](https://python-devtools.helpmanual.io/) to provide human readable representations
  301. of objects.
  302. """
  303. __slots__: Tuple[str, ...] = tuple()
  304. def __repr_args__(self) -> 'ReprArgs':
  305. """
  306. Returns the attributes to show in __str__, __repr__, and __pretty__ this is generally overridden.
  307. Can either return:
  308. * name - value pairs, e.g.: `[('foo_name', 'foo'), ('bar_name', ['b', 'a', 'r'])]`
  309. * or, just values, e.g.: `[(None, 'foo'), (None, ['b', 'a', 'r'])]`
  310. """
  311. attrs = ((s, getattr(self, s)) for s in self.__slots__)
  312. return [(a, v) for a, v in attrs if v is not None]
  313. def __repr_name__(self) -> str:
  314. """
  315. Name of the instance's class, used in __repr__.
  316. """
  317. return self.__class__.__name__
  318. def __repr_str__(self, join_str: str) -> str:
  319. return join_str.join(repr(v) if a is None else f'{a}={v!r}' for a, v in self.__repr_args__())
  320. def __pretty__(self, fmt: Callable[[Any], Any], **kwargs: Any) -> Generator[Any, None, None]:
  321. """
  322. Used by devtools (https://python-devtools.helpmanual.io/) to provide a human readable representations of objects
  323. """
  324. yield self.__repr_name__() + '('
  325. yield 1
  326. for name, value in self.__repr_args__():
  327. if name is not None:
  328. yield name + '='
  329. yield fmt(value)
  330. yield ','
  331. yield 0
  332. yield -1
  333. yield ')'
  334. def __str__(self) -> str:
  335. return self.__repr_str__(' ')
  336. def __repr__(self) -> str:
  337. return f'{self.__repr_name__()}({self.__repr_str__(", ")})'
  338. def __rich_repr__(self) -> 'RichReprResult':
  339. """Get fields for Rich library"""
  340. for name, field_repr in self.__repr_args__():
  341. if name is None:
  342. yield field_repr
  343. else:
  344. yield name, field_repr
  345. class GetterDict(Representation):
  346. """
  347. Hack to make object's smell just enough like dicts for validate_model.
  348. We can't inherit from Mapping[str, Any] because it upsets cython so we have to implement all methods ourselves.
  349. """
  350. __slots__ = ('_obj',)
  351. def __init__(self, obj: Any):
  352. self._obj = obj
  353. def __getitem__(self, key: str) -> Any:
  354. try:
  355. return getattr(self._obj, key)
  356. except AttributeError as e:
  357. raise KeyError(key) from e
  358. def get(self, key: Any, default: Any = None) -> Any:
  359. return getattr(self._obj, key, default)
  360. def extra_keys(self) -> Set[Any]:
  361. """
  362. We don't want to get any other attributes of obj if the model didn't explicitly ask for them
  363. """
  364. return set()
  365. def keys(self) -> List[Any]:
  366. """
  367. Keys of the pseudo dictionary, uses a list not set so order information can be maintained like python
  368. dictionaries.
  369. """
  370. return list(self)
  371. def values(self) -> List[Any]:
  372. return [self[k] for k in self]
  373. def items(self) -> Iterator[Tuple[str, Any]]:
  374. for k in self:
  375. yield k, self.get(k)
  376. def __iter__(self) -> Iterator[str]:
  377. for name in dir(self._obj):
  378. if not name.startswith('_'):
  379. yield name
  380. def __len__(self) -> int:
  381. return sum(1 for _ in self)
  382. def __contains__(self, item: Any) -> bool:
  383. return item in self.keys()
  384. def __eq__(self, other: Any) -> bool:
  385. return dict(self) == dict(other.items())
  386. def __repr_args__(self) -> 'ReprArgs':
  387. return [(None, dict(self))]
  388. def __repr_name__(self) -> str:
  389. return f'GetterDict[{display_as_type(self._obj)}]'
  390. class ValueItems(Representation):
  391. """
  392. Class for more convenient calculation of excluded or included fields on values.
  393. """
  394. __slots__ = ('_items', '_type')
  395. def __init__(self, value: Any, items: Union['AbstractSetIntStr', 'MappingIntStrAny']) -> None:
  396. items = self._coerce_items(items)
  397. if isinstance(value, (list, tuple)):
  398. items = self._normalize_indexes(items, len(value))
  399. self._items: 'MappingIntStrAny' = items
  400. def is_excluded(self, item: Any) -> bool:
  401. """
  402. Check if item is fully excluded.
  403. :param item: key or index of a value
  404. """
  405. return self.is_true(self._items.get(item))
  406. def is_included(self, item: Any) -> bool:
  407. """
  408. Check if value is contained in self._items
  409. :param item: key or index of value
  410. """
  411. return item in self._items
  412. def for_element(self, e: 'IntStr') -> Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']]:
  413. """
  414. :param e: key or index of element on value
  415. :return: raw values for element if self._items is dict and contain needed element
  416. """
  417. item = self._items.get(e)
  418. return item if not self.is_true(item) else None
  419. def _normalize_indexes(self, items: 'MappingIntStrAny', v_length: int) -> 'DictIntStrAny':
  420. """
  421. :param items: dict or set of indexes which will be normalized
  422. :param v_length: length of sequence indexes of which will be
  423. >>> self._normalize_indexes({0: True, -2: True, -1: True}, 4)
  424. {0: True, 2: True, 3: True}
  425. >>> self._normalize_indexes({'__all__': True}, 4)
  426. {0: True, 1: True, 2: True, 3: True}
  427. """
  428. normalized_items: 'DictIntStrAny' = {}
  429. all_items = None
  430. for i, v in items.items():
  431. if not (isinstance(v, Mapping) or isinstance(v, AbstractSet) or self.is_true(v)):
  432. raise TypeError(f'Unexpected type of exclude value for index "{i}" {v.__class__}')
  433. if i == '__all__':
  434. all_items = self._coerce_value(v)
  435. continue
  436. if not isinstance(i, int):
  437. raise TypeError(
  438. 'Excluding fields from a sequence of sub-models or dicts must be performed index-wise: '
  439. 'expected integer keys or keyword "__all__"'
  440. )
  441. normalized_i = v_length + i if i < 0 else i
  442. normalized_items[normalized_i] = self.merge(v, normalized_items.get(normalized_i))
  443. if not all_items:
  444. return normalized_items
  445. if self.is_true(all_items):
  446. for i in range(v_length):
  447. normalized_items.setdefault(i, ...)
  448. return normalized_items
  449. for i in range(v_length):
  450. normalized_item = normalized_items.setdefault(i, {})
  451. if not self.is_true(normalized_item):
  452. normalized_items[i] = self.merge(all_items, normalized_item)
  453. return normalized_items
  454. @classmethod
  455. def merge(cls, base: Any, override: Any, intersect: bool = False) -> Any:
  456. """
  457. Merge a ``base`` item with an ``override`` item.
  458. Both ``base`` and ``override`` are converted to dictionaries if possible.
  459. Sets are converted to dictionaries with the sets entries as keys and
  460. Ellipsis as values.
  461. Each key-value pair existing in ``base`` is merged with ``override``,
  462. while the rest of the key-value pairs are updated recursively with this function.
  463. Merging takes place based on the "union" of keys if ``intersect`` is
  464. set to ``False`` (default) and on the intersection of keys if
  465. ``intersect`` is set to ``True``.
  466. """
  467. override = cls._coerce_value(override)
  468. base = cls._coerce_value(base)
  469. if override is None:
  470. return base
  471. if cls.is_true(base) or base is None:
  472. return override
  473. if cls.is_true(override):
  474. return base if intersect else override
  475. # intersection or union of keys while preserving ordering:
  476. if intersect:
  477. merge_keys = [k for k in base if k in override] + [k for k in override if k in base]
  478. else:
  479. merge_keys = list(base) + [k for k in override if k not in base]
  480. merged: 'DictIntStrAny' = {}
  481. for k in merge_keys:
  482. merged_item = cls.merge(base.get(k), override.get(k), intersect=intersect)
  483. if merged_item is not None:
  484. merged[k] = merged_item
  485. return merged
  486. @staticmethod
  487. def _coerce_items(items: Union['AbstractSetIntStr', 'MappingIntStrAny']) -> 'MappingIntStrAny':
  488. if isinstance(items, Mapping):
  489. pass
  490. elif isinstance(items, AbstractSet):
  491. items = dict.fromkeys(items, ...)
  492. else:
  493. class_name = getattr(items, '__class__', '???')
  494. assert_never(
  495. items,
  496. f'Unexpected type of exclude value {class_name}',
  497. )
  498. return items
  499. @classmethod
  500. def _coerce_value(cls, value: Any) -> Any:
  501. if value is None or cls.is_true(value):
  502. return value
  503. return cls._coerce_items(value)
  504. @staticmethod
  505. def is_true(v: Any) -> bool:
  506. return v is True or v is ...
  507. def __repr_args__(self) -> 'ReprArgs':
  508. return [(None, self._items)]
  509. class ClassAttribute:
  510. """
  511. Hide class attribute from its instances
  512. """
  513. __slots__ = (
  514. 'name',
  515. 'value',
  516. )
  517. def __init__(self, name: str, value: Any) -> None:
  518. self.name = name
  519. self.value = value
  520. def __get__(self, instance: Any, owner: Type[Any]) -> None:
  521. if instance is None:
  522. return self.value
  523. raise AttributeError(f'{self.name!r} attribute of {owner.__name__!r} is class-only')
  524. path_types = {
  525. 'is_dir': 'directory',
  526. 'is_file': 'file',
  527. 'is_mount': 'mount point',
  528. 'is_symlink': 'symlink',
  529. 'is_block_device': 'block device',
  530. 'is_char_device': 'char device',
  531. 'is_fifo': 'FIFO',
  532. 'is_socket': 'socket',
  533. }
  534. def path_type(p: 'Path') -> str:
  535. """
  536. Find out what sort of thing a path is.
  537. """
  538. assert p.exists(), 'path does not exist'
  539. for method, name in path_types.items():
  540. if getattr(p, method)():
  541. return name
  542. return 'unknown'
  543. Obj = TypeVar('Obj')
  544. def smart_deepcopy(obj: Obj) -> Obj:
  545. """
  546. Return type as is for immutable built-in types
  547. Use obj.copy() for built-in empty collections
  548. Use copy.deepcopy() for non-empty collections and unknown objects
  549. """
  550. obj_type = obj.__class__
  551. if obj_type in IMMUTABLE_NON_COLLECTIONS_TYPES:
  552. return obj # fastest case: obj is immutable and not collection therefore will not be copied anyway
  553. try:
  554. if not obj and obj_type in BUILTIN_COLLECTIONS:
  555. # faster way for empty collections, no need to copy its members
  556. return obj if obj_type is tuple else obj.copy() # type: ignore # tuple doesn't have copy method
  557. except (TypeError, ValueError, RuntimeError):
  558. # do we really dare to catch ALL errors? Seems a bit risky
  559. pass
  560. return deepcopy(obj) # slowest way when we actually might need a deepcopy
  561. def is_valid_field(name: str) -> bool:
  562. if not name.startswith('_'):
  563. return True
  564. return ROOT_KEY == name
  565. DUNDER_ATTRIBUTES = {
  566. '__annotations__',
  567. '__classcell__',
  568. '__doc__',
  569. '__module__',
  570. '__orig_bases__',
  571. '__orig_class__',
  572. '__qualname__',
  573. }
  574. def is_valid_private_name(name: str) -> bool:
  575. return not is_valid_field(name) and name not in DUNDER_ATTRIBUTES
  576. _EMPTY = object()
  577. def all_identical(left: Iterable[Any], right: Iterable[Any]) -> bool:
  578. """
  579. Check that the items of `left` are the same objects as those in `right`.
  580. >>> a, b = object(), object()
  581. >>> all_identical([a, b, a], [a, b, a])
  582. True
  583. >>> all_identical([a, b, [a]], [a, b, [a]]) # new list object, while "equal" is not "identical"
  584. False
  585. """
  586. for left_item, right_item in zip_longest(left, right, fillvalue=_EMPTY):
  587. if left_item is not right_item:
  588. return False
  589. return True
  590. def assert_never(obj: NoReturn, msg: str) -> NoReturn:
  591. """
  592. Helper to make sure that we have covered all possible types.
  593. This is mostly useful for ``mypy``, docs:
  594. https://mypy.readthedocs.io/en/latest/literal_types.html#exhaustive-checks
  595. """
  596. raise TypeError(msg)
  597. def get_unique_discriminator_alias(all_aliases: Collection[str], discriminator_key: str) -> str:
  598. """Validate that all aliases are the same and if that's the case return the alias"""
  599. unique_aliases = set(all_aliases)
  600. if len(unique_aliases) > 1:
  601. raise ConfigError(
  602. f'Aliases for discriminator {discriminator_key!r} must be the same (got {", ".join(sorted(all_aliases))})'
  603. )
  604. return unique_aliases.pop()
  605. def get_discriminator_alias_and_values(tp: Any, discriminator_key: str) -> Tuple[str, Tuple[str, ...]]:
  606. """
  607. Get alias and all valid values in the `Literal` type of the discriminator field
  608. `tp` can be a `BaseModel` class or directly an `Annotated` `Union` of many.
  609. """
  610. is_root_model = getattr(tp, '__custom_root_type__', False)
  611. if get_origin(tp) is Annotated:
  612. tp = get_args(tp)[0]
  613. if hasattr(tp, '__pydantic_model__'):
  614. tp = tp.__pydantic_model__
  615. if is_union(get_origin(tp)):
  616. alias, all_values = _get_union_alias_and_all_values(tp, discriminator_key)
  617. return alias, tuple(v for values in all_values for v in values)
  618. elif is_root_model:
  619. union_type = tp.__fields__[ROOT_KEY].type_
  620. alias, all_values = _get_union_alias_and_all_values(union_type, discriminator_key)
  621. if len(set(all_values)) > 1:
  622. raise ConfigError(
  623. f'Field {discriminator_key!r} is not the same for all submodels of {display_as_type(tp)!r}'
  624. )
  625. return alias, all_values[0]
  626. else:
  627. try:
  628. t_discriminator_type = tp.__fields__[discriminator_key].type_
  629. except AttributeError as e:
  630. raise TypeError(f'Type {tp.__name__!r} is not a valid `BaseModel` or `dataclass`') from e
  631. except KeyError as e:
  632. raise ConfigError(f'Model {tp.__name__!r} needs a discriminator field for key {discriminator_key!r}') from e
  633. if not is_literal_type(t_discriminator_type):
  634. raise ConfigError(f'Field {discriminator_key!r} of model {tp.__name__!r} needs to be a `Literal`')
  635. return tp.__fields__[discriminator_key].alias, all_literal_values(t_discriminator_type)
  636. def _get_union_alias_and_all_values(
  637. union_type: Type[Any], discriminator_key: str
  638. ) -> Tuple[str, Tuple[Tuple[str, ...], ...]]:
  639. zipped_aliases_values = [get_discriminator_alias_and_values(t, discriminator_key) for t in get_args(union_type)]
  640. # unzip: [('alias_a',('v1', 'v2)), ('alias_b', ('v3',))] => [('alias_a', 'alias_b'), (('v1', 'v2'), ('v3',))]
  641. all_aliases, all_values = zip(*zipped_aliases_values)
  642. return get_unique_discriminator_alias(all_aliases, discriminator_key), all_values