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

omegaconf does not resolve types before using #963

Closed
4 tasks done
ssnl opened this issue Jun 9, 2022 · 1 comment · Fixed by #979
Closed
4 tasks done

omegaconf does not resolve types before using #963

ssnl opened this issue Jun 9, 2022 · 1 comment · Fixed by #979
Labels
bug Something isn't working structured config

Comments

@ssnl
Copy link

ssnl commented Jun 9, 2022

Describe the bug
When type annotation is a str (e.g., to work around some forward reference), omegaconf does not resolve them before using, leading to various bugs.

To Reproduce

from omegaconf import OmegaConf
import attrs 

@attrs.define
class C:
    @attrs.define
    class CA:
        x: int = 3
        
    @attrs.define
    class CB:
        sub: 'C.CA' = attrs.field()
        
    a: CA 
    b: CB


obj = C(a=C.CA(), b=C.CB(C.CA(x=33)))
OmegaConf.create(obj)

Error is

...
File ~/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py:649, in DictConfig._set_value(self, value, flags)
    [647](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=646) except Exception as e:
    [648](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=647)     self.__dict__["_content"] = previous_content
--> [649](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=648)     raise e

File ~/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py:646, in DictConfig._set_value(self, value, flags)
    [644](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=643) try:
    [645](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=644)     previous_content = self.__dict__["_content"]
--> [646](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=645)     self._set_value_impl(value, flags)
    [647](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=646) except Exception as e:
    [648](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=647)     self.__dict__["_content"] = previous_content

File ~/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py:660, in DictConfig._set_value_impl(self, value, flags)
    [657](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=656)     flags = {}
    [659](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=658) assert not isinstance(value, ValueNode)
--> [660](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=659) self._validate_set(key=None, value=value)
    [662](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=661) if _is_none(value, resolve=True):
    [663](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=662)     self.__dict__["_content"] = None

File ~/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py:206, in DictConfig._validate_set(self, key, value)
    [197](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=196) if is_container_annotation(target_type) and not is_container_annotation(
    [198](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=197)     value_type
    [199](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=198) ):
    [200](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=199)     raise ValidationError(
    [201](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=200)         f"Cannot assign {type_str(value_type)} to {type_str(target_type)}"
    [202](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=201)     )
    [203](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=202) validation_error = (
    [204](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=203)     target_type is not None
    [205](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=204)     and value_type is not None
--> [206](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=205)     and not issubclass(value_type, target_type)
    [207](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=206) )
    [208](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=207) if validation_error:
    [209](file:///Users/S_sn/miniconda3/lib/python3.8/site-packages/omegaconf/dictconfig.py?line=208)     self._raise_invalid_value(value, value_type, target_type)

ConfigTypeError: issubclass() arg 2 must be a class or tuple of classes
    full_key: sub
    object_type=None

caused by having a str ref_type.

Expected behavior
Arguably, omegaconf should use https://docs.python.org/3/library/typing.html#typing.get_type_hints to get the concrete type.

Additional context

  • OmegaConf version: 2.2.1
  • Python version: 3.7
  • Operating system: mac
  • Please provide a minimal repro
@ssnl ssnl added the bug Something isn't working label Jun 9, 2022
@Jasha10
Copy link
Collaborator

Jasha10 commented Jun 11, 2022

Arguably, omegaconf should use https://docs.python.org/3/library/typing.html#typing.get_type_hints to get the concrete type.

👍🏼 Good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working structured config
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants