_utils.py 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. import copy
  2. import os
  3. import pathlib
  4. import re
  5. import string
  6. import sys
  7. import types
  8. import warnings
  9. from contextlib import contextmanager
  10. from enum import Enum
  11. from textwrap import dedent
  12. from typing import (
  13. Any,
  14. Dict,
  15. Iterator,
  16. List,
  17. Optional,
  18. Tuple,
  19. Type,
  20. Union,
  21. get_type_hints,
  22. )
  23. import yaml
  24. from .errors import (
  25. ConfigIndexError,
  26. ConfigTypeError,
  27. ConfigValueError,
  28. GrammarParseError,
  29. OmegaConfBaseException,
  30. ValidationError,
  31. )
  32. from .grammar_parser import SIMPLE_INTERPOLATION_PATTERN, parse
  33. try:
  34. import dataclasses
  35. except ImportError: # pragma: no cover
  36. dataclasses = None # type: ignore # pragma: no cover
  37. try:
  38. import attr
  39. except ImportError: # pragma: no cover
  40. attr = None # type: ignore # pragma: no cover
  41. NoneType: Type[None] = type(None)
  42. BUILTIN_VALUE_TYPES: Tuple[Type[Any], ...] = (
  43. int,
  44. float,
  45. bool,
  46. str,
  47. bytes,
  48. NoneType,
  49. )
  50. # Regexprs to match key paths like: a.b, a[b], ..a[c].d, etc.
  51. # We begin by matching the head (in these examples: a, a, ..a).
  52. # This can be read as "dots followed by any character but `.` or `[`"
  53. # Note that a key starting with brackets, like [a], is purposedly *not*
  54. # matched here and will instead be handled in the next regex below (this
  55. # is to keep this regex simple).
  56. KEY_PATH_HEAD = re.compile(r"(\.)*[^.[]*")
  57. # Then we match other keys. The following expression matches one key and can
  58. # be read as a choice between two syntaxes:
  59. # - `.` followed by anything except `.` or `[` (ex: .b, .d)
  60. # - `[` followed by anything then `]` (ex: [b], [c])
  61. KEY_PATH_OTHER = re.compile(r"\.([^.[]*)|\[(.*?)\]")
  62. # source: https://yaml.org/type/bool.html
  63. YAML_BOOL_TYPES = [
  64. "y",
  65. "Y",
  66. "yes",
  67. "Yes",
  68. "YES",
  69. "n",
  70. "N",
  71. "no",
  72. "No",
  73. "NO",
  74. "true",
  75. "True",
  76. "TRUE",
  77. "false",
  78. "False",
  79. "FALSE",
  80. "on",
  81. "On",
  82. "ON",
  83. "off",
  84. "Off",
  85. "OFF",
  86. ]
  87. class Marker:
  88. def __init__(self, desc: str):
  89. self.desc = desc
  90. def __repr__(self) -> str:
  91. return self.desc
  92. # To be used as default value when `None` is not an option.
  93. _DEFAULT_MARKER_: Any = Marker("_DEFAULT_MARKER_")
  94. class OmegaConfDumper(yaml.Dumper): # type: ignore
  95. str_representer_added = False
  96. @staticmethod
  97. def str_representer(dumper: yaml.Dumper, data: str) -> yaml.ScalarNode:
  98. with_quotes = yaml_is_bool(data) or is_int(data) or is_float(data)
  99. return dumper.represent_scalar(
  100. yaml.resolver.BaseResolver.DEFAULT_SCALAR_TAG,
  101. data,
  102. style=("'" if with_quotes else None),
  103. )
  104. def get_omega_conf_dumper() -> Type[OmegaConfDumper]:
  105. if not OmegaConfDumper.str_representer_added:
  106. OmegaConfDumper.add_representer(str, OmegaConfDumper.str_representer)
  107. OmegaConfDumper.str_representer_added = True
  108. return OmegaConfDumper
  109. def yaml_is_bool(b: str) -> bool:
  110. return b in YAML_BOOL_TYPES
  111. def get_yaml_loader() -> Any:
  112. class OmegaConfLoader(yaml.SafeLoader): # type: ignore
  113. def construct_mapping(self, node: yaml.Node, deep: bool = False) -> Any:
  114. keys = set()
  115. for key_node, value_node in node.value:
  116. if key_node.tag != yaml.resolver.BaseResolver.DEFAULT_SCALAR_TAG:
  117. continue
  118. if key_node.value in keys:
  119. raise yaml.constructor.ConstructorError(
  120. "while constructing a mapping",
  121. node.start_mark,
  122. f"found duplicate key {key_node.value}",
  123. key_node.start_mark,
  124. )
  125. keys.add(key_node.value)
  126. return super().construct_mapping(node, deep=deep)
  127. loader = OmegaConfLoader
  128. loader.add_implicit_resolver(
  129. "tag:yaml.org,2002:float",
  130. re.compile(
  131. """^(?:
  132. [-+]?[0-9]+(?:_[0-9]+)*\\.[0-9_]*(?:[eE][-+]?[0-9]+)?
  133. |[-+]?[0-9]+(?:_[0-9]+)*(?:[eE][-+]?[0-9]+)
  134. |\\.[0-9]+(?:_[0-9]+)*(?:[eE][-+][0-9]+)?
  135. |[-+]?[0-9]+(?:_[0-9]+)*(?::[0-5]?[0-9])+\\.[0-9_]*
  136. |[-+]?\\.(?:inf|Inf|INF)
  137. |\\.(?:nan|NaN|NAN))$""",
  138. re.X,
  139. ),
  140. list("-+0123456789."),
  141. )
  142. loader.yaml_implicit_resolvers = {
  143. key: [
  144. (tag, regexp)
  145. for tag, regexp in resolvers
  146. if tag != "tag:yaml.org,2002:timestamp"
  147. ]
  148. for key, resolvers in loader.yaml_implicit_resolvers.items()
  149. }
  150. loader.add_constructor(
  151. "tag:yaml.org,2002:python/object/apply:pathlib.Path",
  152. lambda loader, node: pathlib.Path(*loader.construct_sequence(node)),
  153. )
  154. loader.add_constructor(
  155. "tag:yaml.org,2002:python/object/apply:pathlib.PosixPath",
  156. lambda loader, node: pathlib.PosixPath(*loader.construct_sequence(node)),
  157. )
  158. loader.add_constructor(
  159. "tag:yaml.org,2002:python/object/apply:pathlib.WindowsPath",
  160. lambda loader, node: pathlib.WindowsPath(*loader.construct_sequence(node)),
  161. )
  162. return loader
  163. def _get_class(path: str) -> type:
  164. from importlib import import_module
  165. module_path, _, class_name = path.rpartition(".")
  166. mod = import_module(module_path)
  167. try:
  168. klass: type = getattr(mod, class_name)
  169. except AttributeError:
  170. raise ImportError(f"Class {class_name} is not in module {module_path}")
  171. return klass
  172. def is_union_annotation(type_: Any) -> bool:
  173. if sys.version_info >= (3, 10): # pragma: no cover
  174. if isinstance(type_, types.UnionType):
  175. return True
  176. return getattr(type_, "__origin__", None) is Union
  177. def _resolve_optional(type_: Any) -> Tuple[bool, Any]:
  178. """Check whether `type_` is equivalent to `typing.Optional[T]` for some T."""
  179. if is_union_annotation(type_):
  180. args = type_.__args__
  181. if NoneType in args:
  182. optional = True
  183. args = tuple(a for a in args if a is not NoneType)
  184. else:
  185. optional = False
  186. if len(args) == 1:
  187. return optional, args[0]
  188. elif len(args) >= 2:
  189. return optional, Union[args]
  190. else:
  191. assert False
  192. if type_ is Any:
  193. return True, Any
  194. if type_ in (None, NoneType):
  195. return True, NoneType
  196. return False, type_
  197. def _is_optional(obj: Any, key: Optional[Union[int, str]] = None) -> bool:
  198. """Check `obj` metadata to see if the given node is optional."""
  199. from .base import Container, Node
  200. if key is not None:
  201. assert isinstance(obj, Container)
  202. obj = obj._get_node(key)
  203. assert isinstance(obj, Node)
  204. return obj._is_optional()
  205. def _resolve_forward(type_: Type[Any], module: str) -> Type[Any]:
  206. import typing # lgtm [py/import-and-import-from]
  207. forward = typing.ForwardRef if hasattr(typing, "ForwardRef") else typing._ForwardRef # type: ignore
  208. if type(type_) is forward:
  209. return _get_class(f"{module}.{type_.__forward_arg__}")
  210. else:
  211. if is_dict_annotation(type_):
  212. kt, vt = get_dict_key_value_types(type_)
  213. if kt is not None:
  214. kt = _resolve_forward(kt, module=module)
  215. if vt is not None:
  216. vt = _resolve_forward(vt, module=module)
  217. return Dict[kt, vt] # type: ignore
  218. if is_list_annotation(type_):
  219. et = get_list_element_type(type_)
  220. if et is not None:
  221. et = _resolve_forward(et, module=module)
  222. return List[et] # type: ignore
  223. if is_tuple_annotation(type_):
  224. its = get_tuple_item_types(type_)
  225. its = tuple(_resolve_forward(it, module=module) for it in its)
  226. return Tuple[its] # type: ignore
  227. return type_
  228. def extract_dict_subclass_data(obj: Any, parent: Any) -> Optional[Dict[str, Any]]:
  229. """Check if obj is an instance of a subclass of Dict. If so, extract the Dict keys/values."""
  230. from omegaconf.omegaconf import _maybe_wrap
  231. is_type = isinstance(obj, type)
  232. obj_type = obj if is_type else type(obj)
  233. subclasses_dict = is_dict_subclass(obj_type)
  234. if subclasses_dict:
  235. warnings.warn(
  236. f"Class `{obj_type.__name__}` subclasses `Dict`."
  237. + " Subclassing `Dict` in Structured Config classes is deprecated,"
  238. + " see github.com/omry/omegaconf/issues/663",
  239. UserWarning,
  240. stacklevel=9,
  241. )
  242. if is_type:
  243. return None
  244. elif subclasses_dict:
  245. dict_subclass_data = {}
  246. key_type, element_type = get_dict_key_value_types(obj_type)
  247. for name, value in obj.items():
  248. is_optional, type_ = _resolve_optional(element_type)
  249. type_ = _resolve_forward(type_, obj.__module__)
  250. try:
  251. dict_subclass_data[name] = _maybe_wrap(
  252. ref_type=type_,
  253. is_optional=is_optional,
  254. key=name,
  255. value=value,
  256. parent=parent,
  257. )
  258. except ValidationError as ex:
  259. format_and_raise(
  260. node=None, key=name, value=value, cause=ex, msg=str(ex)
  261. )
  262. return dict_subclass_data
  263. else:
  264. return None
  265. def get_attr_class_fields(obj: Any) -> List["attr.Attribute[Any]"]:
  266. is_type = isinstance(obj, type)
  267. obj_type = obj if is_type else type(obj)
  268. fields = attr.fields_dict(obj_type).values()
  269. return [f for f in fields if f.metadata.get("omegaconf_ignore") is not True]
  270. def get_attr_data(obj: Any, allow_objects: Optional[bool] = None) -> Dict[str, Any]:
  271. from omegaconf.omegaconf import OmegaConf, _maybe_wrap
  272. flags = {"allow_objects": allow_objects} if allow_objects is not None else {}
  273. from omegaconf import MISSING
  274. d = {}
  275. is_type = isinstance(obj, type)
  276. obj_type = obj if is_type else type(obj)
  277. dummy_parent = OmegaConf.create({}, flags=flags)
  278. dummy_parent._metadata.object_type = obj_type
  279. resolved_hints = get_type_hints(obj_type)
  280. for attrib in get_attr_class_fields(obj):
  281. name = attrib.name
  282. is_optional, type_ = _resolve_optional(resolved_hints[name])
  283. type_ = _resolve_forward(type_, obj.__module__)
  284. if not is_type:
  285. value = getattr(obj, name)
  286. else:
  287. value = attrib.default
  288. if value == attr.NOTHING:
  289. value = MISSING
  290. if is_union_annotation(type_) and not is_supported_union_annotation(type_):
  291. e = ConfigValueError(
  292. f"Unions of containers are not supported:\n{name}: {type_str(type_)}"
  293. )
  294. format_and_raise(node=None, key=None, value=value, cause=e, msg=str(e))
  295. try:
  296. d[name] = _maybe_wrap(
  297. ref_type=type_,
  298. is_optional=is_optional,
  299. key=name,
  300. value=value,
  301. parent=dummy_parent,
  302. )
  303. except (ValidationError, GrammarParseError) as ex:
  304. format_and_raise(
  305. node=dummy_parent, key=name, value=value, cause=ex, msg=str(ex)
  306. )
  307. d[name]._set_parent(None)
  308. dict_subclass_data = extract_dict_subclass_data(obj=obj, parent=dummy_parent)
  309. if dict_subclass_data is not None:
  310. d.update(dict_subclass_data)
  311. return d
  312. def get_dataclass_fields(obj: Any) -> List["dataclasses.Field[Any]"]:
  313. fields = dataclasses.fields(obj)
  314. return [f for f in fields if f.metadata.get("omegaconf_ignore") is not True]
  315. def get_dataclass_data(
  316. obj: Any, allow_objects: Optional[bool] = None
  317. ) -> Dict[str, Any]:
  318. from omegaconf.omegaconf import MISSING, OmegaConf, _maybe_wrap
  319. flags = {"allow_objects": allow_objects} if allow_objects is not None else {}
  320. d = {}
  321. is_type = isinstance(obj, type)
  322. obj_type = get_type_of(obj)
  323. dummy_parent = OmegaConf.create({}, flags=flags)
  324. dummy_parent._metadata.object_type = obj_type
  325. resolved_hints = get_type_hints(obj_type)
  326. for field in get_dataclass_fields(obj):
  327. name = field.name
  328. is_optional, type_ = _resolve_optional(resolved_hints[field.name])
  329. type_ = _resolve_forward(type_, obj.__module__)
  330. has_default = field.default != dataclasses.MISSING
  331. has_default_factory = field.default_factory != dataclasses.MISSING
  332. if not is_type:
  333. value = getattr(obj, name)
  334. else:
  335. if has_default:
  336. value = field.default
  337. elif has_default_factory:
  338. value = field.default_factory() # type: ignore
  339. else:
  340. value = MISSING
  341. if is_union_annotation(type_) and not is_supported_union_annotation(type_):
  342. e = ConfigValueError(
  343. f"Unions of containers are not supported:\n{name}: {type_str(type_)}"
  344. )
  345. format_and_raise(node=None, key=None, value=value, cause=e, msg=str(e))
  346. try:
  347. d[name] = _maybe_wrap(
  348. ref_type=type_,
  349. is_optional=is_optional,
  350. key=name,
  351. value=value,
  352. parent=dummy_parent,
  353. )
  354. except (ValidationError, GrammarParseError) as ex:
  355. format_and_raise(
  356. node=dummy_parent, key=name, value=value, cause=ex, msg=str(ex)
  357. )
  358. d[name]._set_parent(None)
  359. dict_subclass_data = extract_dict_subclass_data(obj=obj, parent=dummy_parent)
  360. if dict_subclass_data is not None:
  361. d.update(dict_subclass_data)
  362. return d
  363. def is_dataclass(obj: Any) -> bool:
  364. from omegaconf.base import Node
  365. if dataclasses is None or isinstance(obj, Node):
  366. return False
  367. return dataclasses.is_dataclass(obj)
  368. def is_attr_class(obj: Any) -> bool:
  369. from omegaconf.base import Node
  370. if attr is None or isinstance(obj, Node):
  371. return False
  372. return attr.has(obj)
  373. def is_structured_config(obj: Any) -> bool:
  374. return is_attr_class(obj) or is_dataclass(obj)
  375. def is_dataclass_frozen(type_: Any) -> bool:
  376. return type_.__dataclass_params__.frozen # type: ignore
  377. def is_attr_frozen(type_: type) -> bool:
  378. # This is very hacky and probably fragile as well.
  379. # Unfortunately currently there isn't an official API in attr that can detect that.
  380. # noinspection PyProtectedMember
  381. return type_.__setattr__ == attr._make._frozen_setattrs # type: ignore
  382. def get_type_of(class_or_object: Any) -> Type[Any]:
  383. type_ = class_or_object
  384. if not isinstance(type_, type):
  385. type_ = type(class_or_object)
  386. assert isinstance(type_, type)
  387. return type_
  388. def is_structured_config_frozen(obj: Any) -> bool:
  389. type_ = get_type_of(obj)
  390. if is_dataclass(type_):
  391. return is_dataclass_frozen(type_)
  392. if is_attr_class(type_):
  393. return is_attr_frozen(type_)
  394. return False
  395. def get_structured_config_init_field_names(obj: Any) -> List[str]:
  396. fields: Union[List["dataclasses.Field[Any]"], List["attr.Attribute[Any]"]]
  397. if is_dataclass(obj):
  398. fields = get_dataclass_fields(obj)
  399. elif is_attr_class(obj):
  400. fields = get_attr_class_fields(obj)
  401. else:
  402. raise ValueError(f"Unsupported type: {type(obj).__name__}")
  403. return [f.name for f in fields if f.init]
  404. def get_structured_config_data(
  405. obj: Any, allow_objects: Optional[bool] = None
  406. ) -> Dict[str, Any]:
  407. if is_dataclass(obj):
  408. return get_dataclass_data(obj, allow_objects=allow_objects)
  409. elif is_attr_class(obj):
  410. return get_attr_data(obj, allow_objects=allow_objects)
  411. else:
  412. raise ValueError(f"Unsupported type: {type(obj).__name__}")
  413. class ValueKind(Enum):
  414. VALUE = 0
  415. MANDATORY_MISSING = 1
  416. INTERPOLATION = 2
  417. def _is_missing_value(value: Any) -> bool:
  418. from omegaconf import Node
  419. if isinstance(value, Node):
  420. value = value._value()
  421. return _is_missing_literal(value)
  422. def _is_missing_literal(value: Any) -> bool:
  423. # Uses literal '???' instead of the MISSING const for performance reasons.
  424. return isinstance(value, str) and value == "???"
  425. def _is_none(
  426. value: Any, resolve: bool = False, throw_on_resolution_failure: bool = True
  427. ) -> bool:
  428. from omegaconf import Node
  429. if not isinstance(value, Node):
  430. return value is None
  431. if resolve:
  432. value = value._maybe_dereference_node(
  433. throw_on_resolution_failure=throw_on_resolution_failure
  434. )
  435. if not throw_on_resolution_failure and value is None:
  436. # Resolution failure: consider that it is *not* None.
  437. return False
  438. assert isinstance(value, Node)
  439. return value._is_none()
  440. def get_value_kind(
  441. value: Any, strict_interpolation_validation: bool = False
  442. ) -> ValueKind:
  443. """
  444. Determine the kind of a value
  445. Examples:
  446. VALUE: "10", "20", True
  447. MANDATORY_MISSING: "???"
  448. INTERPOLATION: "${foo.bar}", "${foo.${bar}}", "${foo:bar}", "[${foo}, ${bar}]",
  449. "ftp://${host}/path", "${foo:${bar}, [true], {'baz': ${baz}}}"
  450. :param value: Input to classify.
  451. :param strict_interpolation_validation: If `True`, then when `value` is a string
  452. containing "${", it is parsed to validate the interpolation syntax. If `False`,
  453. this parsing step is skipped: this is more efficient, but will not detect errors.
  454. """
  455. if _is_missing_value(value):
  456. return ValueKind.MANDATORY_MISSING
  457. if _is_interpolation(value, strict_interpolation_validation):
  458. return ValueKind.INTERPOLATION
  459. return ValueKind.VALUE
  460. def _is_interpolation(v: Any, strict_interpolation_validation: bool = False) -> bool:
  461. from omegaconf import Node
  462. if isinstance(v, Node):
  463. v = v._value()
  464. if isinstance(v, str) and _is_interpolation_string(
  465. v, strict_interpolation_validation
  466. ):
  467. return True
  468. return False
  469. def _is_interpolation_string(value: str, strict_interpolation_validation: bool) -> bool:
  470. # We identify potential interpolations by the presence of "${" in the string.
  471. # Note that escaped interpolations (ex: "esc: \${bar}") are identified as
  472. # interpolations: this is intended, since they must be processed as interpolations
  473. # for the string to be properly un-escaped.
  474. # Keep in mind that invalid interpolations will only be detected when
  475. # `strict_interpolation_validation` is True.
  476. if "${" in value:
  477. if strict_interpolation_validation:
  478. # First try the cheap regex matching that detects common interpolations.
  479. if SIMPLE_INTERPOLATION_PATTERN.match(value) is None:
  480. # If no match, do the more expensive grammar parsing to detect errors.
  481. parse(value)
  482. return True
  483. return False
  484. def _is_special(value: Any) -> bool:
  485. """Special values are None, MISSING, and interpolation."""
  486. return _is_none(value) or get_value_kind(value) in (
  487. ValueKind.MANDATORY_MISSING,
  488. ValueKind.INTERPOLATION,
  489. )
  490. def is_float(st: str) -> bool:
  491. try:
  492. float(st)
  493. return True
  494. except ValueError:
  495. return False
  496. def is_int(st: str) -> bool:
  497. try:
  498. int(st)
  499. return True
  500. except ValueError:
  501. return False
  502. def is_primitive_list(obj: Any) -> bool:
  503. return isinstance(obj, (list, tuple))
  504. def is_primitive_dict(obj: Any) -> bool:
  505. t = get_type_of(obj)
  506. return t is dict
  507. def is_dict_annotation(type_: Any) -> bool:
  508. if type_ in (dict, Dict):
  509. return True
  510. origin = getattr(type_, "__origin__", None)
  511. # type_dict is a bit hard to detect.
  512. # this support is tentative, if it eventually causes issues in other areas it may be dropped.
  513. if sys.version_info < (3, 7, 0): # pragma: no cover
  514. typed_dict = hasattr(type_, "__base__") and type_.__base__ == Dict
  515. return origin is Dict or type_ is Dict or typed_dict
  516. else: # pragma: no cover
  517. typed_dict = hasattr(type_, "__base__") and type_.__base__ == dict
  518. return origin is dict or typed_dict
  519. def is_list_annotation(type_: Any) -> bool:
  520. if type_ in (list, List):
  521. return True
  522. origin = getattr(type_, "__origin__", None)
  523. if sys.version_info < (3, 7, 0):
  524. return origin is List or type_ is List # pragma: no cover
  525. else:
  526. return origin is list # pragma: no cover
  527. def is_tuple_annotation(type_: Any) -> bool:
  528. if type_ in (tuple, Tuple):
  529. return True
  530. origin = getattr(type_, "__origin__", None)
  531. if sys.version_info < (3, 7, 0):
  532. return origin is Tuple or type_ is Tuple # pragma: no cover
  533. else:
  534. return origin is tuple # pragma: no cover
  535. def is_supported_union_annotation(obj: Any) -> bool:
  536. """Currently only primitive types are supported in Unions, e.g. Union[int, str]"""
  537. if not is_union_annotation(obj):
  538. return False
  539. args = obj.__args__
  540. return all(is_primitive_type_annotation(arg) for arg in args)
  541. def is_dict_subclass(type_: Any) -> bool:
  542. return type_ is not None and isinstance(type_, type) and issubclass(type_, Dict)
  543. def is_dict(obj: Any) -> bool:
  544. return is_primitive_dict(obj) or is_dict_annotation(obj) or is_dict_subclass(obj)
  545. def is_primitive_container(obj: Any) -> bool:
  546. return is_primitive_list(obj) or is_primitive_dict(obj)
  547. def get_list_element_type(ref_type: Optional[Type[Any]]) -> Any:
  548. args = getattr(ref_type, "__args__", None)
  549. if ref_type is not List and args is not None and args[0]:
  550. element_type = args[0]
  551. else:
  552. element_type = Any
  553. return element_type
  554. def get_tuple_item_types(ref_type: Type[Any]) -> Tuple[Any, ...]:
  555. args = getattr(ref_type, "__args__", None)
  556. if args in (None, ()):
  557. args = (Any, ...)
  558. assert isinstance(args, tuple)
  559. return args
  560. def get_dict_key_value_types(ref_type: Any) -> Tuple[Any, Any]:
  561. args = getattr(ref_type, "__args__", None)
  562. if args is None:
  563. bases = getattr(ref_type, "__orig_bases__", None)
  564. if bases is not None and len(bases) > 0:
  565. args = getattr(bases[0], "__args__", None)
  566. key_type: Any
  567. element_type: Any
  568. if ref_type is None or ref_type == Dict:
  569. key_type = Any
  570. element_type = Any
  571. else:
  572. if args is not None:
  573. key_type = args[0]
  574. element_type = args[1]
  575. else:
  576. key_type = Any
  577. element_type = Any
  578. return key_type, element_type
  579. def is_valid_value_annotation(type_: Any) -> bool:
  580. _, type_ = _resolve_optional(type_)
  581. return (
  582. type_ is Any
  583. or is_primitive_type_annotation(type_)
  584. or is_structured_config(type_)
  585. or is_container_annotation(type_)
  586. or is_supported_union_annotation(type_)
  587. )
  588. def _valid_dict_key_annotation_type(type_: Any) -> bool:
  589. from omegaconf import DictKeyType
  590. return type_ is None or type_ is Any or issubclass(type_, DictKeyType.__args__) # type: ignore
  591. def is_primitive_type_annotation(type_: Any) -> bool:
  592. type_ = get_type_of(type_)
  593. return issubclass(type_, (Enum, pathlib.Path)) or type_ in BUILTIN_VALUE_TYPES
  594. def _get_value(value: Any) -> Any:
  595. from .base import Container, UnionNode
  596. from .nodes import ValueNode
  597. if isinstance(value, ValueNode):
  598. return value._value()
  599. elif isinstance(value, Container):
  600. boxed = value._value()
  601. if boxed is None or _is_missing_literal(boxed) or _is_interpolation(boxed):
  602. return boxed
  603. elif isinstance(value, UnionNode):
  604. boxed = value._value()
  605. if boxed is None or _is_missing_literal(boxed) or _is_interpolation(boxed):
  606. return boxed
  607. else:
  608. return _get_value(boxed) # pass through value of boxed node
  609. # return primitives and regular OmegaConf Containers as is
  610. return value
  611. def get_type_hint(obj: Any, key: Any = None) -> Optional[Type[Any]]:
  612. from omegaconf import Container, Node
  613. if isinstance(obj, Container):
  614. if key is not None:
  615. obj = obj._get_node(key)
  616. else:
  617. if key is not None:
  618. raise ValueError("Key must only be provided when obj is a container")
  619. if isinstance(obj, Node):
  620. ref_type = obj._metadata.ref_type
  621. if obj._is_optional() and ref_type is not Any:
  622. return Optional[ref_type] # type: ignore
  623. else:
  624. return ref_type
  625. else:
  626. return Any # type: ignore
  627. def _raise(ex: Exception, cause: Exception) -> None:
  628. # Set the environment variable OC_CAUSE=1 to get a stacktrace that includes the
  629. # causing exception.
  630. env_var = os.environ["OC_CAUSE"] if "OC_CAUSE" in os.environ else None
  631. debugging = sys.gettrace() is not None
  632. full_backtrace = (debugging and not env_var == "0") or (env_var == "1")
  633. if full_backtrace:
  634. ex.__cause__ = cause
  635. else:
  636. ex.__cause__ = None
  637. raise ex.with_traceback(sys.exc_info()[2]) # set env var OC_CAUSE=1 for full trace
  638. def format_and_raise(
  639. node: Any,
  640. key: Any,
  641. value: Any,
  642. msg: str,
  643. cause: Exception,
  644. type_override: Any = None,
  645. ) -> None:
  646. from omegaconf import OmegaConf
  647. from omegaconf.base import Node
  648. if isinstance(cause, AssertionError):
  649. raise
  650. if isinstance(cause, OmegaConfBaseException) and cause._initialized:
  651. ex = cause
  652. if type_override is not None:
  653. ex = type_override(str(cause))
  654. ex.__dict__ = copy.deepcopy(cause.__dict__)
  655. _raise(ex, cause)
  656. object_type: Optional[Type[Any]]
  657. object_type_str: Optional[str] = None
  658. ref_type: Optional[Type[Any]]
  659. ref_type_str: Optional[str]
  660. child_node: Optional[Node] = None
  661. if node is None:
  662. full_key = key if key is not None else ""
  663. object_type = None
  664. ref_type = None
  665. ref_type_str = None
  666. else:
  667. if key is not None and not node._is_none():
  668. child_node = node._get_node(key, validate_access=False)
  669. try:
  670. full_key = node._get_full_key(key=key)
  671. except Exception as exc:
  672. # Since we are handling an exception, raising a different one here would
  673. # be misleading. Instead, we display it in the key.
  674. full_key = f"<unresolvable due to {type(exc).__name__}: {exc}>"
  675. object_type = OmegaConf.get_type(node)
  676. object_type_str = type_str(object_type)
  677. ref_type = get_type_hint(node)
  678. ref_type_str = type_str(ref_type)
  679. msg = string.Template(msg).safe_substitute(
  680. REF_TYPE=ref_type_str,
  681. OBJECT_TYPE=object_type_str,
  682. KEY=key,
  683. FULL_KEY=full_key,
  684. VALUE=value,
  685. VALUE_TYPE=type_str(type(value), include_module_name=True),
  686. KEY_TYPE=f"{type(key).__name__}",
  687. )
  688. if ref_type not in (None, Any):
  689. template = dedent(
  690. """\
  691. $MSG
  692. full_key: $FULL_KEY
  693. reference_type=$REF_TYPE
  694. object_type=$OBJECT_TYPE"""
  695. )
  696. else:
  697. template = dedent(
  698. """\
  699. $MSG
  700. full_key: $FULL_KEY
  701. object_type=$OBJECT_TYPE"""
  702. )
  703. s = string.Template(template=template)
  704. message = s.substitute(
  705. REF_TYPE=ref_type_str, OBJECT_TYPE=object_type_str, MSG=msg, FULL_KEY=full_key
  706. )
  707. exception_type = type(cause) if type_override is None else type_override
  708. if exception_type == TypeError:
  709. exception_type = ConfigTypeError
  710. elif exception_type == IndexError:
  711. exception_type = ConfigIndexError
  712. ex = exception_type(f"{message}")
  713. if issubclass(exception_type, OmegaConfBaseException):
  714. ex._initialized = True
  715. ex.msg = message
  716. ex.parent_node = node
  717. ex.child_node = child_node
  718. ex.key = key
  719. ex.full_key = full_key
  720. ex.value = value
  721. ex.object_type = object_type
  722. ex.object_type_str = object_type_str
  723. ex.ref_type = ref_type
  724. ex.ref_type_str = ref_type_str
  725. _raise(ex, cause)
  726. def type_str(t: Any, include_module_name: bool = False) -> str:
  727. is_optional, t = _resolve_optional(t)
  728. if t is NoneType:
  729. return str(t.__name__)
  730. if t is Any:
  731. return "Any"
  732. if t is ...:
  733. return "..."
  734. if hasattr(t, "__name__"):
  735. name = str(t.__name__)
  736. elif getattr(t, "_name", None) is not None: # pragma: no cover
  737. name = str(t._name)
  738. elif getattr(t, "__origin__", None) is not None: # pragma: no cover
  739. name = type_str(t.__origin__)
  740. else:
  741. name = str(t)
  742. if name.startswith("typing."): # pragma: no cover
  743. name = name[len("typing.") :]
  744. args = getattr(t, "__args__", None)
  745. if args is not None:
  746. args = ", ".join(
  747. [type_str(t, include_module_name=include_module_name) for t in t.__args__]
  748. )
  749. ret = f"{name}[{args}]"
  750. else:
  751. ret = name
  752. if include_module_name:
  753. if (
  754. hasattr(t, "__module__")
  755. and t.__module__ != "builtins"
  756. and t.__module__ != "typing"
  757. and not t.__module__.startswith("omegaconf.")
  758. ):
  759. module_prefix = str(t.__module__) + "."
  760. else:
  761. module_prefix = ""
  762. ret = module_prefix + ret
  763. if is_optional:
  764. return f"Optional[{ret}]"
  765. else:
  766. return ret
  767. def _ensure_container(target: Any, flags: Optional[Dict[str, bool]] = None) -> Any:
  768. from omegaconf import OmegaConf
  769. if is_primitive_container(target):
  770. assert isinstance(target, (list, dict))
  771. target = OmegaConf.create(target, flags=flags)
  772. elif is_structured_config(target):
  773. target = OmegaConf.structured(target, flags=flags)
  774. elif not OmegaConf.is_config(target):
  775. raise ValueError(
  776. "Invalid input. Supports one of "
  777. + "[dict,list,DictConfig,ListConfig,dataclass,dataclass instance,attr class,attr class instance]"
  778. )
  779. return target
  780. def is_generic_list(type_: Any) -> bool:
  781. """
  782. Checks if a type is a generic list, for example:
  783. list returns False
  784. typing.List returns False
  785. typing.List[T] returns True
  786. :param type_: variable type
  787. :return: bool
  788. """
  789. return is_list_annotation(type_) and get_list_element_type(type_) is not None
  790. def is_generic_dict(type_: Any) -> bool:
  791. """
  792. Checks if a type is a generic dict, for example:
  793. list returns False
  794. typing.List returns False
  795. typing.List[T] returns True
  796. :param type_: variable type
  797. :return: bool
  798. """
  799. return is_dict_annotation(type_) and len(get_dict_key_value_types(type_)) > 0
  800. def is_container_annotation(type_: Any) -> bool:
  801. return is_list_annotation(type_) or is_dict_annotation(type_)
  802. def split_key(key: str) -> List[str]:
  803. """
  804. Split a full key path into its individual components.
  805. This is similar to `key.split(".")` but also works with the getitem syntax:
  806. "a.b" -> ["a", "b"]
  807. "a[b]" -> ["a", "b"]
  808. ".a.b[c].d" -> ["", "a", "b", "c", "d"]
  809. "[a].b" -> ["a", "b"]
  810. """
  811. # Obtain the first part of the key (in docstring examples: a, a, .a, '')
  812. first = KEY_PATH_HEAD.match(key)
  813. assert first is not None
  814. first_stop = first.span()[1]
  815. # `tokens` will contain all elements composing the key.
  816. tokens = key[0:first_stop].split(".")
  817. # Optimization in case `key` has no other component: we are done.
  818. if first_stop == len(key):
  819. return tokens
  820. if key[first_stop] == "[" and not tokens[-1]:
  821. # This is a special case where the first key starts with brackets, e.g.
  822. # [a] or ..[a]. In that case there is an extra "" in `tokens` that we
  823. # need to get rid of:
  824. # [a] -> tokens = [""] but we would like []
  825. # ..[a] -> tokens = ["", "", ""] but we would like ["", ""]
  826. tokens.pop()
  827. # Identify other key elements (in docstring examples: b, b, b/c/d, b)
  828. others = KEY_PATH_OTHER.findall(key[first_stop:])
  829. # There are two groups in the `KEY_PATH_OTHER` regex: one for keys starting
  830. # with a dot (.b, .d) and one for keys starting with a bracket ([b], [c]).
  831. # Only one group can be non-empty.
  832. tokens += [dot_key if dot_key else bracket_key for dot_key, bracket_key in others]
  833. return tokens
  834. # Similar to Python 3.7+'s `contextlib.nullcontext` (which should be used instead,
  835. # once support for Python 3.6 is dropped).
  836. @contextmanager
  837. def nullcontext(enter_result: Any = None) -> Iterator[Any]:
  838. yield enter_result