base.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. import copy
  2. import sys
  3. from abc import ABC, abstractmethod
  4. from collections import defaultdict
  5. from dataclasses import dataclass, field
  6. from enum import Enum
  7. from typing import Any, Dict, Iterator, List, Optional, Set, Tuple, Type, Union
  8. from antlr4 import ParserRuleContext
  9. from ._utils import (
  10. _DEFAULT_MARKER_,
  11. NoneType,
  12. ValueKind,
  13. _get_value,
  14. _is_interpolation,
  15. _is_missing_value,
  16. _is_special,
  17. format_and_raise,
  18. get_value_kind,
  19. is_union_annotation,
  20. is_valid_value_annotation,
  21. split_key,
  22. type_str,
  23. )
  24. from .errors import (
  25. ConfigKeyError,
  26. ConfigTypeError,
  27. InterpolationKeyError,
  28. InterpolationResolutionError,
  29. InterpolationToMissingValueError,
  30. InterpolationValidationError,
  31. MissingMandatoryValue,
  32. UnsupportedInterpolationType,
  33. ValidationError,
  34. )
  35. from .grammar.gen.OmegaConfGrammarParser import OmegaConfGrammarParser
  36. from .grammar_parser import parse
  37. from .grammar_visitor import GrammarVisitor
  38. DictKeyType = Union[str, bytes, int, Enum, float, bool]
  39. @dataclass
  40. class Metadata:
  41. ref_type: Union[Type[Any], Any]
  42. object_type: Union[Type[Any], Any]
  43. optional: bool
  44. key: Any
  45. # Flags have 3 modes:
  46. # unset : inherit from parent (None if no parent specifies)
  47. # set to true: flag is true
  48. # set to false: flag is false
  49. flags: Optional[Dict[str, bool]] = None
  50. # If True, when checking the value of a flag, if the flag is not set None is returned
  51. # otherwise, the parent node is queried.
  52. flags_root: bool = False
  53. resolver_cache: Dict[str, Any] = field(default_factory=lambda: defaultdict(dict))
  54. def __post_init__(self) -> None:
  55. if self.flags is None:
  56. self.flags = {}
  57. @property
  58. def type_hint(self) -> Union[Type[Any], Any]:
  59. """Compute `type_hint` from `self.optional` and `self.ref_type`"""
  60. # For compatibility with pickled OmegaConf objects created using older
  61. # versions of OmegaConf, we store `ref_type` and `object_type`
  62. # separately (rather than storing `type_hint` directly).
  63. if self.optional:
  64. return Optional[self.ref_type]
  65. else:
  66. return self.ref_type
  67. @dataclass
  68. class ContainerMetadata(Metadata):
  69. key_type: Any = None
  70. element_type: Any = None
  71. def __post_init__(self) -> None:
  72. if self.ref_type is None:
  73. self.ref_type = Any
  74. assert self.key_type is Any or isinstance(self.key_type, type)
  75. if self.element_type is not None:
  76. if not is_valid_value_annotation(self.element_type):
  77. raise ValidationError(
  78. f"Unsupported value type: '{type_str(self.element_type, include_module_name=True)}'"
  79. )
  80. if self.flags is None:
  81. self.flags = {}
  82. class Node(ABC):
  83. _metadata: Metadata
  84. _parent: Optional["Box"]
  85. _flags_cache: Optional[Dict[str, Optional[bool]]]
  86. def __init__(self, parent: Optional["Box"], metadata: Metadata):
  87. self.__dict__["_metadata"] = metadata
  88. self.__dict__["_parent"] = parent
  89. self.__dict__["_flags_cache"] = None
  90. def __getstate__(self) -> Dict[str, Any]:
  91. # Overridden to ensure that the flags cache is cleared on serialization.
  92. state_dict = copy.copy(self.__dict__)
  93. del state_dict["_flags_cache"]
  94. return state_dict
  95. def __setstate__(self, state_dict: Dict[str, Any]) -> None:
  96. self.__dict__.update(state_dict)
  97. self.__dict__["_flags_cache"] = None
  98. def _set_parent(self, parent: Optional["Box"]) -> None:
  99. assert parent is None or isinstance(parent, Box)
  100. self.__dict__["_parent"] = parent
  101. self._invalidate_flags_cache()
  102. def _invalidate_flags_cache(self) -> None:
  103. self.__dict__["_flags_cache"] = None
  104. def _get_parent(self) -> Optional["Box"]:
  105. parent = self.__dict__["_parent"]
  106. assert parent is None or isinstance(parent, Box)
  107. return parent
  108. def _get_parent_container(self) -> Optional["Container"]:
  109. """
  110. Like _get_parent, but returns the grandparent
  111. in the case where `self` is wrapped by a UnionNode.
  112. """
  113. parent = self.__dict__["_parent"]
  114. assert parent is None or isinstance(parent, Box)
  115. if isinstance(parent, UnionNode):
  116. grandparent = parent.__dict__["_parent"]
  117. assert grandparent is None or isinstance(grandparent, Container)
  118. return grandparent
  119. else:
  120. assert parent is None or isinstance(parent, Container)
  121. return parent
  122. def _set_flag(
  123. self,
  124. flags: Union[List[str], str],
  125. values: Union[List[Optional[bool]], Optional[bool]],
  126. ) -> "Node":
  127. if isinstance(flags, str):
  128. flags = [flags]
  129. if values is None or isinstance(values, bool):
  130. values = [values]
  131. if len(values) == 1:
  132. values = len(flags) * values
  133. if len(flags) != len(values):
  134. raise ValueError("Inconsistent lengths of input flag names and values")
  135. for idx, flag in enumerate(flags):
  136. value = values[idx]
  137. if value is None:
  138. assert self._metadata.flags is not None
  139. if flag in self._metadata.flags:
  140. del self._metadata.flags[flag]
  141. else:
  142. assert self._metadata.flags is not None
  143. self._metadata.flags[flag] = value
  144. self._invalidate_flags_cache()
  145. return self
  146. def _get_node_flag(self, flag: str) -> Optional[bool]:
  147. """
  148. :param flag: flag to inspect
  149. :return: the state of the flag on this node.
  150. """
  151. assert self._metadata.flags is not None
  152. return self._metadata.flags.get(flag)
  153. def _get_flag(self, flag: str) -> Optional[bool]:
  154. cache = self.__dict__["_flags_cache"]
  155. if cache is None:
  156. cache = self.__dict__["_flags_cache"] = {}
  157. ret = cache.get(flag, _DEFAULT_MARKER_)
  158. if ret is _DEFAULT_MARKER_:
  159. ret = self._get_flag_no_cache(flag)
  160. cache[flag] = ret
  161. assert ret is None or isinstance(ret, bool)
  162. return ret
  163. def _get_flag_no_cache(self, flag: str) -> Optional[bool]:
  164. """
  165. Returns True if this config node flag is set
  166. A flag is set if node.set_flag(True) was called
  167. or one if it's parents is flag is set
  168. :return:
  169. """
  170. flags = self._metadata.flags
  171. assert flags is not None
  172. if flag in flags and flags[flag] is not None:
  173. return flags[flag]
  174. if self._is_flags_root():
  175. return None
  176. parent = self._get_parent()
  177. if parent is None:
  178. return None
  179. else:
  180. # noinspection PyProtectedMember
  181. return parent._get_flag(flag)
  182. def _format_and_raise(
  183. self,
  184. key: Any,
  185. value: Any,
  186. cause: Exception,
  187. msg: Optional[str] = None,
  188. type_override: Any = None,
  189. ) -> None:
  190. format_and_raise(
  191. node=self,
  192. key=key,
  193. value=value,
  194. msg=str(cause) if msg is None else msg,
  195. cause=cause,
  196. type_override=type_override,
  197. )
  198. assert False
  199. @abstractmethod
  200. def _get_full_key(self, key: Optional[Union[DictKeyType, int]]) -> str:
  201. ...
  202. def _dereference_node(self) -> "Node":
  203. node = self._dereference_node_impl(throw_on_resolution_failure=True)
  204. assert node is not None
  205. return node
  206. def _maybe_dereference_node(
  207. self,
  208. throw_on_resolution_failure: bool = False,
  209. memo: Optional[Set[int]] = None,
  210. ) -> Optional["Node"]:
  211. return self._dereference_node_impl(
  212. throw_on_resolution_failure=throw_on_resolution_failure,
  213. memo=memo,
  214. )
  215. def _dereference_node_impl(
  216. self,
  217. throw_on_resolution_failure: bool,
  218. memo: Optional[Set[int]] = None,
  219. ) -> Optional["Node"]:
  220. if not self._is_interpolation():
  221. return self
  222. parent = self._get_parent_container()
  223. if parent is None:
  224. if throw_on_resolution_failure:
  225. raise InterpolationResolutionError(
  226. "Cannot resolve interpolation for a node without a parent"
  227. )
  228. return None
  229. assert parent is not None
  230. key = self._key()
  231. return parent._resolve_interpolation_from_parse_tree(
  232. parent=parent,
  233. key=key,
  234. value=self,
  235. parse_tree=parse(_get_value(self)),
  236. throw_on_resolution_failure=throw_on_resolution_failure,
  237. memo=memo,
  238. )
  239. def _get_root(self) -> "Container":
  240. root: Optional[Box] = self._get_parent()
  241. if root is None:
  242. assert isinstance(self, Container)
  243. return self
  244. assert root is not None and isinstance(root, Box)
  245. while root._get_parent() is not None:
  246. root = root._get_parent()
  247. assert root is not None and isinstance(root, Box)
  248. assert root is not None and isinstance(root, Container)
  249. return root
  250. def _is_missing(self) -> bool:
  251. """
  252. Check if the node's value is `???` (does *not* resolve interpolations).
  253. """
  254. return _is_missing_value(self)
  255. def _is_none(self) -> bool:
  256. """
  257. Check if the node's value is `None` (does *not* resolve interpolations).
  258. """
  259. return self._value() is None
  260. @abstractmethod
  261. def __eq__(self, other: Any) -> bool:
  262. ...
  263. @abstractmethod
  264. def __ne__(self, other: Any) -> bool:
  265. ...
  266. @abstractmethod
  267. def __hash__(self) -> int:
  268. ...
  269. @abstractmethod
  270. def _value(self) -> Any:
  271. ...
  272. @abstractmethod
  273. def _set_value(self, value: Any, flags: Optional[Dict[str, bool]] = None) -> None:
  274. ...
  275. @abstractmethod
  276. def _is_optional(self) -> bool:
  277. ...
  278. @abstractmethod
  279. def _is_interpolation(self) -> bool:
  280. ...
  281. def _key(self) -> Any:
  282. return self._metadata.key
  283. def _set_key(self, key: Any) -> None:
  284. self._metadata.key = key
  285. def _is_flags_root(self) -> bool:
  286. return self._metadata.flags_root
  287. def _set_flags_root(self, flags_root: bool) -> None:
  288. if self._metadata.flags_root != flags_root:
  289. self._metadata.flags_root = flags_root
  290. self._invalidate_flags_cache()
  291. def _has_ref_type(self) -> bool:
  292. return self._metadata.ref_type is not Any
  293. class Box(Node):
  294. """
  295. Base class for nodes that can contain other nodes.
  296. Concrete subclasses include DictConfig, ListConfig, and UnionNode.
  297. """
  298. _content: Any
  299. def __init__(self, parent: Optional["Box"], metadata: Metadata):
  300. super().__init__(parent=parent, metadata=metadata)
  301. self.__dict__["_content"] = None
  302. def __copy__(self) -> Any:
  303. # real shallow copy is impossible because of the reference to the parent.
  304. return copy.deepcopy(self)
  305. def _re_parent(self) -> None:
  306. from .dictconfig import DictConfig
  307. from .listconfig import ListConfig
  308. # update parents of first level Config nodes to self
  309. if isinstance(self, DictConfig):
  310. content = self.__dict__["_content"]
  311. if isinstance(content, dict):
  312. for _key, value in self.__dict__["_content"].items():
  313. if value is not None:
  314. value._set_parent(self)
  315. if isinstance(value, Box):
  316. value._re_parent()
  317. elif isinstance(self, ListConfig):
  318. content = self.__dict__["_content"]
  319. if isinstance(content, list):
  320. for item in self.__dict__["_content"]:
  321. if item is not None:
  322. item._set_parent(self)
  323. if isinstance(item, Box):
  324. item._re_parent()
  325. elif isinstance(self, UnionNode):
  326. content = self.__dict__["_content"]
  327. if isinstance(content, Node):
  328. content._set_parent(self)
  329. if isinstance(content, Box): # pragma: no cover
  330. # No coverage here as support for containers inside
  331. # UnionNode is not yet implemented
  332. content._re_parent()
  333. class Container(Box):
  334. """
  335. Container tagging interface
  336. """
  337. _metadata: ContainerMetadata
  338. @abstractmethod
  339. def _get_child(
  340. self,
  341. key: Any,
  342. validate_access: bool = True,
  343. validate_key: bool = True,
  344. throw_on_missing_value: bool = False,
  345. throw_on_missing_key: bool = False,
  346. ) -> Union[Optional[Node], List[Optional[Node]]]:
  347. ...
  348. @abstractmethod
  349. def _get_node(
  350. self,
  351. key: Any,
  352. validate_access: bool = True,
  353. validate_key: bool = True,
  354. throw_on_missing_value: bool = False,
  355. throw_on_missing_key: bool = False,
  356. ) -> Union[Optional[Node], List[Optional[Node]]]:
  357. ...
  358. @abstractmethod
  359. def __delitem__(self, key: Any) -> None:
  360. ...
  361. @abstractmethod
  362. def __setitem__(self, key: Any, value: Any) -> None:
  363. ...
  364. @abstractmethod
  365. def __iter__(self) -> Iterator[Any]:
  366. ...
  367. @abstractmethod
  368. def __getitem__(self, key_or_index: Any) -> Any:
  369. ...
  370. def _resolve_key_and_root(self, key: str) -> Tuple["Container", str]:
  371. orig = key
  372. if not key.startswith("."):
  373. return self._get_root(), key
  374. else:
  375. root: Optional[Container] = self
  376. assert key.startswith(".")
  377. while True:
  378. assert root is not None
  379. key = key[1:]
  380. if not key.startswith("."):
  381. break
  382. root = root._get_parent_container()
  383. if root is None:
  384. raise ConfigKeyError(f"Error resolving key '{orig}'")
  385. return root, key
  386. def _select_impl(
  387. self,
  388. key: str,
  389. throw_on_missing: bool,
  390. throw_on_resolution_failure: bool,
  391. memo: Optional[Set[int]] = None,
  392. ) -> Tuple[Optional["Container"], Optional[str], Optional[Node]]:
  393. """
  394. Select a value using dot separated key sequence
  395. """
  396. from .omegaconf import _select_one
  397. if key == "":
  398. return self, "", self
  399. split = split_key(key)
  400. root: Optional[Container] = self
  401. for i in range(len(split) - 1):
  402. if root is None:
  403. break
  404. k = split[i]
  405. ret, _ = _select_one(
  406. c=root,
  407. key=k,
  408. throw_on_missing=throw_on_missing,
  409. throw_on_type_error=throw_on_resolution_failure,
  410. )
  411. if isinstance(ret, Node):
  412. ret = ret._maybe_dereference_node(
  413. throw_on_resolution_failure=throw_on_resolution_failure,
  414. memo=memo,
  415. )
  416. if ret is not None and not isinstance(ret, Container):
  417. parent_key = ".".join(split[0 : i + 1])
  418. child_key = split[i + 1]
  419. raise ConfigTypeError(
  420. f"Error trying to access {key}: node `{parent_key}` "
  421. f"is not a container and thus cannot contain `{child_key}`"
  422. )
  423. root = ret
  424. if root is None:
  425. return None, None, None
  426. last_key = split[-1]
  427. value, _ = _select_one(
  428. c=root,
  429. key=last_key,
  430. throw_on_missing=throw_on_missing,
  431. throw_on_type_error=throw_on_resolution_failure,
  432. )
  433. if value is None:
  434. return root, last_key, None
  435. if memo is not None:
  436. vid = id(value)
  437. if vid in memo:
  438. raise InterpolationResolutionError("Recursive interpolation detected")
  439. # push to memo "stack"
  440. memo.add(vid)
  441. try:
  442. value = root._maybe_resolve_interpolation(
  443. parent=root,
  444. key=last_key,
  445. value=value,
  446. throw_on_resolution_failure=throw_on_resolution_failure,
  447. memo=memo,
  448. )
  449. finally:
  450. if memo is not None:
  451. # pop from memo "stack"
  452. memo.remove(vid)
  453. return root, last_key, value
  454. def _resolve_interpolation_from_parse_tree(
  455. self,
  456. parent: Optional["Container"],
  457. value: "Node",
  458. key: Any,
  459. parse_tree: OmegaConfGrammarParser.ConfigValueContext,
  460. throw_on_resolution_failure: bool,
  461. memo: Optional[Set[int]],
  462. ) -> Optional["Node"]:
  463. """
  464. Resolve an interpolation.
  465. This happens in two steps:
  466. 1. The parse tree is visited, which outputs either a `Node` (e.g.,
  467. for node interpolations "${foo}"), a string (e.g., for string
  468. interpolations "hello ${name}", or any other arbitrary value
  469. (e.g., or custom interpolations "${foo:bar}").
  470. 2. This output is potentially validated and converted when the node
  471. being resolved (`value`) is typed.
  472. If an error occurs in one of the above steps, an `InterpolationResolutionError`
  473. (or a subclass of it) is raised, *unless* `throw_on_resolution_failure` is set
  474. to `False` (in which case the return value is `None`).
  475. :param parent: Parent of the node being resolved.
  476. :param value: Node being resolved.
  477. :param key: The associated key in the parent.
  478. :param parse_tree: The parse tree as obtained from `grammar_parser.parse()`.
  479. :param throw_on_resolution_failure: If `False`, then exceptions raised during
  480. the resolution of the interpolation are silenced, and instead `None` is
  481. returned.
  482. :return: A `Node` that contains the interpolation result. This may be an existing
  483. node in the config (in the case of a node interpolation "${foo}"), or a new
  484. node that is created to wrap the interpolated value. It is `None` if and only if
  485. `throw_on_resolution_failure` is `False` and an error occurs during resolution.
  486. """
  487. try:
  488. resolved = self.resolve_parse_tree(
  489. parse_tree=parse_tree, node=value, key=key, memo=memo
  490. )
  491. except InterpolationResolutionError:
  492. if throw_on_resolution_failure:
  493. raise
  494. return None
  495. return self._validate_and_convert_interpolation_result(
  496. parent=parent,
  497. value=value,
  498. key=key,
  499. resolved=resolved,
  500. throw_on_resolution_failure=throw_on_resolution_failure,
  501. )
  502. def _validate_and_convert_interpolation_result(
  503. self,
  504. parent: Optional["Container"],
  505. value: "Node",
  506. key: Any,
  507. resolved: Any,
  508. throw_on_resolution_failure: bool,
  509. ) -> Optional["Node"]:
  510. from .nodes import AnyNode, InterpolationResultNode, ValueNode
  511. # If the output is not a Node already (e.g., because it is the output of a
  512. # custom resolver), then we will need to wrap it within a Node.
  513. must_wrap = not isinstance(resolved, Node)
  514. # If the node is typed, validate (and possibly convert) the result.
  515. if isinstance(value, ValueNode) and not isinstance(value, AnyNode):
  516. res_value = _get_value(resolved)
  517. try:
  518. conv_value = value.validate_and_convert(res_value)
  519. except ValidationError as e:
  520. if throw_on_resolution_failure:
  521. self._format_and_raise(
  522. key=key,
  523. value=res_value,
  524. cause=e,
  525. msg=f"While dereferencing interpolation '{value}': {e}",
  526. type_override=InterpolationValidationError,
  527. )
  528. return None
  529. # If the converted value is of the same type, it means that no conversion
  530. # was actually needed. As a result, we can keep the original `resolved`
  531. # (and otherwise, the converted value must be wrapped into a new node).
  532. if type(conv_value) != type(res_value):
  533. must_wrap = True
  534. resolved = conv_value
  535. if must_wrap:
  536. return InterpolationResultNode(value=resolved, key=key, parent=parent)
  537. else:
  538. assert isinstance(resolved, Node)
  539. return resolved
  540. def _validate_not_dereferencing_to_parent(self, node: Node, target: Node) -> None:
  541. parent: Optional[Node] = node
  542. while parent is not None:
  543. if parent is target:
  544. raise InterpolationResolutionError(
  545. "Interpolation to parent node detected"
  546. )
  547. parent = parent._get_parent()
  548. def _resolve_node_interpolation(
  549. self, inter_key: str, memo: Optional[Set[int]]
  550. ) -> "Node":
  551. """A node interpolation is of the form `${foo.bar}`"""
  552. try:
  553. root_node, inter_key = self._resolve_key_and_root(inter_key)
  554. except ConfigKeyError as exc:
  555. raise InterpolationKeyError(
  556. f"ConfigKeyError while resolving interpolation: {exc}"
  557. ).with_traceback(sys.exc_info()[2])
  558. try:
  559. parent, last_key, value = root_node._select_impl(
  560. inter_key,
  561. throw_on_missing=True,
  562. throw_on_resolution_failure=True,
  563. memo=memo,
  564. )
  565. except MissingMandatoryValue as exc:
  566. raise InterpolationToMissingValueError(
  567. f"MissingMandatoryValue while resolving interpolation: {exc}"
  568. ).with_traceback(sys.exc_info()[2])
  569. if parent is None or value is None:
  570. raise InterpolationKeyError(f"Interpolation key '{inter_key}' not found")
  571. else:
  572. self._validate_not_dereferencing_to_parent(node=self, target=value)
  573. return value
  574. def _evaluate_custom_resolver(
  575. self,
  576. key: Any,
  577. node: Node,
  578. inter_type: str,
  579. inter_args: Tuple[Any, ...],
  580. inter_args_str: Tuple[str, ...],
  581. ) -> Any:
  582. from omegaconf import OmegaConf
  583. resolver = OmegaConf._get_resolver(inter_type)
  584. if resolver is not None:
  585. root_node = self._get_root()
  586. return resolver(
  587. root_node,
  588. self,
  589. node,
  590. inter_args,
  591. inter_args_str,
  592. )
  593. else:
  594. raise UnsupportedInterpolationType(
  595. f"Unsupported interpolation type {inter_type}"
  596. )
  597. def _maybe_resolve_interpolation(
  598. self,
  599. parent: Optional["Container"],
  600. key: Any,
  601. value: Node,
  602. throw_on_resolution_failure: bool,
  603. memo: Optional[Set[int]] = None,
  604. ) -> Optional[Node]:
  605. value_kind = get_value_kind(value)
  606. if value_kind != ValueKind.INTERPOLATION:
  607. return value
  608. parse_tree = parse(_get_value(value))
  609. return self._resolve_interpolation_from_parse_tree(
  610. parent=parent,
  611. value=value,
  612. key=key,
  613. parse_tree=parse_tree,
  614. throw_on_resolution_failure=throw_on_resolution_failure,
  615. memo=memo if memo is not None else set(),
  616. )
  617. def resolve_parse_tree(
  618. self,
  619. parse_tree: ParserRuleContext,
  620. node: Node,
  621. memo: Optional[Set[int]] = None,
  622. key: Optional[Any] = None,
  623. ) -> Any:
  624. """
  625. Resolve a given parse tree into its value.
  626. We make no assumption here on the type of the tree's root, so that the
  627. return value may be of any type.
  628. """
  629. def node_interpolation_callback(
  630. inter_key: str, memo: Optional[Set[int]]
  631. ) -> Optional["Node"]:
  632. return self._resolve_node_interpolation(inter_key=inter_key, memo=memo)
  633. def resolver_interpolation_callback(
  634. name: str, args: Tuple[Any, ...], args_str: Tuple[str, ...]
  635. ) -> Any:
  636. return self._evaluate_custom_resolver(
  637. key=key,
  638. node=node,
  639. inter_type=name,
  640. inter_args=args,
  641. inter_args_str=args_str,
  642. )
  643. visitor = GrammarVisitor(
  644. node_interpolation_callback=node_interpolation_callback,
  645. resolver_interpolation_callback=resolver_interpolation_callback,
  646. memo=memo,
  647. )
  648. try:
  649. return visitor.visit(parse_tree)
  650. except InterpolationResolutionError:
  651. raise
  652. except Exception as exc:
  653. # Other kinds of exceptions are wrapped in an `InterpolationResolutionError`.
  654. raise InterpolationResolutionError(
  655. f"{type(exc).__name__} raised while resolving interpolation: {exc}"
  656. ).with_traceback(sys.exc_info()[2])
  657. def _invalidate_flags_cache(self) -> None:
  658. from .dictconfig import DictConfig
  659. from .listconfig import ListConfig
  660. # invalidate subtree cache only if the cache is initialized in this node.
  661. if self.__dict__["_flags_cache"] is not None:
  662. self.__dict__["_flags_cache"] = None
  663. if isinstance(self, DictConfig):
  664. content = self.__dict__["_content"]
  665. if isinstance(content, dict):
  666. for value in self.__dict__["_content"].values():
  667. value._invalidate_flags_cache()
  668. elif isinstance(self, ListConfig):
  669. content = self.__dict__["_content"]
  670. if isinstance(content, list):
  671. for item in self.__dict__["_content"]:
  672. item._invalidate_flags_cache()
  673. class SCMode(Enum):
  674. DICT = 1 # Convert to plain dict
  675. DICT_CONFIG = 2 # Keep as OmegaConf DictConfig
  676. INSTANTIATE = 3 # Create a dataclass or attrs class instance
  677. class UnionNode(Box):
  678. """
  679. This class handles Union type hints. The `_content` attribute is either a
  680. child node that is compatible with the given Union ref_type, or it is a
  681. special value (None or MISSING or interpolation).
  682. Much of the logic for e.g. value assignment and type validation is
  683. delegated to the child node. As such, UnionNode functions as a
  684. "pass-through" node. User apps and downstream libraries should not need to
  685. know about UnionNode (assuming they only use OmegaConf's public API).
  686. """
  687. _parent: Optional[Container]
  688. _content: Union[Node, None, str]
  689. def __init__(
  690. self,
  691. content: Any,
  692. ref_type: Any,
  693. is_optional: bool = True,
  694. key: Any = None,
  695. parent: Optional[Box] = None,
  696. ) -> None:
  697. try:
  698. if not is_union_annotation(ref_type): # pragma: no cover
  699. msg = (
  700. f"UnionNode got unexpected ref_type {ref_type}. Please file a bug"
  701. + " report at https://github.com/omry/omegaconf/issues"
  702. )
  703. raise AssertionError(msg)
  704. if not isinstance(parent, (Container, NoneType)):
  705. raise ConfigTypeError("Parent type is not omegaconf.Container")
  706. super().__init__(
  707. parent=parent,
  708. metadata=Metadata(
  709. ref_type=ref_type,
  710. object_type=None,
  711. optional=is_optional,
  712. key=key,
  713. flags={"convert": False},
  714. ),
  715. )
  716. self._set_value(content)
  717. except Exception as ex:
  718. format_and_raise(node=None, key=key, value=content, msg=str(ex), cause=ex)
  719. def _get_full_key(self, key: Optional[Union[DictKeyType, int]]) -> str:
  720. parent = self._get_parent()
  721. if parent is None:
  722. if self._metadata.key is None:
  723. return ""
  724. else:
  725. return str(self._metadata.key)
  726. else:
  727. return parent._get_full_key(self._metadata.key)
  728. def __eq__(self, other: Any) -> bool:
  729. content = self.__dict__["_content"]
  730. if isinstance(content, Node):
  731. ret = content.__eq__(other)
  732. elif isinstance(other, Node):
  733. ret = other.__eq__(content)
  734. else:
  735. ret = content.__eq__(other)
  736. assert isinstance(ret, (bool, type(NotImplemented)))
  737. return ret
  738. def __ne__(self, other: Any) -> bool:
  739. x = self.__eq__(other)
  740. if x is NotImplemented:
  741. return NotImplemented
  742. return not x
  743. def __hash__(self) -> int:
  744. return hash(self.__dict__["_content"])
  745. def _value(self) -> Union[Node, None, str]:
  746. content = self.__dict__["_content"]
  747. assert isinstance(content, (Node, NoneType, str))
  748. return content
  749. def _set_value(self, value: Any, flags: Optional[Dict[str, bool]] = None) -> None:
  750. previous_content = self.__dict__["_content"]
  751. previous_metadata = self.__dict__["_metadata"]
  752. try:
  753. self._set_value_impl(value, flags)
  754. except Exception as e:
  755. self.__dict__["_content"] = previous_content
  756. self.__dict__["_metadata"] = previous_metadata
  757. raise e
  758. def _set_value_impl(
  759. self, value: Any, flags: Optional[Dict[str, bool]] = None
  760. ) -> None:
  761. from omegaconf.omegaconf import _node_wrap
  762. ref_type = self._metadata.ref_type
  763. type_hint = self._metadata.type_hint
  764. value = _get_value(value)
  765. if _is_special(value):
  766. assert isinstance(value, (str, NoneType))
  767. if value is None:
  768. if not self._is_optional():
  769. raise ValidationError(
  770. f"Value '$VALUE' is incompatible with type hint '{type_str(type_hint)}'"
  771. )
  772. self.__dict__["_content"] = value
  773. elif isinstance(value, Container):
  774. raise ValidationError(
  775. f"Cannot assign container '$VALUE' of type '$VALUE_TYPE' to {type_str(type_hint)}"
  776. )
  777. else:
  778. for candidate_ref_type in ref_type.__args__:
  779. try:
  780. self.__dict__["_content"] = _node_wrap(
  781. value=value,
  782. ref_type=candidate_ref_type,
  783. is_optional=False,
  784. key=None,
  785. parent=self,
  786. )
  787. break
  788. except ValidationError:
  789. continue
  790. else:
  791. raise ValidationError(
  792. f"Value '$VALUE' of type '$VALUE_TYPE' is incompatible with type hint '{type_str(type_hint)}'"
  793. )
  794. def _is_optional(self) -> bool:
  795. return self.__dict__["_metadata"].optional is True
  796. def _is_interpolation(self) -> bool:
  797. return _is_interpolation(self.__dict__["_content"])
  798. def __str__(self) -> str:
  799. return str(self.__dict__["_content"])
  800. def __repr__(self) -> str:
  801. return repr(self.__dict__["_content"])
  802. def __deepcopy__(self, memo: Dict[int, Any]) -> "UnionNode":
  803. res = object.__new__(type(self))
  804. for key, value in self.__dict__.items():
  805. if key not in ("_content", "_parent"):
  806. res.__dict__[key] = copy.deepcopy(value, memo=memo)
  807. src_content = self.__dict__["_content"]
  808. if isinstance(src_content, Node):
  809. old_parent = src_content.__dict__["_parent"]
  810. try:
  811. src_content.__dict__["_parent"] = None
  812. content_copy = copy.deepcopy(src_content, memo=memo)
  813. content_copy.__dict__["_parent"] = res
  814. finally:
  815. src_content.__dict__["_parent"] = old_parent
  816. else:
  817. # None and strings can be assigned as is
  818. content_copy = src_content
  819. res.__dict__["_content"] = content_copy
  820. res.__dict__["_parent"] = self.__dict__["_parent"]
  821. return res