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

[Bug] [Python 3.10] Crash when using postponed evaluation of annotations #303

Closed
odelalleau opened this issue Jul 15, 2020 · 12 comments · Fixed by #424
Closed

[Bug] [Python 3.10] Crash when using postponed evaluation of annotations #303

odelalleau opened this issue Jul 15, 2020 · 12 comments · Fixed by #424
Labels
bug Something isn't working
Milestone

Comments

@odelalleau
Copy link
Collaborator

🐛 Bug

Structured configs seem not to work with the upcoming postponed evaluation of annotations (https://www.python.org/dev/peps/pep-0563/) which will be the default in Python 3.10.

Obviously not really a problem yet but flagging it now as a FYI.

To reproduce

** Minimal Code/Config snippet to reproduce **

from __future__ import annotations

from dataclasses import dataclass
from omegaconf import OmegaConf

@dataclass
class Config:
    a: int = 0

cfg = OmegaConf.structured(Config)

** Stack trace/error message **

Traceback (most recent call last):
  File "test2.py", line 10, in <module>
    cfg = OmegaConf.structured(Config)
  File "/private/home/odelalleau/.conda/envs/py37-hydra/lib/python3.7/site-packages/omegaconf/omegaconf.py", line 134, in structured
    return OmegaConf.create(obj, parent)
  File "/private/home/odelalleau/.conda/envs/py37-hydra/lib/python3.7/site-packages/omegaconf/omegaconf.py", line 171, in create
    return OmegaConf._create_impl(obj=obj, parent=parent)
  File "/private/home/odelalleau/.conda/envs/py37-hydra/lib/python3.7/site-packages/omegaconf/omegaconf.py", line 220, in _create_impl
    element_type=element_type,
  File "/private/home/odelalleau/.conda/envs/py37-hydra/lib/python3.7/site-packages/omegaconf/dictconfig.py", line 74, in __init__
    self._set_value(content)
  File "/private/home/odelalleau/.conda/envs/py37-hydra/lib/python3.7/site-packages/omegaconf/dictconfig.py", line 546, in _set_value
    data = get_structured_config_data(value)
  File "/private/home/odelalleau/.conda/envs/py37-hydra/lib/python3.7/site-packages/omegaconf/_utils.py", line 258, in get_structured_config_data
    return get_dataclass_data(obj)
  File "/private/home/odelalleau/.conda/envs/py37-hydra/lib/python3.7/site-packages/omegaconf/_utils.py", line 202, in get_dataclass_data
    ref_type=type_, is_optional=is_optional, key=name, value=value, parent=None,
  File "/private/home/odelalleau/.conda/envs/py37-hydra/lib/python3.7/site-packages/omegaconf/omegaconf.py", line 681, in _maybe_wrap
    key=key,
  File "/private/home/odelalleau/.conda/envs/py37-hydra/lib/python3.7/site-packages/omegaconf/omegaconf.py", line 641, in _node_wrap
    elif issubclass(type_, Enum):
TypeError: issubclass() arg 1 must be a class
@omry
Copy link
Owner

omry commented Jul 15, 2020

What is this supposed to do?

@omry
Copy link
Owner

omry commented Jul 15, 2020

Okay, I saw the answer in the pep. Thanks for the heads up.

@omry omry added this to the OmegaConf 2.1 milestone Jul 15, 2020
@omry omry added the bug Something isn't working label Jul 15, 2020
@seminumber
Copy link

I came here to report the same bug. This is truly a bug that I encountered (using other third party tools hydra-core) and took me a whole day to figure out what is being wrong, largely because the TypeError output message is very hard to decipher. Please also consider making early assertions so that the type error reads more sensible (like type annotation for field xx is missing..)

@omry
Copy link
Owner

omry commented Sep 18, 2020

Are you using Python 3.10?
This issue is specific to it. Since it's not officially supported or even easily available yet I am surprised you ran into it.

@omry
Copy link
Owner

omry commented Sep 18, 2020

If you are not using Python 3.10 (as I suspect is the case), please file a different issue with a minimal repro.

@omry omry changed the title [Bug] Crash when using postponed evaluation of annotations [Bug] [Python 3.10] Crash when using postponed evaluation of annotations Sep 18, 2020
@tmke8
Copy link
Contributor

tmke8 commented Oct 27, 2020

If from __future__ import annotations is used (as in the code snippet at the top), then this also happens in Python 3.7+. I just reproduced it with Python 3.7.7.

The solution should be simple: instead of using field.type, use typing.get_type_hints(cls) and then map the resolved types to the field.

@omry
Copy link
Owner

omry commented Oct 28, 2020

At this point this is low priority, but feel free to send a pull request.

@gibiansky
Copy link

I just hit this issue, please merge this PR ASAP! In my experience almost all code that uses types heavily uses from __future__ import annotations, so this is fairly serious.

@omry
Copy link
Owner

omry commented Oct 29, 2020

@gibiansky, mind jumping into the chat to tell me more about your use case?
This issue has not been a priority and the is currently no plan to fix it in 2.0 (and 2.1 is still months away).

@gibiansky
Copy link

A relatively clean workaround is to put your config types / dataclasses in a separate file in which you don't use from __future__ import annotations , then import those, since the future import is per-file.

@omry omry closed this as completed in #424 Oct 30, 2020
@bo5o
Copy link

bo5o commented Jan 25, 2021

I am hitting the same issue with omegaconf 2.0.6 on Python 3.7.9. Will this go into a patch release on 2.0.x or will it be released as part of 2.1?

@omry
Copy link
Owner

omry commented Jan 25, 2021

It is planned to be released with 2.1.
If you don't mind trying a dev release, you can use 2.1.0dev* (e.g 2.1.10dev17). Be sure to report any issues if you do.

Note that there are some known breaking changes, you can look at the news directory for *.api_change.

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

Successfully merging a pull request may close this issue.

6 participants