-
Notifications
You must be signed in to change notification settings - Fork 116
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
Construction of OmegaConf from Structured Configs #87
Comments
Regarding:
Maybe the user could pass an argument to decide wether the object should be created in the strict mode or not. |
@shagunsodhani, thanks for the suggestion. This brings up the use case of creating a config from a typed Dict or List: d : Dict(str, int) = {"foo" : 10}
cfg = OmegaConf.create(d) This should retain the types but it currently doesn't. |
Need to check if this is even technically possibly, it's possible that without a class the annotations does not actually exists at runtime. |
@shagunsodhani, can't be done as I wrote it. |
Sorry @omry I missed this notification. I think you are right. There are some libraries to get the type from functions/classes but for variables, they only way I could find was to use |
The associated PR (#86 ) is adding support for initializing OmegaConf from class files and objects.
Details below
Config classes and objects (dataclass or attrs classes) can be converted to OmegaConf object.
The following value types are supported:
The semantics here is that this value is not yet set. it must be set prior to read access.
II("foo") is equivalent to ${foo} in the YAML file.
Examples:
foo=II("bar")
: Foo inherits the type and value of bar at runtime.foo=II("http://${host}:${port}/")
: foo is always a string. actual value is determined by host and portfoo=II("env:USER")
: foo is evaluated on access, the type and value is determined by the function (env in this case)Example for bool types (The same is supported for int, str, float and enum):
the typing information in them is available and acted on at runtime (composition and command line overrides are at runtime).
type annotations can be used to get static type checking.
Currently the following is supported for both dataclasses and attr classes.
Examples below are @dataclasses. attr classes are similar.
This class, and objects of this class can be used to construct an OmegaConf object:
Python type annotation can then be used on the config object to static type checking:
To support that, there is also a runtime validation and conversion.
The following will succeed at runtime:
The text was updated successfully, but these errors were encountered: