Skip to content
New issue

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

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

Already on GitHub? # to your account

Support TypedDict #473

Closed
indigoviolet opened this issue Jan 16, 2021 · 3 comments · Fixed by #474
Closed

Support TypedDict #473

indigoviolet opened this issue Jan 16, 2021 · 3 comments · Fixed by #474
Milestone

Comments

@indigoviolet
Copy link

Is your feature request related to a problem? Please describe.

TypedDict is a convenient way to tell mypy about the allowed keys in a dict. However, using a TypedDict as the type of a structured config causes an error.

from omegaconf import OmegaConf, MISSING
import attr
from typing import TypedDict

class Foo(TypedDict):
   foo: str

@attr.s(auto_attribs=True)
class Bar:
   f: Foo = MISSING


Bar(f={'foo': 'a'})
Out [39]:
Bar(f={'foo': 'a'})

In [40]:
OmegaConf.structured(Bar)

Truncated Traceback (Use C-c C-$ to view full TB):
~/dev/devr/py/.venv/lib/python3.8/site-packages/omegaconf/_utils.py in _raise(ex, cause)
    608     else:
    609         ex.__cause__ = None
--> 610     raise ex  # set end OC_CAUSE=1 for full backtrace
    611 
    612 

ValidationError: Unexpected object type : Foo
	full_key: 
	reference_type=None
	object_type=None

Describe the solution you'd like
I'd like TypedDict to be supported.

@omry omry added this to the OmegaConf 2.1 milestone Jan 16, 2021
@omry
Copy link
Owner

omry commented Jan 16, 2021

Thanks for the suggestion.
Adding this on a provisional basis. If it turns out to have a serious conflict downstream I may revert this.

@omry
Copy link
Owner

omry commented Jan 16, 2021

NOTE: The runtime behavior of a typed dict after #474 is that it's just like a regular dict.
The extra type information is ignored at runtime. I am not currently interested in supporting additional inputs beyond dataclass and attr classes for Structured Configs.

@indigoviolet
Copy link
Author

Thanks for the quick feature support, @omry .

@omry omry closed this as completed in #474 Jan 19, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants