Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Python 3.12 type aliases in structured configs #1163

Open
tmke8 opened this issue Mar 5, 2024 · 0 comments
Open

Support for Python 3.12 type aliases in structured configs #1163

tmke8 opened this issue Mar 5, 2024 · 0 comments

Comments

@tmke8
Copy link
Contributor

tmke8 commented 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:

from pathlib import Path

# new type alias syntax
type StrOrPath = str | Path

@dataclass
class C:
    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.

@tmke8 tmke8 changed the title Support for Python 3.12 type aliases Support for Python 3.12 type aliases in structured configs Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant