You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
I would like to be able to define type aliases and use them in structured configs:
frompathlibimportPath# new type alias syntaxtypeStrOrPath=str|Path@dataclassclassC:
path: StrOrPath
However, this currently doesn't work:
Full example with traceback.
>>> from dataclasses import dataclass
>>> type Int = int
>>> @dataclass
... class C:
... x: Int
...
>>> from omegaconf import OmegaConf
>>> conf1 = OmegaConf.structured(C)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/omegaconf.py", line 125, in structured
return OmegaConf.create(obj, parent, flags)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/omegaconf.py", line 178, in create
return OmegaConf._create_impl(
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/omegaconf.py", line 900, in _create_impl
format_and_raise(node=None, key=None, value=None, msg=str(e), cause=e)
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/_utils.py", line 819, in format_and_raise
_raise(ex, cause)
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/_utils.py", line 797, in _raise
raise ex.with_traceback(sys.exc_info()[2]) # set env var OC_CAUSE=1 for full trace
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/omegaconf.py", line 861, in _create_impl
return DictConfig(
^^^^^^^^^^^
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/dictconfig.py", line 111, in __init__
format_and_raise(node=None, key=key, value=None, cause=ex, msg=str(ex))
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/_utils.py", line 819, in format_and_raise
_raise(ex, cause)
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/_utils.py", line 797, in _raise
raise ex.with_traceback(sys.exc_info()[2]) # set env var OC_CAUSE=1 for full trace
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/dictconfig.py", line 94, in __init__
self._set_value(content, flags=flags)
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/dictconfig.py", line 647, in _set_value
raise e
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/dictconfig.py", line 644, in _set_value
self._set_value_impl(value, flags)
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/dictconfig.py", line 674, in _set_value_impl
data = get_structured_config_data(value, allow_objects=ao)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/_utils.py", line 495, in get_structured_config_data
return get_dataclass_data(obj, allow_objects=allow_objects)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/_utils.py", line 421, in get_dataclass_data
format_and_raise(
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/_utils.py", line 899, in format_and_raise
_raise(ex, cause)
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/_utils.py", line 797, in _raise
raise ex.with_traceback(sys.exc_info()[2]) # set env var OC_CAUSE=1 for full trace
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/_utils.py", line 413, in get_dataclass_data
d[name] = _maybe_wrap(
^^^^^^^^^^^^
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/omegaconf.py", line 1105, in _maybe_wrap
return _node_wrap(
^^^^^^^^^^^
File "/home/tmk/dev/python/py12proj/.venv/lib/python3.12/site-packages/omegaconf/omegaconf.py", line 1087, in _node_wrap
raise ValidationError(f"Unexpected type annotation: {type_str(ref_type)}")
omegaconf.errors.ValidationError: Unexpected type annotation: Int
full_key: x
object_type=C
Describe the solution you'd like
I think what needs to be done is, the code needs to check for the TypeAliasType objects that are produced by type T = ... and then unwrap the type.
Describe alternatives you've considered
Not using new type alias syntax.
The text was updated successfully, but these errors were encountered:
tmke8
changed the title
Support for Python 3.12 type aliases
Support for Python 3.12 type aliases in structured configs
Mar 5, 2024
Is your feature request related to a problem? Please describe.
I would like to be able to define type aliases and use them in structured configs:
However, this currently doesn't work:
Full example with traceback.
Describe the solution you'd like
I think what needs to be done is, the code needs to check for the
TypeAliasType
objects that are produced bytype T = ...
and then unwrap the type.Describe alternatives you've considered
Not using new type alias syntax.
The text was updated successfully, but these errors were encountered: