Fix mutable default error in MetricsConfig dataclasses #3137
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary:
Context
Found a dataclass validation error: "mutable default ... is not allowed: use default_factory". The issue was that
DefaultMetricsConfig
andEmptyMetricsConfig
were created as static instances with mutable objects (lists/dicts), but then used as defaults in dataclass fields. This violates Python's dataclass rules since all instances would share the same mutable objects.Check out this for more info on python dataclasses:
https://docs.python.org/3/library/dataclasses.html#mutable-default-values
Changes
Converted the static config instances to factory functions that return fresh objects each time:
DefaultMetricsConfig
→_create_default_metrics_config()
EmptyMetricsConfig
→_create_empty_metrics_config()
Updated the dataclass field in
TrainingAppConfig
to usefield(default_factory=_create_empty_metrics_config)
instead of the static instance.Now each dataclass gets its own separate config object, fixing the mutable default error.
Differential Revision: D77263018