Open iamhatesz opened 1 year ago
Hi @iamhatesz,
Structured configs (such as GraylogConfigEvaluationExtrasConf
) are supposed to provide some degree of type safety by enforcing a schema. The schema is determined by the fields of the backing dataclass / attr class. You're seeing that error message because there is no context
field in the GraylogConfigEvaluationExtrasConf
dataclass.
Two ways to resolve the issue are (1) add a context
field to GraylogConfigEvaluationExtrasConf
, or (2) replace the dataclass GraylogConfigEvaluationExtrasConf
with an untyped dictionary.
Here's approach 1:
@hydrated_dataclass(target=dict)
class GraylogConfigEvaluationExtrasConf:
context: Any # added context field
run_id: str = "${run.id}"
evaluation_url: str = "${getattr:${resolved_spec},url}"
GraylogConfigConf = builds(
GraylogConfig,
host="${oc.env:GRAYLOG_HOST}",
port="${oc.env:GRAYLOG_PORT}",
extras=GraylogConfigEvaluationExtrasConf,
)
Here's approach 2:
graylog_config_evaluation_extras_conf = { # using untyped dict
"run_id": "${run.id}",
"evaluation_url": "${getattr:${resolved_spec},url}",
}
GraylogConfigConf = builds(
GraylogConfig,
host="${oc.env:GRAYLOG_HOST}",
port="${oc.env:GRAYLOG_PORT}",
extras=graylog_config_evaluation_extras_conf,
)
@Jasha10 thanks for the tip. I was hoping it is possible to achieve both: enforce structure (run_id
and evaluation_url
must be present), but allow for extra fields (like context
) at runtime.
As I understand, I need to use untyped dict for that. However, isn't it a bug that it is possible to override this with CLI, but not in YAML file?
@> However, isn't it a bug that it is possible to override this with CLI, but not in YAML file?
Writing greylog: {extras: {context: evaluation}}
in a yaml file, and then merging that yaml file, is roughly equivalent to writing graylog.extras.context=evaluation
at the command line (without a plus symbol). The plus symbol in +graylog.extras.context=evaluation
means "I know what I'm doing, make the override happen even if it violates the structured config schema". If you want the schema to be enforced, you shouldn't use the plus symbol.
As I understand, I need to use untyped dict for that.
I have a very ugly workaround that uses the _args_
keyword supported by instantiate
to pass a list-of-lists to builtins.dict
.
There's a deprecated (and currently undocumented) feature that may allow you to get typed behavior for run_id
/evaluation_url
and untyped behavior for other keys: inheriting from typing.Dict
.
🚀 Feature Request
I have a
graylog
entry in my config like:Now, I want to add a key to the
graylog.extras
dict in my config file:This fails because of:
But it works using CLI overrides:
Motivation
Is your feature request related to a problem? Please describe. Maybe I am simply missing an existing functionality?
Pitch
Describe the solution you'd like A mechanism to append to a dict inside a config file.
Describe alternatives you've considered I am using CLI overrides as a workaround, but it breaks my rule of keeping all the necessary config for a certain job in its config file.
Are you willing to open a pull request? (See CONTRIBUTING) Sure.
Additional context
N/A